Files
notify-bridge/packages/server/src/notify_bridge_server/api/status.py
T
alexei.dolgolyov b803d004e1 refactor: comprehensive codebase review — security, performance, quality, UX
Security:
- Fix NUT protocol command injection (validate names against safe regex)
- Enable Jinja2 autoescape=True to prevent HTML injection via external data
- Add WebhookProviderConfig validation model

Performance:
- Shared aiohttp.ClientSession singleton (replaces 40+ per-request sessions)
- Fix 4 N+1 queries with batch IN loads (poller, scheduler, memory, broadcast)
- asyncio.gather for Gitea commands and notification dispatcher
- Add DB indexes on NotificationTrackerState.tracker_id, CommandTrackerListener
- LRU cache for compiled Jinja2 templates
- Daily EventLog cleanup job (90-day retention)
- 30s HTTP timeout on all external calls
- GROUP BY for target type counts (replaces 7 sequential queries)

Code quality:
- Extract get_owned_entity() helper (replaces 11 duplicate functions)
- Extract slot_helpers.py (load_slots, save_slots, render_template_preview)
- Extract command_utils.py (tracker lookup, last event, collection IDs)
- Extract http_session.py (shared session lifecycle)
- Provider connection validation dedup (3x → 1 helper)
- Command dispatch tables replacing if/elif chains
- Album+links fetch helper (fetch_albums_with_links)
- Provider dispatch polymorphism (list_provider_collections)
- Immutable _enrich_assets (no longer mutates in-place)
- Fix _format_assets return type + handler unpacking

Frontend:
- Fix 18+ hardcoded English strings → t() with new i18n keys (en + ru)
- Mobile "More" nav panel with provider filter and search
- Shared Button.svelte component (4 variants, 2 sizes)
- Shared ErrorBanner.svelte component (8 pages updated)
- SvelteKit goto() replacing window.location.href
- Dashboard grid fixed for 4 cards, paginator opacity consistency

Functionality:
- max_instances=1 on scheduler jobs (prevents duplicate events)
- Webhook provider in watcher (prevents error spam)
- Fix stale SQLModel reference in poller
- Gitea get_repo() direct API call
2026-03-28 13:22:26 +03:00

230 lines
7.8 KiB
Python

