Make incidents diff resilient and send sample if empty
This commit is contained in:
@@ -397,6 +397,7 @@ async def incidents_summary(msg: Message):
|
|||||||
async def incidents_diff(msg: Message):
|
async def incidents_diff(msg: Message):
|
||||||
if not is_admin_msg(msg):
|
if not is_admin_msg(msg):
|
||||||
return
|
return
|
||||||
|
try:
|
||||||
parts = msg.text.split()
|
parts = msg.text.split()
|
||||||
hours = 24
|
hours = 24
|
||||||
reset = False
|
reset = False
|
||||||
@@ -410,8 +411,12 @@ async def incidents_diff(msg: Message):
|
|||||||
hours = 24
|
hours = 24
|
||||||
if reset:
|
if reset:
|
||||||
runtime_state.set_state("incidents_diff_last_ts", None)
|
runtime_state.set_state("incidents_diff_last_ts", None)
|
||||||
await msg.answer("📣 Diff marker reset. Next run will show all within window.", reply_markup=system_logs_audit_kb)
|
await msg.answer(
|
||||||
|
"📣 Diff marker reset. Next run will show all within window.",
|
||||||
|
reply_markup=system_logs_audit_kb,
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
last_iso = runtime_state.get("incidents_diff_last_ts")
|
last_iso = runtime_state.get("incidents_diff_last_ts")
|
||||||
last_dt = None
|
last_dt = None
|
||||||
if isinstance(last_iso, str):
|
if isinstance(last_iso, str):
|
||||||
@@ -436,30 +441,47 @@ async def incidents_diff(msg: Message):
|
|||||||
runtime_state.set_state("incidents_diff_last_ts", None)
|
runtime_state.set_state("incidents_diff_last_ts", None)
|
||||||
fresh = collect(None)
|
fresh = collect(None)
|
||||||
|
|
||||||
|
def fmt_lines(items: list[tuple[datetime, str]], limit_chars: int = 3500) -> str:
|
||||||
|
buf = []
|
||||||
|
total_len = 0
|
||||||
|
for dt, line in items:
|
||||||
|
entry = f"{dt.astimezone().strftime('%m-%d %H:%M')} {line}"
|
||||||
|
if total_len + len(entry) + 1 > limit_chars:
|
||||||
|
break
|
||||||
|
buf.append(entry)
|
||||||
|
total_len += len(entry) + 1
|
||||||
|
return "\n".join(buf)
|
||||||
|
|
||||||
if not fresh:
|
if not fresh:
|
||||||
note = f"since {last_dt.astimezone().strftime('%Y-%m-%d %H:%M')}" if last_dt else "for the period"
|
note = f"since {last_dt.astimezone().strftime('%Y-%m-%d %H:%M')}" if last_dt else "for the period"
|
||||||
if rows:
|
if rows:
|
||||||
sample = rows[-50:]
|
sample = rows[-50:][::-1] # newest first
|
||||||
body = "\n".join(f"{dt.astimezone().strftime('%m-%d %H:%M')} {line}" for dt, line in sample)
|
body = fmt_lines(sample)
|
||||||
await msg.answer(
|
await msg.answer(
|
||||||
f"📣 No new incidents {note} (window {hours}h).\nRecent sample:\n```\n{body}\n```",
|
f"📣 No new incidents {note} (window {hours}h).\nRecent sample:\n```\n{body}\n```",
|
||||||
reply_markup=system_logs_audit_kb,
|
reply_markup=system_logs_audit_kb,
|
||||||
parse_mode="Markdown",
|
parse_mode="Markdown",
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
await msg.answer(f"📣 No new incidents {note} (window {hours}h)", reply_markup=system_logs_audit_kb)
|
await msg.answer(
|
||||||
|
f"📣 No new incidents {note} (window {hours}h)",
|
||||||
|
reply_markup=system_logs_audit_kb,
|
||||||
|
)
|
||||||
return
|
return
|
||||||
fresh.sort(key=lambda x: x[0])
|
|
||||||
body = "\n".join(f"{dt.astimezone().strftime('%m-%d %H:%M')} {line}" for dt, line in fresh[-200:])
|
fresh.sort(key=lambda x: x[0], reverse=True)
|
||||||
|
body = fmt_lines(fresh)
|
||||||
await msg.answer(
|
await msg.answer(
|
||||||
f"📣 New incidents (since last mark, window {hours}h): {len(fresh)}\n```\n{body}\n```",
|
f"📣 New incidents (since last mark, window {hours}h): {len(fresh)}\n```\n{body}\n```",
|
||||||
reply_markup=system_logs_audit_kb,
|
reply_markup=system_logs_audit_kb,
|
||||||
parse_mode="Markdown",
|
parse_mode="Markdown",
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
runtime_state.set_state("incidents_diff_last_ts", fresh[-1][0].isoformat())
|
runtime_state.set_state("incidents_diff_last_ts", fresh[0][0].isoformat())
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
except Exception as e:
|
||||||
|
await msg.answer(f"⚠️ Diff error: {type(e).__name__}: {e}", reply_markup=system_logs_audit_kb)
|
||||||
|
|
||||||
|
|
||||||
@dp.message(F.text.startswith("/alerts_heatmap"))
|
@dp.message(F.text.startswith("/alerts_heatmap"))
|
||||||
|
|||||||
Reference in New Issue
Block a user