fix: close remaining task 2 review findings

This commit is contained in:
gti
2026-09-22 18:23:38 -03:00
parent 041257d254
commit 2ee271ca80
5 changed files with 73 additions and 8 deletions
+7 -1
View File
@@ -8,7 +8,13 @@ from .operational_state import blocker_fingerprint
def upsert_blocker(session, tenant_id: int, payload: dict, *, installation_id: int | None = None) -> BlockedProduct:
fingerprint = payload.get("blocker_fingerprint") or blocker_fingerprint(payload.get("tenant", str(tenant_id)), payload.get("company", ""), payload.get("product_code", ""), payload.get("field", ""), payload.get("reason_code", ""), payload.get("input_fingerprint", ""))
supplied_fingerprint = payload.get("blocker_fingerprint")
if installation_id is None:
fingerprint = supplied_fingerprint or blocker_fingerprint(payload.get("tenant", str(tenant_id)), payload.get("company", ""), payload.get("product_code", ""), payload.get("field", ""), payload.get("reason_code", ""), payload.get("input_fingerprint", ""))
elif supplied_fingerprint:
fingerprint = blocker_fingerprint(supplied_fingerprint, "", "", "", "", "", installation_id=installation_id)
else:
fingerprint = blocker_fingerprint(payload.get("tenant", str(tenant_id)), payload.get("company", ""), payload.get("product_code", ""), payload.get("field", ""), payload.get("reason_code", ""), payload.get("input_fingerprint", ""), installation_id=installation_id)
row = session.scalar(select(BlockedProduct).where(BlockedProduct.blocker_fingerprint == fingerprint))
now = datetime.now(timezone.utc)
if row is None:
+10 -5
View File
@@ -3,8 +3,9 @@
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from decimal import Decimal, ROUND_HALF_UP
from sqlalchemy import and_, case, func, or_, select
from sqlalchemy import Numeric, and_, case, cast, func, or_, select
from .config import settings
from .events import list_events
@@ -46,7 +47,9 @@ def _health_expression(now):
def _fiscal_completion_expression():
total = func.coalesce(AgentSnapshot.total_products, 0)
complete = func.coalesce(AgentSnapshot.fiscally_complete, 0)
raw = case((total > 0, complete * 100.0 / total), else_=func.coalesce(AgentSnapshot.coverage_percent, 0))
numeric_total = cast(total, Numeric(18, 6))
numeric_complete = cast(complete * 100, Numeric(18, 6))
raw = case((total > 0, numeric_complete / numeric_total), else_=cast(func.coalesce(AgentSnapshot.coverage_percent, 0), Numeric(18, 6)))
return func.round(case((raw < 0, 0), (raw > 100, 100), else_=raw), 1)
@@ -54,8 +57,10 @@ def _fiscal_completion(snapshot) -> float:
total = int(getattr(snapshot, "total_products", 0) or 0)
complete = int(getattr(snapshot, "fiscally_complete", 0) or 0)
if total:
return round(max(0, min(total, complete)) * 100 / total, 1)
return round(max(0, min(100, float(getattr(snapshot, "coverage_percent", 0) or 0))), 1)
value = Decimal(max(0, min(total, complete)) * 100) / Decimal(total)
else:
value = Decimal(str(max(0, min(100, float(getattr(snapshot, "coverage_percent", 0) or 0)))))
return float(value.quantize(Decimal("0.1"), rounding=ROUND_HALF_UP))
def _fiscal_status(snapshot) -> str:
@@ -357,7 +362,7 @@ 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, BlockedProduct.field.in_(BLOCKER_FIELDS[:-1])).order_by(BlockedProduct.last_seen_at.desc(), BlockedProduct.id.desc()).limit(ATTENTION_CANDIDATE_LIMIT)
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()
attention = []
+3 -1
View File
@@ -10,8 +10,10 @@ STATES = {
}
def blocker_fingerprint(tenant: str, company: str, product_code: str, field: str, reason_code: str, input_fingerprint: str) -> str:
def blocker_fingerprint(tenant: str, company: str, product_code: str, field: str, reason_code: str, input_fingerprint: str, installation_id: str | int | None = None) -> str:
raw = {"tenant": tenant, "company": company, "product_code": product_code, "field": field, "reason_code": reason_code, "input_fingerprint": input_fingerprint}
if installation_id is not None:
raw["installation_id"] = str(installation_id)
return hashlib.sha256(json.dumps(raw, sort_keys=True, separators=(",", ":")).encode()).hexdigest()