From f29816ded4095cd47270d1123db95747bf1633be Mon Sep 17 00:00:00 2001 From: feyhoa Date: Fri, 30 Aug 2024 16:20:29 +0300 Subject: [PATCH] Create statistics class, update user db --- src/commands/user_commands.py | 3 +- src/databases/db.py | 10 ++++- src/utilities/__init__.py | 4 ++ src/utilities/statistics.py | 73 +++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/utilities/statistics.py diff --git a/src/commands/user_commands.py b/src/commands/user_commands.py index 30765e3..1ae2d64 100644 --- a/src/commands/user_commands.py +++ b/src/commands/user_commands.py @@ -6,7 +6,8 @@ from utilities import ( set_translation, - dialog + dialog, + Statistics ) from databases.db import ( diff --git a/src/databases/db.py b/src/databases/db.py index 4d7fe00..fe040bd 100644 --- a/src/databases/db.py +++ b/src/databases/db.py @@ -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 @@ -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): @@ -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): @@ -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() @@ -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() diff --git a/src/utilities/__init__.py b/src/utilities/__init__.py index a066144..480b38b 100644 --- a/src/utilities/__init__.py +++ b/src/utilities/__init__.py @@ -5,3 +5,7 @@ from utilities.wrapper import ( dialog ) + +from utilities.statistics import ( + Statistics +) diff --git a/src/utilities/statistics.py b/src/utilities/statistics.py new file mode 100644 index 0000000..6c99ccc --- /dev/null +++ b/src/utilities/statistics.py @@ -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()