добавлен модуль со статистикой

This commit is contained in:
Maxim Slipenko 2024-08-20 16:56:50 +03:00
parent a28e0b308f
commit 7c196371c0
No known key found for this signature in database
GPG Key ID: 6DEA9FA7DE98C7D6
9 changed files with 474 additions and 8 deletions

View File

@ -10,23 +10,16 @@ async def main():
[
block_loader("standard", "config", safe=False),
block_loader("standard", "database", safe=False),
block_loader("standard", "statistics", safe=False),
block_loader("standard", "command_helper"),
block_loader("standard", "roles", safe=False),
block_loader("standard", "fsm_database_storage", safe=False),
block_loader("external", "create_report_apps"),
# block_loader("experimental", "message_db_logger", safe=False),
block_loader("standard", "info"),
block_loader("standard", "help"),
# block_loader("standard", "config", safe=False),
# block_loader("standard", "database", safe=False),
# block_loader("standard", "fsm_database_storage", safe=False),
# block_loader("standard", "roles", safe=False),
# block_loader("external", "yandexgpt", safe=False),
#
# block_loader("standard", "command_helper"),
# block_loader("standard", "info"),
# block_loader("standard", "filters"),
# block_loader("external", "create_report_apps"),
# block_loader("standard", "admin"),
# block_loader("standard", "message_processing"),
# block_loader("standard", "miniapp", safe=False),

View File

@ -0,0 +1,15 @@
from karkas_core.modules_system.public_api import (
get_module,
register_outer_message_middleware,
)
from .main import StatisticsMiddleware
def module_init():
register_app_config = get_module("standard.database", "register_app_config")
from .db import APP_CONFIG
register_app_config(APP_CONFIG)
register_outer_message_middleware(StatisticsMiddleware())

View File

@ -0,0 +1 @@
from .piccolo_app import APP_CONFIG

View File

@ -0,0 +1,15 @@
import os
from karkas_piccolo.conf.apps import AppConfig
from .tables import ChatStats, Messages, UserStats
CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__))
APP_CONFIG = AppConfig(
app_name="standard.statistics",
migrations_folder_path=os.path.join(CURRENT_DIRECTORY, "piccolo_migrations"),
table_classes=[ChatStats, Messages, UserStats],
migration_dependencies=[],
commands=[],
)

View File