"""Status/dashboard API route."""
from datetime import datetime, timedelta, timezone
from fastapi import APIRouter, Depends, Query
from sqlmodel import func, select
from sqlmodel.ext.asyncio.session import AsyncSession
from ..auth.dependencies import get_current_user
from ..database.engine import get_session
from ..database.models import (
CommandConfig,
CommandTemplateConfig,
CommandTracker,
EmailBot,
EventLog,
MatrixBot,
NotificationTarget,
NotificationTracker,
ServiceProvider,
TelegramBot,
TemplateConfig,
TrackingConfig,
User,
)
router = APIRouter(prefix="/api/status", tags=["status"])
@router.get("")
async def get_status(
user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
# Event filtering
event_type: str | None = Query(None),
provider_id: int | None = Query(None),
search: str | None = Query(None),
sort: str = Query("newest"),
limit: int = Query(20, ge=1, le=100),
offset: int = Query(0, ge=0),
):
"""Get dashboard status data with enriched events."""
providers_count = (await session.exec(
select(func.count()).select_from(ServiceProvider).where(ServiceProvider.user_id == user.id)
)).one()
trackers_result = await session.exec(
select(NotificationTracker).where(NotificationTracker.user_id == user.id)
)
trackers = trackers_result.all()
active_count = sum(1 for t in trackers if t.enabled)
targets_count = (await session.exec(
select(func.count()).select_from(NotificationTarget).where(NotificationTarget.user_id == user.id)
)).one()
# Build events query with filters
events_query = (
select(EventLog)
.join(NotificationTracker, EventLog.tracker_id == NotificationTracker.id)
.where(NotificationTracker.user_id == user.id)
)
if event_type:
events_query = events_query.where(EventLog.event_type == event_type)
if provider_id is not None:
events_query = events_query.where(EventLog.provider_id == provider_id)
if search:
events_query = events_query.where(
EventLog.collection_name.contains(search)
| EventLog.tracker_name.contains(search)
| EventLog.provider_name.contains(search)
)
# Count total matching events (for pagination)
count_query = select(func.count()).select_from(events_query.subquery())
total_events = (await session.exec(count_query)).one()
# Sort
if sort == "oldest":
events_query = events_query.order_by(EventLog.created_at.asc())
else:
events_query = events_query.order_by(EventLog.created_at.desc())
events_query = events_query.offset(offset).limit(limit)
recent_events = await session.exec(events_query)
return {
"providers": providers_count,
"trackers": {"total": len(trackers), "active": active_count},
"targets": targets_count,
"total_events": total_events,
"recent_events": [
{
"id": e.id,
"event_type": e.event_type,
"collection_name": e.collection_name,
"tracker_name": e.tracker_name or "",
"provider_name": e.provider_name or "",
"provider_id": e.provider_id,
"assets_count": e.assets_count or 0,
"created_at": e.created_at.isoformat() + ("Z" if not e.created_at.tzinfo else ""),
"details": e.details or {},
}
for e in recent_events.all()
],
}
@router.get("/counts")
async def get_nav_counts(
user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
):
"""Return entity counts for sidebar navigation badges.
Note: queries run sequentially because SQLAlchemy AsyncSession is NOT safe
for concurrent use within a single session (no asyncio.gather). We
minimise round-trips by combining user + system counts and per-type
target counts into single aggregate queries where possible.
"""
counts: dict[str, int] = {}
# --- 1) User-owned entity counts (one query per model) ---
for model, key in [
(ServiceProvider, "providers"),
(NotificationTracker, "notification_trackers"),
(TrackingConfig, "tracking_configs"),
(TemplateConfig, "template_configs"),
(NotificationTarget, "targets"),
(TelegramBot, "telegram_bots"),
(EmailBot, "email_bots"),
(MatrixBot, "matrix_bots"),
(CommandTracker, "command_trackers"),
(CommandConfig, "command_configs"),
(CommandTemplateConfig, "command_template_configs"),
]:
count = (await session.exec(
select(func.count()).select_from(model).where(model.user_id == user.id)
)).one()
counts[key] = count
# --- 2) Add system-owned counts (user_id=0) for shared entities ---
for model, key in [
(TemplateConfig, "template_configs"),
(CommandTemplateConfig, "command_template_configs"),
(TrackingConfig, "tracking_configs"),
(CommandConfig, "command_configs"),
]:
system_count = (await session.exec(
select(func.count()).select_from(model).where(model.user_id == 0)
)).one()
counts[key] += system_count
# --- 3) Per-type target counts in a single query using conditional aggregation ---
target_types = ("telegram", "webhook", "email", "discord", "slack", "ntfy", "matrix")
type_counts_result = (await session.exec(
select(
NotificationTarget.type,
func.count(),
)
.where(
NotificationTarget.user_id == user.id,
NotificationTarget.type.in_(target_types),
)
.group_by(NotificationTarget.type)
)).all()
type_counts_map = dict(type_counts_result)
for target_type in target_types:
counts[f"targets_{target_type}"] = type_counts_map.get(target_type, 0)
return counts
@router.get("/chart")
async def get_event_chart(
user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_session),
days: int = Query(14, ge=1, le=90),
event_type: str | None = Query(None),
provider_id: int | None = Query(None),
search: str | None = Query(None),
):
"""Return daily event counts by type for the last N days, with optional filters."""
cutoff = datetime.now(timezone.utc) - timedelta(days=days)
day_col = func.date(EventLog.created_at)
query = (
select(
day_col.label("day"),
EventLog.event_type,
func.count().label("total"),
)
.join(NotificationTracker, EventLog.tracker_id == NotificationTracker.id)
.where(NotificationTracker.user_id == user.id, EventLog.created_at >= cutoff)
)
if event_type:
query = query.where(EventLog.event_type == event_type)
if provider_id is not None:
query = query.where(EventLog.provider_id == provider_id)
if search:
query = query.where(
EventLog.collection_name.contains(search)
| EventLog.tracker_name.contains(search)
| EventLog.provider_name.contains(search)
)
query = query.group_by(day_col, EventLog.event_type).order_by(day_col)
rows = (await session.exec(query)).all()
# Build a dict: { "2026-03-15": { "assets_added": 18, ... }, ... }
by_day: dict[str, dict[str, int]] = {}
for row in rows:
day_str = str(row.day)
if day_str not in by_day:
by_day[day_str] = {}
by_day[day_str][row.event_type] = row.total
# Fill in missing days so the frontend gets a continuous series
result = []
for i in range(days):
d = (datetime.now(timezone.utc) - timedelta(days=days - 1 - i)).strftime("%Y-%m-%d")
counts = by_day.get(d, {})
result.append({"date": d, **counts})
return {"days": result}