Add log time ranges and filter
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import json
|
||||
import time
|
||||
from aiogram import F
|
||||
from aiogram.types import CallbackQuery, InlineKeyboardMarkup, InlineKeyboardButton
|
||||
from app import dp, ADMIN_ID
|
||||
from services.docker import docker_cmd
|
||||
from state import DOCKER_MAP
|
||||
from state import DOCKER_MAP, LOG_FILTER_PENDING
|
||||
from handlers.backup import cmd_backup_status
|
||||
|
||||
|
||||
@@ -25,11 +26,22 @@ async def docker_callback(cb: CallbackQuery):
|
||||
)
|
||||
|
||||
elif action == "logs":
|
||||
await cb.answer("Loading logs…")
|
||||
rc, out = await docker_cmd(["logs", "--tail", "80", real])
|
||||
|
||||
await cb.answer()
|
||||
opts = InlineKeyboardMarkup(
|
||||
inline_keyboard=[
|
||||
[
|
||||
InlineKeyboardButton(text="Tail 80", callback_data=f"logsopt:tail:{alias}"),
|
||||
InlineKeyboardButton(text="10m", callback_data=f"logsopt:since:{alias}:600"),
|
||||
InlineKeyboardButton(text="1h", callback_data=f"logsopt:since:{alias}:3600"),
|
||||
],
|
||||
[
|
||||
InlineKeyboardButton(text="Filter", callback_data=f"logsopt:filter:{alias}")
|
||||
],
|
||||
]
|
||||
)
|
||||
await cb.message.answer(
|
||||
f"📜 **Logs: {alias}**\n```{out}```",
|
||||
f"📜 **Logs options: {alias}**",
|
||||
reply_markup=opts,
|
||||
parse_mode="Markdown"
|
||||
)
|
||||
|
||||
@@ -83,3 +95,58 @@ async def snapshot_back(cb: CallbackQuery):
|
||||
# просто вызываем статус снова
|
||||
fake_msg = cb.message
|
||||
await cmd_backup_status(fake_msg)
|
||||
|
||||
|
||||
@dp.callback_query(F.data.startswith("logsopt:"))
|
||||
async def logs_options(cb: CallbackQuery):
|
||||
if cb.from_user.id != ADMIN_ID:
|
||||
return
|
||||
|
||||
parts = cb.data.split(":")
|
||||
if len(parts) < 3:
|
||||
await cb.answer("Bad request")
|
||||
return
|
||||
|
||||
action = parts[1]
|
||||
alias = parts[2]
|
||||
real = DOCKER_MAP.get(alias)
|
||||
if not real:
|
||||
await cb.answer("Container not found")
|
||||
return
|
||||
|
||||
if action == "tail":
|
||||
await cb.answer("Loading logs…")
|
||||
rc, out = await docker_cmd(["logs", "--tail", "80", real])
|
||||
await cb.message.answer(
|
||||
f"📜 **Logs: {alias}**\n```{out}```",
|
||||
parse_mode="Markdown"
|
||||
)
|
||||
return
|
||||
|
||||
if action == "since" and len(parts) == 4:
|
||||
try:
|
||||
seconds = int(parts[3])
|
||||
except ValueError:
|
||||
await cb.answer("Bad request")
|
||||
return
|
||||
since_ts = str(int(time.time() - seconds))
|
||||
await cb.answer("Loading logs…")
|
||||
rc, out = await docker_cmd(["logs", "--since", since_ts, "--tail", "200", real])
|
||||
await cb.message.answer(
|
||||
f"📜 **Logs: {alias}**\n```{out}```",
|
||||
parse_mode="Markdown"
|
||||
)
|
||||
return
|
||||
|
||||
if action == "filter":
|
||||
LOG_FILTER_PENDING[cb.from_user.id] = {
|
||||
"alias": alias,
|
||||
"since_sec": 1800,
|
||||
}
|
||||
await cb.message.answer(
|
||||
f"🔎 Send filter text for `{alias}` (logs last 30m).",
|
||||
parse_mode="Markdown",
|
||||
)
|
||||
return
|
||||
|
||||
await cb.answer("Bad request")
|
||||
|
||||
Reference in New Issue
Block a user