Refactor project structure and remove obsolete files
- Deleted the Makefile, `README_TESTING.md`, and several deployment scripts to streamline the project. - Updated `.dockerignore` to exclude unnecessary development files. - Adjusted database schema comments for clarity. - Refactored metrics handling in middleware for improved command extraction and logging. - Enhanced command mappings for buttons and callbacks in constants for better maintainability. - Start refactor voice bot
This commit is contained in:
@@ -3,7 +3,7 @@ Metrics middleware for aiogram 3.x.
|
||||
Automatically collects metrics for message processing, command execution, and errors.
|
||||
"""
|
||||
|
||||
from typing import Any, Awaitable, Callable, Dict
|
||||
from typing import Any, Awaitable, Callable, Dict, Union, Optional
|
||||
from aiogram import BaseMiddleware
|
||||
from aiogram.types import TelegramObject, Message, CallbackQuery
|
||||
from aiogram.enums import ChatType
|
||||
@@ -11,6 +11,18 @@ import time
|
||||
import logging
|
||||
from ..utils.metrics import metrics
|
||||
|
||||
# Import button command mapping
|
||||
try:
|
||||
from ..handlers.private.constants import BUTTON_COMMAND_MAPPING
|
||||
from ..handlers.callback.constants import CALLBACK_COMMAND_MAPPING
|
||||
from ..handlers.admin.constants import ADMIN_BUTTON_COMMAND_MAPPING, ADMIN_COMMANDS
|
||||
except ImportError:
|
||||
# Fallback if constants not available
|
||||
BUTTON_COMMAND_MAPPING = {}
|
||||
CALLBACK_COMMAND_MAPPING = {}
|
||||
ADMIN_BUTTON_COMMAND_MAPPING = {}
|
||||
ADMIN_COMMANDS = {}
|
||||
|
||||
|
||||
class MetricsMiddleware(BaseMiddleware):
|
||||
"""Middleware for automatic metrics collection in aiogram handlers."""
|
||||
@@ -35,23 +47,11 @@ class MetricsMiddleware(BaseMiddleware):
|
||||
if isinstance(event, Message):
|
||||
self.logger.info(f"📊 Processing Message event")
|
||||
await self._record_message_metrics(event)
|
||||
if event.text and event.text.startswith('/'):
|
||||
command_info = {
|
||||
'command': event.text.split()[0][1:], # Remove '/' and get command name
|
||||
'user_type': "user" if event.from_user else "unknown",
|
||||
'handler_type': "message_handler"
|
||||
}
|
||||
command_info = self._extract_command_info(event)
|
||||
elif isinstance(event, CallbackQuery):
|
||||
self.logger.info(f"📊 Processing CallbackQuery event")
|
||||
await self._record_callback_metrics(event)
|
||||
if event.data:
|
||||
parts = event.data.split(':', 1)
|
||||
if parts:
|
||||
command_info = {
|
||||
'command': parts[0],
|
||||
'user_type': "user" if event.from_user else "unknown",
|
||||
'handler_type': "callback_handler"
|
||||
}
|
||||
command_info = self._extract_callback_command_info(event)
|
||||
else:
|
||||
self.logger.info(f"📊 Processing unknown event type: {type(event).__name__}")
|
||||
|
||||
@@ -167,6 +167,62 @@ class MetricsMiddleware(BaseMiddleware):
|
||||
async def _record_callback_metrics(self, callback: CallbackQuery):
|
||||
"""Record callback metrics efficiently."""
|
||||
metrics.record_message("callback_query", "callback", "callback_handler")
|
||||
|
||||
def _extract_command_info(self, message: Message) -> Optional[Dict[str, str]]:
|
||||
"""Extract command information from message (commands or button clicks)."""
|
||||
if not message.text:
|
||||
return None
|
||||
|
||||
# Check if it's a slash command
|
||||
if message.text.startswith('/'):
|
||||
command_name = message.text.split()[0][1:] # Remove '/' and get command name
|
||||
# Check if it's an admin command
|
||||
if command_name in ADMIN_COMMANDS:
|
||||
return {
|
||||
'command': ADMIN_COMMANDS[command_name],
|
||||
'user_type': "admin" if message.from_user else "unknown",
|
||||
'handler_type': "admin_handler"
|
||||
}
|
||||
else:
|
||||
return {
|
||||
'command': command_name,
|
||||
'user_type': "user" if message.from_user else "unknown",
|
||||
'handler_type': "message_handler"
|
||||
}
|
||||
|
||||
# Check if it's an admin button click
|
||||
if message.text in ADMIN_BUTTON_COMMAND_MAPPING:
|
||||
return {
|
||||
'command': ADMIN_BUTTON_COMMAND_MAPPING[message.text],
|
||||
'user_type': "admin" if message.from_user else "unknown",
|
||||
'handler_type': "admin_button_handler"
|
||||
}
|
||||
|
||||
# Check if it's a regular button click (text button)
|
||||
if message.text in BUTTON_COMMAND_MAPPING:
|
||||
return {
|
||||
'command': BUTTON_COMMAND_MAPPING[message.text],
|
||||
'user_type': "user" if message.from_user else "unknown",
|
||||
'handler_type': "button_handler"
|
||||
}
|
||||
|
||||
return None
|
||||
|
||||
def _extract_callback_command_info(self, callback: CallbackQuery) -> Optional[Dict[str, str]]:
|
||||
"""Extract command information from callback query."""
|
||||
if not callback.data:
|
||||
return None
|
||||
|
||||
# Extract command from callback data
|
||||
parts = callback.data.split(':', 1)
|
||||
if parts and parts[0] in CALLBACK_COMMAND_MAPPING:
|
||||
return {
|
||||
'command': CALLBACK_COMMAND_MAPPING[parts[0]],
|
||||
'user_type': "user" if callback.from_user else "unknown",
|
||||
'handler_type': "callback_handler"
|
||||
}
|
||||
|
||||
return None
|
||||
|
||||
|
||||
class DatabaseMetricsMiddleware(BaseMiddleware):
|
||||
|
||||
Reference in New Issue
Block a user