diff --git a/central/app/control_plane.py b/central/app/control_plane.py index ad2116d..5acebc7 100644 --- a/central/app/control_plane.py +++ b/central/app/control_plane.py @@ -4,19 +4,34 @@ from __future__ import annotations from datetime import datetime, timedelta, timezone from decimal import Decimal, ROUND_HALF_UP +from urllib.parse import urlencode from sqlalchemy import Numeric, and_, case, cast, func, or_, select from .config import settings from .events import list_events from .installation_identity import compute_agent_health, compute_version_drift -from .models import Agent, AgentEvent, AgentSnapshot, AuditEvent, BlockedProduct, Installation, Job, Tenant -from .panel_presentation import format_age, format_duration, format_ui_datetime, health_label, operational_priority +from .models import Agent, AgentEnrollment, AgentEvent, AgentSnapshot, AuditEvent, BlockedProduct, Installation, Job, Tenant +from .panel_presentation import blocker_reason_label, blocker_type_label, fiscal_status_label, format_age, format_duration, format_ui_datetime, health_label, job_kind_label, job_status_label, operational_priority, priority_label HEALTH_STATES = ("ONLINE", "DEGRADED", "OFFLINE", "FAILED", "UNKNOWN") BLOCKER_FIELDS = ("CFOP", "ST", "ICMS", "CSOSN", "NCM", "OTHER") ATTENTION_CANDIDATE_LIMIT = 10 +INACTIVE_ENROLLMENT_STATES = ("REVOKED", "FAILED", "EXPIRED") + + +def operational_installation_predicate(): + """Only installations eligible for current operational indicators. + + Legacy Agents without an enrollment remain eligible. Disabling job + processing is not the same as deactivating an installation. + """ + inactive_enrollment = select(AgentEnrollment.id).where( + AgentEnrollment.agent_id == Agent.id, + AgentEnrollment.status.in_(INACTIVE_ENROLLMENT_STATES), + ).exists() + return and_(Tenant.active.is_(True), Agent.enabled.is_(True), ~inactive_enrollment) def _page(page: int, page_size: int): @@ -167,7 +182,7 @@ def installation_view(installation, agent, tenant, snapshot=None, *, now=None): "coverage_percent": snapshot.coverage_percent if has_snapshot else None, "fiscal_completion_percent": _fiscal_completion(snapshot) if has_snapshot else None, "fiscal_status": fiscal_status if has_snapshot else "UNKNOWN", - "fiscal_status_label": fiscal_status.title() if has_snapshot else "Não informado", + "fiscal_status_label": fiscal_status_label(fiscal_status) if has_snapshot else "Não informado", "blockers": snapshot.blockers or {}, "last_job_status": snapshot.last_job_status, "last_job_at": _iso(snapshot.last_job_at), @@ -186,7 +201,7 @@ def installation_view(installation, agent, tenant, snapshot=None, *, now=None): def _installation_query_filters(query, *, client=None, version=None, search=None, last_seen_before=None): if client: query = query.where(or_(Tenant.name.ilike(f"%{client}%"), Installation.customer_code.ilike(f"%{client}%"))) - if version: + if version and version != "outdated": query = query.where(Agent.agent_version == version) if search: query = query.where(or_(Installation.display_name.ilike(f"%{search}%"), Installation.hostname.ilike(f"%{search}%"), Tenant.name.ilike(f"%{search}%"))) @@ -195,11 +210,17 @@ def _installation_query_filters(query, *, client=None, version=None, search=None return query -def list_installations(session, *, page=1, page_size=50, client=None, health=None, version=None, search=None, last_seen_before=None, sort="display_name", now=None): +def list_installations(session, *, page=1, page_size=50, client=None, health=None, version=None, search=None, last_seen_before=None, sort="display_name", scope="active", now=None): page, page_size = _page(page, page_size) now = _utc_now(now) - query = select(Installation, Agent, Tenant, AgentSnapshot).join(Agent, Agent.id == Installation.agent_id).join(Tenant, Tenant.id == Installation.tenant_id).outerjoin(AgentSnapshot, AgentSnapshot.installation_id == Installation.id) + query = select(Installation, Agent, Tenant, AgentSnapshot, operational_installation_predicate().label("operational_active")).join(Agent, Agent.id == Installation.agent_id).join(Tenant, Tenant.id == Installation.tenant_id).outerjoin(AgentSnapshot, AgentSnapshot.installation_id == Installation.id) + if scope == "active": + query = query.where(operational_installation_predicate()) + elif scope != "all": + raise ValueError("scope must be active or all") query = _installation_query_filters(query, client=client, version=version, search=search, last_seen_before=last_seen_before) + if version == "outdated": + query = query.where(or_(AgentSnapshot.version_drift == "OUTDATED", and_(Agent.agent_version.is_not(None), Agent.agent_version != settings.agent_server_version))) health_expression = _health_expression(now) if health: query = query.where(health_expression == health) @@ -208,7 +229,7 @@ def list_installations(session, *, page=1, page_size=50, client=None, health=Non else: ordered = query.order_by(Installation.display_name.asc(), Installation.id.asc()) rows = session.execute(ordered.offset((page - 1) * page_size).limit(page_size)).all() - items = [installation_view(i, a, t, s, now=now) for i, a, t, s in rows] + items = [{**installation_view(i, a, t, s, now=now), "operational_active": bool(active)} for i, a, t, s, active in rows] count_query = select(func.count()).select_from(query.with_only_columns(Installation.id).order_by(None).subquery()) total = session.scalar(count_query) or 0 return {"page": page, "page_size": page_size, "items": items, "total": total, "health_filter_applied": health} @@ -223,8 +244,10 @@ def _blocker_item(row, tenant, installation=None, agent=None, *, now=None): "description": row.description, "field": row.field, "blocker_type": row.blocker_type, + "blocker_type_label": blocker_type_label(row.blocker_type), "reason": row.reason_code, "reason_code": row.reason_code, + "reason_label": blocker_reason_label(row.reason_code), "evidence_summary": row.evidence_summary, "first_seen_at": _iso(row.first_seen_at), "last_seen_at": _iso(row.last_seen_at), @@ -235,6 +258,7 @@ def _blocker_item(row, tenant, installation=None, agent=None, *, now=None): "resolved_at": _iso(row.resolved_at), "resolution_source": row.resolution_source, "priority": operational_priority(row), + "priority_label": priority_label(operational_priority(row)), "installation_id": installation.installation_id if installation else None, "installation_db_id": installation.id if installation else None, "display_name": installation.display_name if installation else None, @@ -260,8 +284,12 @@ def _parse_age(value): return None -def _blockers_query(session, *, tenant_id=None, agent_id=None, product_code=None, blocker_type=None, field=None, status="OPEN", client=None, installation=None, search=None, age=None): +def _blockers_query(session, *, tenant_id=None, agent_id=None, product_code=None, blocker_type=None, field=None, reason_code=None, status="OPEN", client=None, installation=None, search=None, age=None, scope="active"): query = select(BlockedProduct, Tenant, Installation, Agent).join(Tenant, Tenant.id == BlockedProduct.tenant_id).outerjoin(Installation, Installation.id == BlockedProduct.installation_id).outerjoin(Agent, Agent.id == Installation.agent_id) + if scope == "active": + query = query.where(operational_installation_predicate(), Installation.id.is_not(None)) + elif scope != "all": + raise ValueError("scope must be active or all") status = (status or "OPEN").upper() if status == "OPEN": query = query.where(BlockedProduct.resolved_at.is_(None)) @@ -276,7 +304,9 @@ def _blockers_query(session, *, tenant_id=None, agent_id=None, product_code=None if blocker_type: query = query.where(BlockedProduct.blocker_type == blocker_type) if field: - query = query.where(BlockedProduct.field == field) + query = query.where(func.upper(BlockedProduct.field) == field.upper()) + if reason_code: + query = query.where(BlockedProduct.reason_code == reason_code) if client: query = query.where(or_(Tenant.name.ilike(f"%{client}%"), BlockedProduct.company.ilike(f"%{client}%"))) if installation: @@ -290,8 +320,8 @@ def _blockers_query(session, *, tenant_id=None, agent_id=None, product_code=None return query -def blocker_filter_summary(session, *, tenant_id=None, agent_id=None, product_code=None, blocker_type=None, field=None, status="OPEN", client=None, installation=None, age=None, search=None): - query = _blockers_query(session, tenant_id=tenant_id, agent_id=agent_id, product_code=product_code, blocker_type=blocker_type, field=field, status=status, client=client, installation=installation, search=search, age=age) +def blocker_filter_summary(session, *, tenant_id=None, agent_id=None, product_code=None, blocker_type=None, field=None, reason_code=None, status="OPEN", client=None, installation=None, age=None, search=None, scope="active"): + query = _blockers_query(session, tenant_id=tenant_id, agent_id=agent_id, product_code=product_code, blocker_type=blocker_type, field=field, reason_code=reason_code, status=status, client=client, installation=installation, search=search, age=age, scope=scope) open_count = func.sum(case((BlockedProduct.resolved_at.is_(None), 1), else_=0)) open_count, = session.execute(query.with_only_columns(open_count).order_by(None)).one() rows = session.execute(_blocker_breakdown_query(query)).all() @@ -314,11 +344,11 @@ def _blocker_breakdown_query(query): return query.with_only_columns(BlockedProduct.field, func.count(BlockedProduct.id)).group_by(BlockedProduct.field).order_by(None) -def list_blockers(session, *, tenant_id=None, agent_id=None, product_code=None, page=1, page_size=50, blocker_type=None, field=None, resolved=False, client=None, installation=None, status="OPEN", age=None, search=None): +def list_blockers(session, *, tenant_id=None, agent_id=None, product_code=None, page=1, page_size=50, blocker_type=None, field=None, reason_code=None, resolved=False, client=None, installation=None, status="OPEN", age=None, search=None, scope="active"): page, page_size = _page(page, page_size) if resolved and status == "OPEN": status = "RESOLVED" - query = _blockers_query(session, tenant_id=tenant_id, agent_id=agent_id, product_code=product_code, blocker_type=blocker_type, field=field, status=status, client=client, installation=installation, search=search, age=age) + query = _blockers_query(session, tenant_id=tenant_id, agent_id=agent_id, product_code=product_code, blocker_type=blocker_type, field=field, reason_code=reason_code, status=status, client=client, installation=installation, search=search, age=age, scope=scope) ordered = query.order_by(BlockedProduct.last_seen_at.desc(), BlockedProduct.id.desc()) rows = session.execute(ordered.offset((page - 1) * page_size).limit(page_size)).all() items = [_blocker_item(blocker, tenant, installation_row, agent, now=datetime.now(timezone.utc)) for blocker, tenant, installation_row, agent in rows] @@ -335,7 +365,9 @@ def _job_item(row, agent, installation, tenant, *, now=None): "display_name": installation.display_name if installation else None, "hostname": installation.hostname if installation else None, "kind": row.kind, + "kind_label": job_kind_label(row.kind), "status": row.status, + "status_label": job_status_label(row.status), "created_at": _iso(row.created_at), "started_at": _iso(row.started_at), "finished_at": _iso(row.finished_at), @@ -349,9 +381,13 @@ def _job_item(row, agent, installation, tenant, *, now=None): } -def list_jobs(session, *, agent_id=None, job_id=None, page=1, page_size=50, status=None, kind=None, since=None, until=None, client=None, search=None): +def list_jobs(session, *, agent_id=None, job_id=None, page=1, page_size=50, status=None, kind=None, since=None, until=None, client=None, search=None, scope="active"): page, page_size = _page(page, page_size) query = select(Job, Agent, Installation, Tenant).join(Agent, Agent.id == Job.agent_id).join(Installation, Installation.agent_id == Agent.id).join(Tenant, Tenant.id == Installation.tenant_id) + if scope == "active": + query = query.where(operational_installation_predicate()) + elif scope != "all": + raise ValueError("scope must be active or all") if agent_id is not None: query = query.where(Job.agent_id == agent_id) if job_id is not None: @@ -376,9 +412,26 @@ def list_jobs(session, *, agent_id=None, job_id=None, page=1, page_size=50, stat return {"page": page, "page_size": page_size, "items": [_job_item(row, agent, installation, tenant) for row, agent, installation, tenant in rows], "total": total} +def jobs_status_summary(session, *, scope="active"): + """Bounded job counts for the current operational scope.""" + query = (select(Job.status, func.count(Job.id)).join(Agent, Agent.id == Job.agent_id) + .join(Installation, Installation.agent_id == Agent.id) + .join(Tenant, Tenant.id == Installation.tenant_id)) + if scope == "active": + query = query.where(operational_installation_predicate()) + elif scope != "all": + raise ValueError("scope must be active or all") + counts = {status: 0 for status in ("PENDING", "RUNNING", "COMPLETED", "FAILED")} + for status, count in session.execute(query.group_by(Job.status)): + if status in counts: + counts[status] = int(count) + return counts + + def dashboard_summary(session, now=None): now = _utc_now(now) - installation_query = select(Installation, Agent, Tenant, AgentSnapshot).join(Agent, Agent.id == Installation.agent_id).join(Tenant, Tenant.id == Installation.tenant_id).outerjoin(AgentSnapshot, AgentSnapshot.installation_id == Installation.id) + active = operational_installation_predicate() + installation_query = select(Installation, Agent, Tenant, AgentSnapshot).join(Agent, Agent.id == Installation.agent_id).join(Tenant, Tenant.id == Installation.tenant_id).outerjoin(AgentSnapshot, AgentSnapshot.installation_id == Installation.id).where(active) health_expression = _health_expression(now) health_rows = session.execute( select(health_expression.label("health"), func.count(Installation.id)) @@ -386,6 +439,7 @@ def dashboard_summary(session, now=None): .join(Agent, Agent.id == Installation.agent_id) .join(Tenant, Tenant.id == Installation.tenant_id) .outerjoin(AgentSnapshot, AgentSnapshot.installation_id == Installation.id) + .where(active) .group_by(health_expression) ).all() counts = {state: 0 for state in HEALTH_STATES} @@ -407,20 +461,23 @@ def dashboard_summary(session, now=None): func.coalesce(func.sum(case((and_(~snapshot_has_negative, snapshot_explicit <= snapshot_total), snapshot_total - snapshot_explicit), else_=0)), 0), func.coalesce(func.sum(case((and_(~snapshot_has_negative, snapshot_explicit > snapshot_total), snapshot_explicit - snapshot_total), else_=0)), 0), func.coalesce(func.sum(case((snapshot_has_negative, 1), else_=0)), 0), - ).select_from(Installation).outerjoin(AgentSnapshot, AgentSnapshot.installation_id == Installation.id) + ).select_from(Installation).join(Agent, Agent.id == Installation.agent_id).join(Tenant, Tenant.id == Installation.tenant_id).outerjoin(AgentSnapshot, AgentSnapshot.installation_id == Installation.id).where(active) ).one() totals = dict(zip(("total_products", "fiscally_complete", "safe_blocked", "failed_products", "unclassified_products", "coverage_overcount_products", "coverage_invalid_snapshots"), (int(value or 0) for value in total_row))) totals["coverage_invariant_pass"] = totals["coverage_overcount_products"] == 0 and totals["coverage_invalid_snapshots"] == 0 open_filter = BlockedProduct.resolved_at.is_(None) - open_blockers = session.scalar(select(func.count(BlockedProduct.id)).where(open_filter)) or 0 + active_blockers = (select(BlockedProduct).join(Installation, Installation.id == BlockedProduct.installation_id) + .join(Agent, Agent.id == Installation.agent_id).join(Tenant, Tenant.id == BlockedProduct.tenant_id) + .where(open_filter, active)) + open_blockers = session.scalar(active_blockers.with_only_columns(func.count(BlockedProduct.id)).order_by(None)) or 0 breakdown_expression = func.coalesce(BlockedProduct.field, "OTHER") - breakdown_rows = session.execute(select(breakdown_expression, func.count(BlockedProduct.id)).where(open_filter).group_by(breakdown_expression)).all() + breakdown_rows = session.execute(active_blockers.with_only_columns(breakdown_expression, func.count(BlockedProduct.id)).group_by(breakdown_expression).order_by(None)).all() open_blocker_breakdown = {field: 0 for field in BLOCKER_FIELDS} for field, count in breakdown_rows: key = str(field or "").upper() open_blocker_breakdown[key if key in open_blocker_breakdown else "OTHER"] += int(count) - product_pairs = select(BlockedProduct.tenant_id, BlockedProduct.product_code).where(open_filter).distinct().subquery() + product_pairs = active_blockers.with_only_columns(BlockedProduct.tenant_id, BlockedProduct.product_code).distinct().subquery() safe_blocked_products = session.scalar(select(func.count()).select_from(product_pairs)) or 0 blocker_counts = {field: count for field, count in open_blocker_breakdown.items() if count} @@ -433,28 +490,47 @@ def dashboard_summary(session, now=None): outdated_query = installation_query.where(version_filter).order_by(Installation.id.asc()).limit(ATTENTION_CANDIDATE_LIMIT) outdated_agents = [installation_view(i, a, t, s, now=now) for i, a, t, s in session.execute(outdated_query).all()] - blocker_query = select(BlockedProduct, Tenant, Installation, Agent, AgentSnapshot).join(Tenant, Tenant.id == BlockedProduct.tenant_id).outerjoin(Installation, Installation.id == BlockedProduct.installation_id).outerjoin(Agent, Agent.id == Installation.agent_id).outerjoin(AgentSnapshot, AgentSnapshot.installation_id == Installation.id).where(open_filter, func.upper(BlockedProduct.field).in_(BLOCKER_FIELDS[:-1])).order_by(BlockedProduct.last_seen_at.desc(), BlockedProduct.id.desc()).limit(ATTENTION_CANDIDATE_LIMIT) - blocker_candidates = session.execute(blocker_query).all() + group_field = func.upper(func.coalesce(BlockedProduct.field, "OTHER")) + fiscal_priority = case( + (func.upper(BlockedProduct.blocker_type).in_(("TECHNICAL_FAILURE", "CONNECTION_ERROR", "AGENT_OFFLINE", "JOB_FAILED")), 0), + (func.upper(BlockedProduct.blocker_type).in_(("BLOCKED_MISSING_EVIDENCE", "MISSING_EVIDENCE")), 1), + (group_field.in_(BLOCKER_FIELDS[:-1]), 2), + else_=3, + ) + blocker_groups = session.execute( + active_blockers.with_only_columns( + Tenant.name, Installation.display_name, Installation.hostname, Agent.id, + group_field, BlockedProduct.blocker_type, BlockedProduct.reason_code, + func.count(BlockedProduct.id), func.count(func.distinct(BlockedProduct.product_code)), + func.max(BlockedProduct.last_seen_at), + ).group_by(Tenant.name, Installation.id, Installation.display_name, Installation.hostname, + Agent.id, group_field, BlockedProduct.blocker_type, BlockedProduct.reason_code) + .order_by(fiscal_priority.asc(), func.max(BlockedProduct.last_seen_at).desc(), Agent.id.asc()) + .limit(ATTENTION_CANDIDATE_LIMIT) + ).all() attention = [] for health in ("FAILED", "DEGRADED", "OFFLINE", "UNKNOWN"): rank = {"FAILED": 0, "DEGRADED": 1, "OFFLINE": 2, "UNKNOWN": 3}[health] attention.extend((_attention_item(item, item["health_reason"], _technical_timestamp(item), rank=rank, now=now), rank, item["agent_id"]) for item in health_candidates[health]) - for blocker, tenant, installation, agent, snapshot in blocker_candidates: - if installation and agent: - item = installation_view(installation, agent, tenant, snapshot, now=now) - else: - item = {"client": tenant.name, "display_name": None, "hostname": None, "agent_id": None, "technical_health": "UNKNOWN", "health_reason": "tenant-level blocker", "last_seen_at": None, "last_job_at": None} - attention.append((_attention_item(item, f"{blocker.field or blocker.blocker_type}: {blocker.reason_code}", blocker.last_seen_at, rank=4, link=f"/blockers/{blocker.id}", now=now), 4, blocker.id)) + for client_name, display_name, hostname, agent_id, field, blocker_type, reason, count, products, last_seen, in blocker_groups: + item = {"client": client_name, "display_name": display_name, "hostname": hostname, "agent_id": agent_id, "technical_health": "BLOCKED"} + link = "/blockers?" + urlencode({"scope": "active", "status": "OPEN", "agent_id": agent_id, "field": field, "blocker_type": blocker_type, "reason_code": reason}) + priority = operational_priority({"field": field, "blocker_type": blocker_type, "reason_code": reason}) + rank = {"CRITICAL": 3, "HIGH": 4, "NORMAL": 5, "LOW": 6}[priority] + entry = _attention_item(item, f"{field}: {reason}", last_seen, rank=rank, link=link, now=now) + entry.update(group_count=count, products_affected=products, field=field, priority=priority) + attention.append((entry, rank, agent_id)) for item in outdated_agents: - attention.append((_attention_item(item, "Versão do agent desatualizada", item.get("last_seen_at"), rank=5, now=now), 5, item["agent_id"])) + attention.append((_attention_item(item, "Versão do agent desatualizada", item.get("last_seen_at"), rank=7, now=now), 7, item["agent_id"])) attention.sort(key=lambda entry: (entry[1], -(entry[0]["timestamp"] is not None), entry[2])) - failed_jobs = session.scalar(select(func.count(Job.id)).where(Job.status == "FAILED")) or 0 + active_jobs = select(Job).join(Agent, Agent.id == Job.agent_id).join(Installation, Installation.agent_id == Agent.id).join(Tenant, Tenant.id == Installation.tenant_id).where(active) + failed_jobs = session.scalar(active_jobs.with_only_columns(func.count(Job.id)).where(Job.status == "FAILED").order_by(None)) or 0 failed_job_rows = session.execute( select(Job, Agent, Installation, Tenant).join(Agent, Agent.id == Job.agent_id) .join(Installation, Installation.agent_id == Agent.id).join(Tenant, Tenant.id == Installation.tenant_id) - .where(Job.status == "FAILED").order_by(Job.created_at.desc(), Job.id.desc()).limit(10) + .where(Job.status == "FAILED", active).order_by(Job.created_at.desc(), Job.id.desc()).limit(10) ).all() recent_failed_jobs = [] for job, agent, installation, tenant in failed_job_rows: @@ -482,7 +558,7 @@ def dashboard_summary(session, now=None): def blocker_detail(session, blocker_id): - row = session.execute(_blockers_query(session, status="ALL").where(BlockedProduct.id == blocker_id)).first() + row = session.execute(_blockers_query(session, status="ALL", scope="all").where(BlockedProduct.id == blocker_id)).first() if not row: return None blocker, tenant, installation, agent = row diff --git a/central/app/panel_presentation.py b/central/app/panel_presentation.py index 67cc5d4..ef77de0 100644 --- a/central/app/panel_presentation.py +++ b/central/app/panel_presentation.py @@ -99,3 +99,34 @@ def health_label(value) -> str: return "-" text = str(value) return _HEALTH_LABELS.get(text.upper(), text) + + +def _human_label(value, labels): + if not value: + return "Não informado" + text = str(value).upper() + return labels.get(text, str(value).replace("_", " ").lower().capitalize()) + + +def fiscal_status_label(value): + return _human_label(value, {"BLOCKED": "Bloqueado", "COMPLETE": "Completo", "INCOMPLETE": "Incompleto", "UNKNOWN": "Não informado"}) + + +def job_status_label(value): + return _human_label(value, {"PENDING": "Pendente", "RUNNING": "Executando", "COMPLETED": "Concluído", "FAILED": "Falhou"}) + + +def job_kind_label(value): + return _human_label(value, {"SYNC_INCREMENTAL": "Sincronização incremental", "INITIAL_SYNC": "Sincronização inicial", "REPROCESS_ALL": "Reprocessar tudo", "REPROCESS_PRODUCTS": "Reprocessar produtos"}) + + +def blocker_type_label(value): + return _human_label(value, {"BLOCKED_MISSING_EVIDENCE": "Evidência fiscal ausente", "MISSING_EVIDENCE": "Evidência fiscal ausente", "TECHNICAL_FAILURE": "Falha técnica", "CONNECTION_ERROR": "Erro de conexão"}) + + +def blocker_reason_label(value): + return _human_label(value, {"INVALID_NCM": "NCM inválido", "INSUFFICIENT_EVIDENCE": "Evidência insuficiente", "MISSING_CONTEXT": "Contexto fiscal ausente", "NO_EVIDENCE": "Sem evidência"}) + + +def priority_label(value): + return _human_label(value, {"CRITICAL": "Crítica", "HIGH": "Alta", "NORMAL": "Média", "MEDIUM": "Média", "LOW": "Baixa"}) diff --git a/central/app/web.py b/central/app/web.py index 207f0a3..653596d 100644 --- a/central/app/web.py +++ b/central/app/web.py @@ -14,7 +14,7 @@ from . import agent_distribution, client_sync, config, enrollment, sso from .config import settings from .blocker_resolution import BlockerResolutionUnavailable, _company_key, resolve_blocker_with_context from .context_keys import ALLOWED_KEYS, ENUMS -from .control_plane import blocker_detail, blocker_filter_summary, dashboard_summary, installation_view, list_blockers, list_installations, list_jobs, list_panel_events +from .control_plane import blocker_detail, blocker_filter_summary, dashboard_summary, installation_view, jobs_status_summary, list_blockers, list_installations, list_jobs, list_panel_events from .human_auth import create_session, current_session, require_csrf, verify_password from .panel_presentation import format_ui_datetime from .models import AdminUser, Agent, AgentEnrollment, AgentRelease, AgentSnapshot, BlockedProduct, FiscalContextEntry, Installation, Job, Tenant @@ -162,11 +162,15 @@ def router(session_factory): finally: session.close() @routes.get("/agents", response_class=HTMLResponse) - def agents(request: Request, page: int = 1, search: str | None = None, health: str | None = None, sort: str = "display_name"): + def agents(request: Request, page: int = 1, search: str | None = None, health: str | None = None, sort: str = "display_name", scope: str = "active", version: str | None = None): session, auth_data, redirect = page_auth(request) if redirect: return redirect try: - return render(request, "agents.html", {"result": list_installations(session, page=page, page_size=50, search=search, health=health, sort=sort), "auth": auth_data, "search": search or "", "health_filter": health or "", "sort": sort, "query_base": query_base(search=search, health=health, sort=sort)}) + scope = scope if scope in ("active", "all") else "active" + health = health if health in ("ONLINE", "DEGRADED", "OFFLINE", "FAILED", "UNKNOWN") else None + sort = sort if sort in ("display_name", "worst_coverage") else "display_name" + version = version if version == "outdated" else None + return render(request, "agents.html", {"result": list_installations(session, page=page, page_size=50, search=search, health=health, sort=sort, scope=scope, version=version), "auth": auth_data, "search": search or "", "health_filter": health or "", "sort": sort, "scope_filter": scope, "version_filter": version or "", "query_base": query_base(search=search, health=health, sort=sort, scope=scope, version=version)}) finally: session.close() # --- Static /agents/* routes MUST be declared before /agents/{agent_id}: @@ -278,8 +282,8 @@ def router(session_factory): events = events[:25] for event in events: event["occurred_display"] = format_ui_datetime(datetime.fromisoformat(event["occurred_at"]) if event["occurred_at"] else None) - agent_blockers = list_blockers(session, tenant_id=installation.tenant_id, agent_id=agent_id, page=1, page_size=25) - return render(request, "agent_detail.html", {"detail": detail, "tab": tab, "blockers": agent_blockers, "blocker_breakdown": blocker_filter_summary(session, tenant_id=installation.tenant_id, agent_id=agent_id, status="OPEN")["blocker_breakdown"], "jobs": list_jobs(session, agent_id=agent_id, page=1, page_size=25), "events": events, "event_page": max(1, event_page), "has_more": has_more, "severity": severity or "", "event_type": event_type or "", "job_id": job_id or "", "product_code": product_code or "", "since": since or "", "until": until or "", "event_query_base": query_base(tab=tab, severity=severity, event_type=event_type, job_id=job_id, product_code=product_code, since=since, until=until), "auth": auth_data, "csrf_token": request.cookies.get("gti_csrf", ""), "ops": _ops_view(session, installation, agent)}) + agent_blockers = list_blockers(session, tenant_id=installation.tenant_id, agent_id=agent_id, page=1, page_size=25, scope="all") + return render(request, "agent_detail.html", {"detail": detail, "tab": tab, "blockers": agent_blockers, "blocker_breakdown": blocker_filter_summary(session, tenant_id=installation.tenant_id, agent_id=agent_id, status="OPEN", scope="all")["blocker_breakdown"], "jobs": list_jobs(session, agent_id=agent_id, page=1, page_size=25, scope="all"), "events": events, "event_page": max(1, event_page), "has_more": has_more, "severity": severity or "", "event_type": event_type or "", "job_id": job_id or "", "product_code": product_code or "", "since": since or "", "until": until or "", "event_query_base": query_base(tab=tab, severity=severity, event_type=event_type, job_id=job_id, product_code=product_code, since=since, until=until), "auth": auth_data, "csrf_token": request.cookies.get("gti_csrf", ""), "ops": _ops_view(session, installation, agent)}) finally: session.close() def _ops_view(session, installation, agent): @@ -463,14 +467,16 @@ def router(session_factory): finally: session.close() @routes.get("/blockers", response_class=HTMLResponse) - def blockers(request: Request, page: int = 1, client: str | None = None, installation: str | None = None, field: str | None = None, blocker_type: str | None = None, status: str = "OPEN", age: str | None = None, search: str | None = None, agent_id: int | None = None, product_code: str | None = None): + def blockers(request: Request, page: int = 1, client: str | None = None, installation: str | None = None, field: str | None = None, blocker_type: str | None = None, reason_code: str | None = None, status: str = "OPEN", age: str | None = None, search: str | None = None, agent_id: int | None = None, product_code: str | None = None, scope: str = "active"): session, auth_data, redirect = page_auth(request) if redirect: return redirect try: - filters = {"agent_id": agent_id, "product_code": product_code, "client": client, "installation": installation, "field": field, "blocker_type": blocker_type, "status": status, "age": age, "search": search} + scope = scope if scope in ("active", "all") else "active" + status = status if status in ("OPEN", "RESOLVED", "ALL") else "OPEN" + filters = {"agent_id": agent_id, "product_code": product_code, "client": client, "installation": installation, "field": field, "blocker_type": blocker_type, "reason_code": reason_code, "status": status, "age": age, "search": search, "scope": scope} result = list_blockers(session, page=page, page_size=50, **filters) summary = blocker_filter_summary(session, **filters) - return render(request, "blockers.html", {"result": result, "summary": summary, "auth": auth_data, "csrf_token": request.cookies.get("gti_csrf", ""), "client_filter": client or "", "installation_filter": installation or "", "field_filter": field or "", "blocker_type_filter": blocker_type or "", "status_filter": status or "OPEN", "age_filter": age or "", "search": search or "", "query_base": query_base(**filters)}) + return render(request, "blockers.html", {"result": result, "summary": summary, "auth": auth_data, "csrf_token": request.cookies.get("gti_csrf", ""), "client_filter": client or "", "installation_filter": installation or "", "field_filter": field or "", "blocker_type_filter": blocker_type or "", "reason_code_filter": reason_code or "", "status_filter": status, "scope_filter": scope, "age_filter": age or "", "search": search or "", "query_base": query_base(**filters), "field_query_base": query_base(**{key: value for key, value in filters.items() if key != "field"})}) finally: session.close() @routes.get("/blockers/{blocker_id}", response_class=HTMLResponse) @@ -516,10 +522,14 @@ def router(session_factory): session.close() @routes.get("/jobs", response_class=HTMLResponse) - def jobs(request: Request, page: int = 1, status: str | None = None, search: str | None = None, kind: str | None = None, job_id: int | None = None): + def jobs(request: Request, page: int = 1, status: str | None = None, search: str | None = None, kind: str | None = None, job_id: int | None = None, scope: str = "active"): session, auth_data, redirect = page_auth(request) if redirect: return redirect - try: return render(request, "jobs.html", {"result": list_jobs(session, page=page, page_size=50, status=status, search=search, kind=kind, job_id=job_id), "auth": auth_data, "status_filter": status or "", "search": search or "", "kind_filter": kind or "", "query_base": query_base(status=status, search=search, kind=kind, job_id=job_id)}) + try: + scope = scope if scope in ("active", "all") else "active" + status = status if status in ("PENDING", "RUNNING", "COMPLETED", "FAILED") else None + result = list_jobs(session, page=page, page_size=50, status=status, search=search, kind=kind, job_id=job_id, scope=scope) + return render(request, "jobs.html", {"result": result, "status_counts": jobs_status_summary(session, scope=scope), "auth": auth_data, "status_filter": status or "", "scope_filter": scope, "search": search or "", "kind_filter": kind or "", "job_id_filter": job_id, "query_base": query_base(status=status, search=search, kind=kind, job_id=job_id, scope=scope)}) finally: session.close() return routes diff --git a/central/static/control-panel.css b/central/static/control-panel.css index acd53ac..754feb1 100644 --- a/central/static/control-panel.css +++ b/central/static/control-panel.css @@ -148,3 +148,63 @@ td a { text-decoration: none; } .cards, .cards.compact, .split, .bar-grid { grid-template-columns: 1fr; } .filters label, .filters input, .filters select, .filters button { width: 100%; } } + +/* ---------- painel operacional: drill-down, filtros ativos, tabelas densas ---------- */ +a.metric-link { display: block; color: inherit; text-decoration: none; transition: border-color .15s, transform .15s; } +a.metric-link:hover { border-color: var(--cor-acento); transform: translateY(-1px); } +a.metric-link small { color: var(--cor-acento-texto); margin-top: 6px; font-weight: 600; } +a.metric-link:focus-visible, .coverage-item:focus-visible, .bar-link:focus-visible, .row-primary-link:focus-visible, .list-row:focus-visible, +.filter-clear:focus-visible, .navigable-row:focus-visible { outline: 3px solid var(--cor-acento); outline-offset: 2px; } +.coverage-item { display: block; color: inherit; text-decoration: none; border-left: 3px solid var(--cor-acento); padding-left: 12px; border-radius: 4px; } +.coverage-item:hover { background: var(--cor-superficie-alt); } +.coverage-item strong { display: block; font-size: 1.4rem; } +.coverage-item span { color: var(--cor-texto-secundario); font-size: 0.78rem; } +.coverage-status.error { color: var(--cor-perigo); } +.registry-summary { display: flex; flex-wrap: wrap; gap: 8px 20px; margin-top: 14px; font-size: 0.85rem; color: var(--cor-texto-secundario); } +a.bar-link { color: inherit; text-decoration: none; } +a.bar-link:hover { background: var(--cor-superficie-alt); } +.bar-row.muted { opacity: .65; } +.attention-row b, .list-row b { display: block; } +.attention-row { border-left: 3px solid var(--cor-aviso); padding-left: 12px; } +.filter-panel { padding: 14px 20px; } +.active-filters { margin: 12px 0 0; font-size: 0.82rem; color: var(--cor-texto-secundario); display: flex; flex-wrap: wrap; gap: 6px; align-items: center; } +.filter-chip { display: inline-flex; background: var(--cor-acento-suave); color: var(--cor-acento-texto); border-radius: 999px; padding: 2px 10px; font-weight: 600; } +.filter-clear { align-self: end; font-size: 0.85rem; } +.filter-search { flex: 1 1 260px; } +.filter-search input { width: 100%; } +.agents-table-wrap, .table-wrap { overflow-x: auto; } +.agents-table { width: 100%; table-layout: auto; } +.agents-table td small, .agents-table th small { display: block; } +.agents-table td { vertical-align: top; } +.navigable-row { cursor: pointer; } +.navigable-row:hover { background: var(--cor-superficie-alt); } +.historical-row { opacity: .7; } +.row-primary-link { color: inherit; text-decoration: none; display: block; } +.row-primary-link b { color: var(--cor-acento-texto); } +.warning-text { color: var(--cor-aviso); font-weight: 600; } +.compact-table td, .compact-table th { padding: 8px 10px; } +.job-error-summary { max-width: 46ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } +@media (max-width: 1100px) { .agents-table th:nth-child(5), .agents-table td:nth-child(5) { display: none; } } +@media (max-width: 640px) { + .agents-table thead { position: absolute; left: -9999px; } + .agents-table, .agents-table tbody, .agents-table tr, .agents-table td { display: block; width: 100%; } + .agents-table tr { border: 1px solid var(--cor-borda); border-radius: 10px; padding: 10px 12px; margin-bottom: 10px; } + .agents-table td { display: flex; justify-content: space-between; gap: 12px; border: 0; padding: 4px 0; } + .agents-table td::before { content: attr(data-label); color: var(--cor-texto-secundario); font-size: 0.75rem; font-weight: 600; } + .agents-table td:nth-child(5) { display: flex; } + .agents-table td:first-child { display: block; } + .agents-table td:first-child::before { content: none; } + .coverage { grid-template-columns: repeat(2, 1fr); } +} +@media (prefers-reduced-motion: reduce) { a.metric-link { transition: none; } a.metric-link:hover { transform: none; } } +.job-failed-row td:first-child { border-left: 3px solid var(--cor-perigo); } +.job-failed-row .badge { font-weight: 700; } +/* grid children must be allowed to shrink, otherwise a wide table/link inside a panel widens the whole page */ +.split { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } +.split > *, .cards > *, .coverage > * { min-width: 0; } +.list-row > span { min-width: 0; overflow-wrap: anywhere; } +code { overflow-wrap: anywhere; } +@media (max-width: 900px) { .split { grid-template-columns: minmax(0, 1fr); } } +@media (max-width: 768px) { .cards, .cards.compact { grid-template-columns: repeat(2, minmax(0, 1fr)); } } +@media (max-width: 520px) { .cards, .cards.compact, .bar-grid { grid-template-columns: minmax(0, 1fr); } } +@media (max-width: 520px) { .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; } .cards .metric { padding: 12px; } .cards .metric strong { font-size: 1.5rem; } } diff --git a/central/static/control-panel.js b/central/static/control-panel.js index d157cb7..2711710 100644 --- a/central/static/control-panel.js +++ b/central/static/control-panel.js @@ -1,3 +1,10 @@ document.addEventListener("DOMContentLoaded", () => { document.documentElement.classList.add("js"); + // Whole-row navigation for dense tables; the primary cell keeps a native link (keyboard/screen readers). + document.querySelectorAll("tr.navigable-row[data-href]").forEach((row) => { + row.addEventListener("click", (event) => { + if (event.target.closest("a, button, input, select, textarea, label")) return; + window.location.href = row.dataset.href; + }); + }); }); diff --git a/central/templates/agents.html b/central/templates/agents.html index 0d89968..3ef3c99 100644 --- a/central/templates/agents.html +++ b/central/templates/agents.html @@ -1,8 +1,31 @@ {% extends "base.html" %} {% block title %}Agents | GTI Fiscal{% endblock %} {% block content %} -
INVENTÁRIO OPERACIONAL
Identidade do cliente primeiro; IDs técnicos ficam no detalhe.
| Cliente | Nome de exibição | Hostname | Saúde técnica | Status fiscal | Cobertura fiscal | Produtos | Versão | Último heartbeat |
|---|---|---|---|---|---|---|---|---|
| {{ item.client or "Cliente não identificado" }} | {{ item.display_name or "Não informado" }} | {{ item.hostname or "Não informado" }} | Saúde técnica: {{ item.technical_health_label }} | Status fiscal: {{ item.fiscal_status_label }}{% if item.total_products is none %}Cobertura ainda não informada (nenhum snapshot recebido){% else %}{{ item.fiscally_complete }} completos · {{ item.safe_blocked }} bloqueados com segurança · {{ item.failed_products }} falhas · {{ item.unclassified_products }} não classificados{% if not item.coverage_invariant_pass %}Inconsistência: {{ item.coverage_overcount_products }} acima do total{% if item.coverage_invalid_counts %}; contagens negativas{% endif %}{% endif %}{% endif %} | {% if item.fiscal_completion_percent is none %}–Não informado{% else %}{{ "%.1f"|format(item.fiscal_completion_percent)|replace(".", ",") }}%{{ item.fiscally_complete }} / {{ item.total_products }} completos{% endif %} | {% if item.total_products is none %}Não informado{% else %}{{ item.total_products }}{{ item.failed_products }} falhas técnicas{% endif %} | {{ item.agent_version_display }} | {{ item.last_seen_display }}{{ item.last_seen_age }} |
| Nenhuma instalação encontrada. | ||||||||
INVENTÁRIO OPERACIONAL
Clientes e servidores ativos. Use Histórico para consultar instalações inativas ou revogadas.
Filtros ativos: {% if scope_filter=="all" %}Histórico e inativos{% endif %}{% if health_filter %}Saúde: {{ {"ONLINE":"Online","DEGRADED":"Atenção","OFFLINE":"Offline","FAILED":"Falhou","UNKNOWN":"Não informado"}[health_filter] }}{% endif %}{% if version_filter %}Agents desatualizados{% endif %}
{% endif %} +| Cliente / servidor | Saúde | Status fiscal | Cobertura | Produtos | Versão | Último contato |
|---|---|---|---|---|---|---|
| {{ item.client or "Cliente não identificado" }}{{ item.hostname or item.display_name or "Servidor não informado" }}{% if item.display_name and item.hostname and item.display_name!=item.hostname %}{{ item.display_name }}{% endif %}{% if not item.operational_active %}Histórico / inativo{% endif %} | +{{ item.technical_health_label }} | +{{ item.fiscal_status_label }}{% if item.total_products is not none %}{{ item.fiscally_complete }} completos · {{ item.safe_blocked }} bloqueados{% if item.failed_products %} · {{ item.failed_products }} falhas{% endif %}{% endif %}{% if not item.coverage_invariant_pass %}Inconsistência no snapshot{% endif %} | +{% if item.fiscal_completion_percent is none %}Não informada{% else %}{{ "%.1f"|format(item.fiscal_completion_percent)|replace(".", ",") }}%{% endif %} | +{{ item.total_products if item.total_products is not none else "Não informado" }} | +{{ item.agent_version or "Não informada" }}{% if item.version_drift=="OUTDATED" %}Atualização disponível{% endif %} | +{{ item.last_seen_age }}{{ item.last_seen_display }} | +
| Nenhuma instalação encontrada para os filtros atuais. Consultar histórico | ||||||
FILA FISCAL
Problemas fiscais separados de falhas técnicas.
| Cliente | Instalação | Produto | Campo | Tipo de blocker | Prioridade | Problema | Última ocorrência | Ação |
|---|---|---|---|---|---|---|---|---|
| {{ item.client or "Cliente não identificado" }} | {{ item.display_name or "-" }}{{ item.hostname or "hostname indisponível" }} | {{ item.product_code }}{{ item.description or "" }} | {{ item.field or "OTHER" }} | Fiscal: {{ item.blocker_type }} | Prioridade: {{ item.priority }} | {{ item.reason }}{{ item.age }} | {{ item.last_seen_display }}{{ item.retry_count }} tentativas | Ver detalhe |
| Nenhum blocker corresponde aos filtros. | ||||||||
FILA FISCAL
Problemas fiscais da operação ativa; histórico e resolvidos disponíveis no filtro.
Filtros ativos: {% if scope_filter=="all" %}Histórico e inativos{% endif %}{% if field_filter %}Campo: {{ field_filter }}{% endif %}{% if reason_code_filter %}Motivo: {{ reason_code_filter }}{% endif %}{% if status_filter!="OPEN" %}Status: {{ "Resolvidos" if status_filter=="RESOLVED" else "Todos" }}{% endif %}
{% endif %} +| Cliente / servidor | Produto | Campo / tipo | Prioridade | Problema | Última ocorrência | Ação |
|---|---|---|---|---|---|---|
| {{ item.client or "Cliente não identificado" }}{{ item.hostname or item.display_name or "Instalação não informada" }} | {{ item.product_code }}{{ item.description or "Sem descrição" }} | {{ item.field or "Outros" }}{{ item.blocker_type_label }} | {{ item.priority_label }} | {{ item.reason_label }}{{ item.age }} | {{ item.last_seen_display }}{{ item.retry_count }} tentativas | Ver detalhe |
| Nenhum blocker corresponde aos filtros. Consultar histórico | ||||||
OPERAÇÃO CENTRAL
Fila operacional baseada no último snapshot recebido.
OPERAÇÃO CENTRAL
Somente instalações ativas. Histórico permanece disponível nas listagens.
Cobertura reconciliada: total = completos + bloqueados com segurança + falhas técnicas + não classificados.
{% else %}Inconsistência de cobertura: {{ summary.coverage_overcount_products }} acima do total; {{ summary.coverage_invalid_snapshots }} snapshot(s) com contagens negativas.
{% endif %}Cobertura reconciliada: total = completos + bloqueados + falhas + não classificados.
{% else %}Inconsistência de cobertura: {{ summary.coverage_overcount_products }} acima do total; {{ summary.coverage_invalid_snapshots }} snapshot(s) com contagens negativas.
{% endif %} +FILA DE AÇÃO
Nenhum item requer atenção imediata.
{% endfor %}{{ summary.failed_jobs }} jobs aguardam investigação.
| Cliente | Instalação | Job ID / Tipo | Erro | Criado | Finalizado |
|---|---|---|---|---|---|
| {{ job.client or "-" }} | {{ job.display_name or "-" }}{{ job.hostname or "hostname indisponível" }} | {{ job.job_id }}{{ job.kind }} | {{ job.error or "Sem detalhe de erro" }} | {{ job.created_display }}{{ job.created_age }} | {{ job.finished_display }}{{ job.finished_age }} |
Nenhum job com falha.
{% endif %}Nenhum agent desatualizado.
{% endfor %}FILA DE AÇÃO
Nenhum item requer atenção imediata.
{% endfor %} +{{ summary.failed_jobs }} jobs aguardam investigação.
+| Cliente / servidor | Tipo / erro | Falhou | Ação |
|---|---|---|---|
| {{ job.client or "Cliente não identificado" }}{{ job.display_name or "Instalação" }} · {{ job.hostname or "Servidor não informado" }} | {{ job.kind_label }}{{ job.error or "Sem detalhe de erro" }} | {{ job.finished_display }}{{ job.finished_age }} | Ver job #{{ job.job_id }} |
Nenhum job com falha.
{% endif %} +Nenhum Agent desatualizado.
{% endfor %} +EXECUÇÕES
Acompanhe o que está em andamento e onde falhou.
| Cliente | Nome de exibição | Hostname | Job ID | Tipo | Status | Criado | Finalizado | Duração | Erro |
|---|---|---|---|---|---|---|---|---|---|
| {{ item.client or "Cliente não identificado" }} | {{ item.display_name or "-" }} | {{ item.hostname or "-" }} | #{{ item.job_id }}Agent técnico {{ item.agent_id }} | {{ item.kind }} | Status: {{ item.status }} | {{ item.created_display }} | {{ item.finished_display }} | {{ item.duration }} | {{ item.error or "-" }} |
| Nenhum job encontrado para os filtros atuais. | |||||||||
EXECUÇÕES
Acompanhe execuções ativas e investigue falhas sem perder o histórico.
Filtros ativos: {% if scope_filter=="all" %}Histórico e inativos{% endif %}{% if status_filter %}Status: {{ {"PENDING":"Pendente","RUNNING":"Executando","COMPLETED":"Concluído","FAILED":"Falhou"}[status_filter] }}{% endif %}{% if job_id_filter %}Job #{{ job_id_filter }}{% endif %}
{% endif %}{{ selected.client or "Cliente não identificado" }} · {{ selected.display_name or "Instalação" }} · {{ selected.hostname or "Servidor não informado" }}
{{ selected.kind_label }} {{ selected.status_label }}
Erro completo
{{ selected.error or "Sem erro registrado" }}Tipo técnico: {{ selected.kind }} · Status técnico: {{ selected.status }}| Cliente / servidor | Tipo / job | Status | Criado | Finalizado / duração | Erro / ação |
|---|---|---|---|---|---|
| {{ item.client or "Cliente não identificado" }}{{ item.display_name or "Instalação" }} · {{ item.hostname or "Servidor não informado" }} | {{ item.kind_label }}Job #{{ item.job_id }} | {{ item.status_label }} | {{ item.created_display }} | {{ item.finished_display }}{{ item.duration }} | {{ (item.error or "")[:100] }}{% if item.error and item.error|length > 100 %}…{% endif %}Ver job |
| Nenhum job encontrado. Consultar histórico | |||||