- Added new commands in the Makefile for restarting individual services: `restart-bot`, `restart-prometheus`, and `restart-grafana`. - Updated Prometheus and Grafana dashboard expressions for better metrics aggregation. - Removed the `main_with_metrics.py` file and integrated metrics handling directly into the main bot file. - Refactored middleware to improve metrics tracking and error handling across message and callback processing. - Optimized metrics recording with enhanced bucket configurations for better performance monitoring.
73 lines
2.1 KiB
Python
73 lines
2.1 KiB
Python
"""Service classes for group handlers"""
|
||
|
||
# Standard library imports
|
||
from typing import Protocol, Optional
|
||
|
||
# Third-party imports
|
||
from aiogram import types
|
||
|
||
# Local imports
|
||
from helper_bot.utils.helper_func import send_text_message
|
||
from .exceptions import NoReplyToMessageError, UserNotFoundError
|
||
from logs.custom_logger import logger
|
||
|
||
# Local imports - metrics
|
||
from helper_bot.utils.metrics import (
|
||
metrics,
|
||
track_time,
|
||
track_errors,
|
||
db_query_time
|
||
)
|
||
|
||
|
||
class DatabaseProtocol(Protocol):
|
||
"""Protocol for database operations"""
|
||
def get_user_by_message_id(self, message_id: int) -> Optional[int]: ...
|
||
|
||
|
||
class AdminReplyService:
|
||
"""Service for admin reply operations"""
|
||
|
||
def __init__(self, db: DatabaseProtocol) -> None:
|
||
self.db = db
|
||
|
||
def get_user_id_for_reply(self, message_id: int) -> int:
|
||
"""
|
||
Get user ID for reply by message ID.
|
||
|
||
Args:
|
||
message_id: ID of the message to reply to
|
||
|
||
Returns:
|
||
User ID for the reply
|
||
|
||
Raises:
|
||
UserNotFoundError: If user is not found in database
|
||
"""
|
||
user_id = self.db.get_user_by_message_id(message_id)
|
||
if user_id is None:
|
||
raise UserNotFoundError(f"User not found for message_id: {message_id}")
|
||
return user_id
|
||
|
||
async def send_reply_to_user(
|
||
self,
|
||
chat_id: int,
|
||
message: types.Message,
|
||
reply_text: str,
|
||
markup: types.ReplyKeyboardMarkup
|
||
) -> None:
|
||
"""
|
||
Send reply to user.
|
||
|
||
Args:
|
||
chat_id: User's chat ID
|
||
message: Original message from admin
|
||
reply_text: Text to send to user
|
||
markup: Reply keyboard markup
|
||
"""
|
||
await send_text_message(chat_id, message, reply_text, markup)
|
||
logger.info(
|
||
f'Ответ админа "{reply_text}" отправлен пользователю с ID: {chat_id} '
|
||
f'на сообщение {message.reply_to_message.message_id if message.reply_to_message else "N/A"}'
|
||
)
|