refactor(alerts): improve active problem fetching and message formatting
Reworked the logic for retrieving data from Zabbix API to make it more efficient and filter-aware. Message generation for Telegram bot was refactored and decoupled from data retrieval logic to improve structure, readability, and reuse. Signed-off-by: UdoChudo <stream@udochudo.ru>
This commit is contained in:
@@ -1,19 +1,20 @@
|
||||
from telebot import types
|
||||
from app.bot.keyboards.main_menu import get_main_menu
|
||||
from app.bot.utils.zabbix import get_region_groups, get_all_groups_for_region, fetch_filtered_triggers
|
||||
from app.bot.utils.tg_formatter import format_trigger_message # ⬅️ добавлено
|
||||
from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton
|
||||
|
||||
def process_region_selection(bot,chat_id, region_id):
|
||||
from app.bot.keyboards.groups import create_groups_keyboard
|
||||
from app.bot.keyboards.main_menu import get_main_menu
|
||||
from app.bot.utils.tg_formatter import format_trigger_for_tg
|
||||
from app.bot.utils.zabbix_alt import (
|
||||
get_region_groups,
|
||||
get_all_groups_for_region,
|
||||
fetch_triggers_data)
|
||||
|
||||
|
||||
def process_region_selection(bot, chat_id, region_id):
|
||||
try:
|
||||
groups = get_region_groups(region_id)
|
||||
if not groups:
|
||||
return bot.send_message(chat_id, "Нет групп хостов для этого региона.")
|
||||
|
||||
markup = types.InlineKeyboardMarkup()
|
||||
for group in groups:
|
||||
markup.add(types.InlineKeyboardButton(text=group['name'], callback_data=f"group_{group['groupid']}"))
|
||||
markup.add(types.InlineKeyboardButton(text="Все группы региона\n(Долгое выполнение)", callback_data=f"all_groups_{region_id}"))
|
||||
|
||||
markup = create_groups_keyboard(groups, region_id)
|
||||
bot.send_message(chat_id, "Выберите группу хостов:", reply_markup=markup)
|
||||
except Exception as e:
|
||||
bot.send_message(chat_id, f"Ошибка при получении групп: {str(e)}", reply_markup=get_main_menu())
|
||||
@@ -21,11 +22,16 @@ def process_region_selection(bot,chat_id, region_id):
|
||||
|
||||
def process_group_selection(bot, chat_id, group_id):
|
||||
try:
|
||||
triggers = fetch_filtered_triggers(group_id)
|
||||
triggers = fetch_triggers_data(group_id)
|
||||
if not triggers:
|
||||
bot.send_message(chat_id, "Нет активных событий.")
|
||||
else:
|
||||
send_trigger_messages(chat_id, triggers)
|
||||
return
|
||||
|
||||
for trigger in triggers:
|
||||
text, url = format_trigger_for_tg(trigger)
|
||||
markup = InlineKeyboardMarkup()
|
||||
markup.add(InlineKeyboardButton(text="Открыть график", url=url))
|
||||
bot.send_message(chat_id, text, reply_markup=markup, parse_mode="HTML")
|
||||
except Exception as e:
|
||||
bot.send_message(chat_id, f"Ошибка при получении событий: {str(e)}")
|
||||
|
||||
@@ -36,21 +42,21 @@ def process_all_groups_request(bot, chat_id, region_id):
|
||||
groups = get_all_groups_for_region(region_id)
|
||||
for group in groups:
|
||||
try:
|
||||
triggers = fetch_filtered_triggers(group['groupid'])
|
||||
triggers = fetch_triggers_data(group['groupid'])
|
||||
if triggers:
|
||||
all_triggers.extend(triggers)
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
if all_triggers:
|
||||
send_trigger_messages(chat_id, all_triggers)
|
||||
else:
|
||||
if not all_triggers:
|
||||
bot.send_message(chat_id, "Нет активных событий.")
|
||||
return
|
||||
|
||||
for trigger in all_triggers:
|
||||
text, url = format_trigger_for_tg(trigger)
|
||||
markup = InlineKeyboardMarkup()
|
||||
markup.add(InlineKeyboardButton(text="Открыть график", url=url))
|
||||
bot.send_message(chat_id, text, reply_markup=markup, parse_mode="HTML")
|
||||
|
||||
except Exception as e:
|
||||
bot.send_message(chat_id, f"Ошибка при получении данных: {str(e)}")
|
||||
|
||||
|
||||
def send_trigger_messages(chat_id, triggers):
|
||||
for trigger in triggers:
|
||||
text = format_trigger_message(trigger)
|
||||
bot.send_message(chat_id, text, parse_mode="MarkdownV2")
|
||||
|
||||
Reference in New Issue
Block a user