From b1d95a1496c84aa1ce99320dfde13f31593742d5 Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Tue, 8 Oct 2024 16:18:08 +0300 Subject: [PATCH] feat: make save_messages optional in statistics --- .../standard/statistics/__init__.py | 9 +++++++++ .../karkas_blocks/standard/statistics/info.json | 1 + .../karkas_blocks/standard/statistics/main.py | 16 +++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/karkas_blocks/karkas_blocks/standard/statistics/__init__.py b/src/karkas_blocks/karkas_blocks/standard/statistics/__init__.py index ab5e22e..a77f1cf 100644 --- a/src/karkas_blocks/karkas_blocks/standard/statistics/__init__.py +++ b/src/karkas_blocks/karkas_blocks/standard/statistics/__init__.py @@ -1,3 +1,5 @@ +from typing import TYPE_CHECKING + from karkas_core.modules_system.public_api import ( get_module, register_outer_message_middleware, @@ -5,8 +7,15 @@ from karkas_core.modules_system.public_api import ( from .main import StatisticsMiddleware +if TYPE_CHECKING: + from karkas_blocks.standard.config import IConfig + +config: "IConfig" = get_module("standard.config", "config") + def module_init(): + config.register("statistics::save_messages", "boolean", default_value=False) + register_app_config = get_module("standard.database", "register_app_config") from .db import APP_CONFIG diff --git a/src/karkas_blocks/karkas_blocks/standard/statistics/info.json b/src/karkas_blocks/karkas_blocks/standard/statistics/info.json index 1f3e0d8..af756c2 100644 --- a/src/karkas_blocks/karkas_blocks/standard/statistics/info.json +++ b/src/karkas_blocks/karkas_blocks/standard/statistics/info.json @@ -7,6 +7,7 @@ "privileged": true, "dependencies": { "required": { + "standard.config": "^1.0.0", "standard.database": "^1.0.0" } }, diff --git a/src/karkas_blocks/karkas_blocks/standard/statistics/main.py b/src/karkas_blocks/karkas_blocks/standard/statistics/main.py index e0cd72c..374df56 100644 --- a/src/karkas_blocks/karkas_blocks/standard/statistics/main.py +++ b/src/karkas_blocks/karkas_blocks/standard/statistics/main.py @@ -7,6 +7,12 @@ from .db.tables import ChatStats, Messages, UserStats if TYPE_CHECKING: from aiogram.types import Message, TelegramObject + from karkas_blocks.standard.config import IConfig + +from karkas_core.modules_system.public_api import get_module + +config: "IConfig" = get_module("standard.config", "config") + async def update_chat_stats(event: "Message"): await ChatStats.insert( @@ -53,6 +59,12 @@ async def save_messages(event: "Message"): ) +async def get_user_stats(chat_id: int, user_id: int): + return ( + await UserStats.select().where(UserStats.key == f"{chat_id}-{user_id}").first() + ) + + class StatisticsMiddleware(BaseMiddleware): async def __call__( self, @@ -63,7 +75,9 @@ class StatisticsMiddleware(BaseMiddleware): await update_chat_stats(event) await update_user_stats(event) - await save_messages(event) + + if config.get("statistics::save_messages"): + await save_messages(event) result = await handler(event, data)