From 6f602f417f0df9192d6680f61d1d344b30252aae Mon Sep 17 00:00:00 2001 From: mihalin Date: Fri, 18 Feb 2022 21:47:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D1=82=D0=B8=D1=81=D1=82=D0=B8=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- olgram/commands/info.py | 10 ++++-- olgram/commands/menu.py | 31 +++++++++++++++++++ .../models/9_20220218211744_update.sql | 6 ++++ olgram/models/models.py | 3 ++ server/custom.py | 7 +++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 olgram/migrations/models/9_20220218211744_update.sql diff --git a/olgram/commands/info.py b/olgram/commands/info.py index eb1ef34..11e2600 100644 --- a/olgram/commands/info.py +++ b/olgram/commands/info.py @@ -20,10 +20,16 @@ async def info(message: types.Message, state: FSMContext): await message.answer("Недостаточно прав") return - bots_count = len(await models.Bot.all()) + bots = await models.Bot.all() + bots_count = len(bots) user_count = len(await models.User.all()) templates_count = len(await models.DefaultAnswer.all()) + income_messages = sum([bot.incoming_messages_count for bot in bots]) + outgoing_messages = sum([bot.outgoing_messages_count for bot in bots]) + await message.answer(f"Количество ботов: {bots_count}\n" f"Количество пользователей: {user_count}\n" - f"Шаблонов ответов: {templates_count}\n") + f"Шаблонов ответов: {templates_count}\n" + f"Входящих сообщений у всех ботов: {income_messages}\n" + f"Исходящих сообщений у всех ботов: {outgoing_messages}\n") diff --git a/olgram/commands/menu.py b/olgram/commands/menu.py index 79029cb..fa59a6e 100644 --- a/olgram/commands/menu.py +++ b/olgram/commands/menu.py @@ -107,6 +107,11 @@ async def send_bot_menu(bot: Bot, call: types.CallbackQuery): callback_data=menu_callback.new(level=2, bot_id=bot.id, operation="delete", chat=empty)) ) + keyboard.insert( + types.InlineKeyboardButton(text="Статистика", + callback_data=menu_callback.new(level=2, bot_id=bot.id, operation="stat", + chat=empty)) + ) keyboard.insert( types.InlineKeyboardButton(text="<< Назад", callback_data=menu_callback.new(level=0, bot_id=empty, operation=empty, chat=empty)) @@ -174,6 +179,30 @@ async def send_bot_text_menu(bot: Bot, call: ty.Optional[types.CallbackQuery] = await AioBot.get_current().send_message(chat_id, text, reply_markup=keyboard, parse_mode="HTML") +async def send_bot_statistic_menu(bot: Bot, call: ty.Optional[types.CallbackQuery] = None, + chat_id: ty.Optional[int] = None): + if call: + await call.answer() + keyboard = types.InlineKeyboardMarkup(row_width=2) + keyboard.insert( + types.InlineKeyboardButton(text="<< Назад", + callback_data=menu_callback.new(level=1, bot_id=bot.id, operation=empty, chat=empty)) + ) + + text = dedent(f""" + Статистика по боту @{bot.name} + + Входящих сообщений: {bot.incoming_messages_count} + Ответов: {bot.outgoing_messages_count} + Шаблоны ответов: {len(await bot.answers)} + Забанено пользователей: {len(await bot.banned_users)} + """) + if call: + await edit_or_create(call, text, keyboard, parse_mode="HTML") + else: + await AioBot.get_current().send_message(chat_id, text, reply_markup=keyboard, parse_mode="HTML") + + async def send_bot_second_text_menu(bot: Bot, call: ty.Optional[types.CallbackQuery] = None, chat_id: ty.Optional[int] = None): if call: @@ -329,6 +358,8 @@ async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMCon return await send_chats_menu(bot, call) if operation == "delete": return await send_bot_delete_menu(bot, call) + if operation == "stat": + return await send_bot_statistic_menu(bot, call) if operation == "text": await state.set_state("wait_start_text") async with state.proxy() as proxy: diff --git a/olgram/migrations/models/9_20220218211744_update.sql b/olgram/migrations/models/9_20220218211744_update.sql new file mode 100644 index 0000000..71ca41c --- /dev/null +++ b/olgram/migrations/models/9_20220218211744_update.sql @@ -0,0 +1,6 @@ +-- upgrade -- +ALTER TABLE "bot" ADD "outgoing_messages_count" BIGINT NOT NULL DEFAULT 0; +ALTER TABLE "bot" ADD "incoming_messages_count" BIGINT NOT NULL DEFAULT 0; +-- downgrade -- +ALTER TABLE "bot" DROP COLUMN "outgoing_messages_count"; +ALTER TABLE "bot" DROP COLUMN "incoming_messages_count"; diff --git a/olgram/models/models.py b/olgram/models/models.py index 20f2e6d..18cd4e4 100644 --- a/olgram/models/models.py +++ b/olgram/models/models.py @@ -38,6 +38,9 @@ class Bot(Model): on_delete=fields.relational.CASCADE, null=True) + incoming_messages_count = fields.BigIntField(default=0) + outgoing_messages_count = fields.BigIntField(default=0) + def decrypted_token(self): cryptor = DatabaseSettings.cryptor() return cryptor.decrypt(self.token) diff --git a/server/custom.py b/server/custom.py index ef4808a..f72f5da 100644 --- a/server/custom.py +++ b/server/custom.py @@ -7,6 +7,7 @@ from contextvars import ContextVar from aiohttp.web_exceptions import HTTPNotFound from aioredis.commands import create_redis_pool from aioredis import Redis +from tortoise.expressions import F import logging import typing as ty from olgram.settings import ServerSettings @@ -55,6 +56,9 @@ async def message_handler(message: types.Message, *args, **kwargs): return SendMessage(chat_id=message.chat.id, text="Вы заблокированы в этом боте") + bot.incoming_messages_count = F("incoming_messages_count") + 1 + await bot.save(update_fields=["incoming_messages_count"]) + # Пересылаем сообщение в супер-чат if is_super_group: thread_first_message = await _redis.get(_thread_uniqie_id(bot.pk, message.chat.id)) @@ -109,6 +113,9 @@ async def message_handler(message: types.Message, *args, **kwargs): await message.reply("Невозможно переслать сообщение (автор заблокировал бота?)", parse_mode="HTML") return + + bot.outgoing_messages_count = F("outgoing_messages_count") + 1 + await bot.save(update_fields=["outgoing_messages_count"]) elif super_chat_id > 0: # в супер-чате кто-то пишет сообщение сам себе, только для личных сообщений await message.forward(super_chat_id)