@ -0,0 +1,322 @@
from piccolo.apps.migrations.auto.migration_manager import MigrationManager
from piccolo.columns.column_types import Date, Integer, Text
from piccolo.columns.defaults.date import DateNow
from piccolo.columns.indexes import IndexMethod
ID = "2024-08-20T16:28:38:371951"
VERSION = "1.16.0"
DESCRIPTION = ""
async def forwards():
manager = MigrationManager(
migration_id=ID, app_name="standard.statistics", description=DESCRIPTION
)
manager.add_table(
class_name="Messages", tablename="messages", schema=None, columns=None
)
manager.add_table(
class_name="ChatStats", tablename="chat_stats", schema=None, columns=None
)
manager.add_table(
class_name="UserStats", tablename="user_stats", schema=None, columns=None
)
manager.add_column(
table_class_name="Messages",
tablename="messages",
column_name="key",
db_column_name="key",
column_class_name="Text",
column_class=Text,
params={
"default": "",
"null": False,
"primary_key": True,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
schema=None,
)
manager.add_column(
table_class_name="Messages",
tablename="messages",
column_name="chat_id",
db_column_name="chat_id",
column_class_name="Integer",
column_class=Integer,
params={
"default": 0,
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
schema=None,
)
manager.add_column(
table_class_name="Messages",
tablename="messages",
column_name="message_id",
db_column_name="message_id",
column_class_name="Integer",
column_class=Integer,
params={
"default": 0,
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
schema=None,
)
manager.add_column(
table_class_name="Messages",
tablename="messages",
column_name="sender_id",
db_column_name="sender_id",
column_class_name="Integer",
column_class=Integer,
params={
"default": 0,
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
schema=None,
)
manager.add_column(
table_class_name="Messages",
tablename="messages",
column_name="answer_to_message_id",
db_column_name="answer_to_message_id",
column_class_name="Integer",
column_class=Integer,
params={
"default": None,
"null": True,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
schema=None,
)
manager.add_column(
table_class_name="Messages",
tablename="messages",
column_name="message_text",
db_column_name="message_text",
column_class_name="Text",
column_class=Text,
params={
"default": "",
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
schema=None,
)
manager.add_column(
table_class_name="ChatStats",
tablename="chat_stats",
column_name="chat_id",
db_column_name="chat_id",
column_class_name="Integer",
column_class=Integer,
params={
"default": 0,
"null": False,
"primary_key": True,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
schema=None,
)
manager.add_column(
table_class_name="ChatStats",
tablename="chat_stats",
column_name="date",
db_column_name="date",
column_class_name="Date",
column_class=Date,
params={
"default": DateNow(),
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
schema=None,
)
manager.add_column(
table_class_name="ChatStats",
tablename="chat_stats",
column_name="messages_count",
db_column_name="messages_count",
column_class_name="Integer",
column_class=Integer,
params={
"default": 0,
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
schema=None,
)
manager.add_column(
table_class_name="UserStats",
tablename="user_stats",
column_name="key",
db_column_name="key",
column_class_name="Text",
column_class=Text,
params={
"default": "",
"null": False,
"primary_key": True,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
schema=None,
)
manager.add_column(
table_class_name="UserStats",
tablename="user_stats",
column_name="chat_id",
db_column_name="chat_id",
column_class_name="Integer",
column_class=Integer,
params={
"default": 0,
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
schema=None,
)
manager.add_column(
table_class_name="UserStats",
tablename="user_stats",
column_name="user_id",
db_column_name="user_id",
column_class_name="Integer",
column_class=Integer,
params={
"default": 0,
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
schema=None,
)
manager.add_column(
table_class_name="UserStats",
tablename="user_stats",
column_name="date",
db_column_name="date",
column_class_name="Date",
column_class=Date,
params={
"default": DateNow(),
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
schema=None,
)
manager.add_column(
table_class_name="UserStats",
tablename="user_stats",
column_name="messages_count",
db_column_name="messages_count",
column_class_name="Integer",
column_class=Integer,
params={
"default": 0,
"null": False,
"primary_key": False,
"unique": False,
"index": False,
"index_method": IndexMethod.btree,
"choices": None,
"db_column_name": None,
"secret": False,
},
schema=None,
)
return manager

View File

@ -0,0 +1,34 @@
from piccolo.columns import Date, Integer, Text
from piccolo.table import Table
class ChatStats(Table):
chat_id = Integer(primary_key=True)
date = Date()
messages_count = Integer(default=0)
class Messages(Table):
# Временная мера, пока не примут
# https://github.com/piccolo-orm/piccolo/pull/984
# {message_chat_id}-{message_id}
key = Text(primary_key=True)
chat_id = Integer()
message_id = Integer()
sender_id = Integer()
answer_to_message_id = Integer(null=True, default=None)
# message_ai_model = pw.TextField(null=True)
message_text = Text()
class UserStats(Table):
# Временная мера, пока не примут
# https://github.com/piccolo-orm/piccolo/pull/984
# {chat_id}-{user_id}
key = Text(primary_key=True)
chat_id = Integer()
user_id = Integer()
date = Date()
messages_count = Integer(default=0)

View File

@ -0,0 +1,18 @@
{
"id": "standard.statistics",
"name": "Статистика",
"description": "Очень полезный модуль",
"author": "Karkas Team",
"version": "1.0.0",
"privileged": true,
"dependencies": {
"required": {
"standard.database": "^1.0.0"
}
},
"pythonDependencies": {
"required": {
"piccolo": "*"
}
}
}

View File

@ -0,0 +1,68 @@
from typing import Any, Awaitable, Callable, Dict
from aiogram import BaseMiddleware
from aiogram.types import Message, TelegramObject
from .db.tables import ChatStats, Messages, UserStats
async def update_chat_stats(event: Message):
await ChatStats.insert(
ChatStats(chat_id=event.chat.id, date=event.date, messages_count=1)
).on_conflict(
action="DO UPDATE",
values=[
ChatStats.date,
(ChatStats.messages_count, ChatStats.messages_count + 1),
],
).run()
async def update_user_stats(event: Message):
await UserStats.insert(
UserStats(
key=f"{event.chat.id}-{event.from_user.id}",
chat_id=event.chat.id,
user_id=event.from_user.id,
date=event.date,
messages_count=1,
)
).on_conflict(
action="DO UPDATE",
values=[
UserStats.date,
(UserStats.messages_count, UserStats.messages_count + 1),
],
).run()
async def save_messages(event: Message):
await Messages.insert(
Messages(
key=f"{event.chat.id}-{event.message_id}",
chat_id=event.chat.id,
message_id=event.message_id,
sender_id=event.from_user.id,
answer_to_message_id=(
event.reply_to_message.message_id if event.reply_to_message else None
),
message_text=event.text,
)
)
class StatisticsMiddleware(BaseMiddleware):
async def __call__(
self,
handler: Callable[[TelegramObject, Dict[str, Any]], Awaitable[Any]],
event: TelegramObject,
data: Dict[str, Any],
) -> Any:
await update_chat_stats(event)
await update_user_stats(event)
await save_messages(event)
result = await handler(event, data)
return result