Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#161 Statistics #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/commands/user_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from utilities import (
set_translation,
dialog
dialog,
Statistics
)

from databases.db import (
Expand Down
10 changes: 9 additions & 1 deletion src/databases/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import List, Optional
from string import punctuation
from collections import Counter

import nltk
from nltk.corpus import stopwords
from pymystem3 import Mystem
Expand Down Expand Up @@ -49,6 +50,7 @@ class User(MongoModel):
feelings = fields.ListField(fields.DictField())
ready_flag = fields.BooleanField()
last_usage = fields.DateTimeField()
registration_date = fields.DateTimeField()
preferences = fields.ListField(fields.DictField()) # [{"voice mode": False - text mode}, {"pronoun": True - Вы}]

def get_last_focus(self):
Expand All @@ -61,7 +63,11 @@ def get_last_focus(self):
# return f'{self.id} | {self.first_name} | {self.last_name}'

def __str__(self):
return f'{self.id} | {self.language_code} | {self.first_name} | {self.last_name}'
return (f'{self.id} | '
f'{self.language_code} | '
f'{self.first_name} | '
f'{self.last_name} | '
f'{self.registration_date.strftime("%d/%m/%Y, %H:%M:%S")}')


class Schedule(MongoModel):
Expand Down Expand Up @@ -161,6 +167,7 @@ def create_admin(user_id: int) -> None:
is_admin=True,
username='admin',
language_code='ru',
registration_date=datetime.datetime.now(),
).save()


Expand Down Expand Up @@ -191,6 +198,7 @@ def init_user(user) -> User:
initial_reason=' ',
initial_reason_flag=False,
language_code=user.language_code,
registration_date=datetime.datetime.now(),
).save()


Expand Down
4 changes: 4 additions & 0 deletions src/utilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
from utilities.wrapper import (
dialog
)

from utilities.statistics import (
Statistics
)
73 changes: 73 additions & 0 deletions src/utilities/statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import datetime
from typing import Optional

from databases import db


class Statistics:
def __init__(self):
self.period = 'day'

def set_period(self, period: str) -> None:
if period in ('day', 'week', 'month', 'year'):
self.period = period

def get_period(
self, start_day: datetime.date = None, period: str = None
) -> Optional[tuple[datetime.date, datetime.date]]:
today = start_day
if start_day is None:
today = datetime.date.today()

period_value = period
if period is None:
period_value = self.period

match period_value:
case 'day':
timedelta = datetime.timedelta(days=1)
end_day = today + timedelta
case 'week':
timedelta = datetime.timedelta(weeks=1)
end_day = today + timedelta
case 'month':
timedelta = datetime.timedelta(days=30)
end_day = today + timedelta
case 'year':
timedelta = datetime.timedelta(days=365)
end_day = today + timedelta
case _:
print(f"Warning: unknown period '{self.period}'")
return None
return today, end_day

@staticmethod
def get_new_users_for_period(
start_date: datetime.date, end_date: datetime.date
) -> tuple[list, int]:
"""
:return: list of questions like list[dict], not list[User]
"""

start, end = start_date, end_date
if not isinstance(start_date, datetime.date) or not isinstance(end_date, datetime.date):
raise TypeError(f"Error: wrong type of date, expected datetime.date")

queries = db.User.objects.raw(
{
'registration_date': {
'$lt': end,
'$gte': start
}
},
{
'id': True
}
)

count = queries.count()
return list(queries), count

@staticmethod
def get_all_users_count() -> int:
return db.User.objects.count()
Loading