"""Truthful monitoring and cost coverage for directory resources. Coverage is deliberately separate from governance findings. A DNS record does not have CPU metrics, while a running VM whose provider supports metrics but produced no samples is an actionable gap. Likewise, subscription-level billing is useful evidence but must not be presented as exact resource cost. """ from __future__ import annotations from dataclasses import dataclass from datetime import UTC, date, datetime, timedelta from sqlalchemy import func, select from sqlalchemy.ext.asyncio import AsyncSession from app.domains.cmdb.models import ( CmdbCloudResource, CmdbCloudScope, CmdbCostFact, CmdbServerMetricsDaily, ) from app.integrations.models import Integration _METRICS_PROVIDERS = frozenset({"aws", "aliyun", "alicloud_intl", "azure", "gcp"}) _MONITORING_NOT_APPLICABLE = frozenset({"cloud_scope", "dns_record", "domain", "iam_user"}) _COST_NOT_APPLICABLE = frozenset({"dns_record", "domain", "iam_user"}) @dataclass(frozen=True) class ResourceCoverage: monitoring_status: str monitoring_latest_at: str | None cost_status: str cost_source: str | None async def resolve_resource_coverage( db: AsyncSession, resources: list[CmdbCloudResource], *, monitoring_days: int = 30, cost_days: int = 31, ) -> dict[str, ResourceCoverage]: """Resolve one bounded API page without N+1 queries.""" if not resources: return {} now = datetime.now(UTC) monitoring_cutoff = now.date() - timedelta(days=max(1, monitoring_days) - 1) cost_cutoff = now.date() - timedelta(days=max(1, cost_days) - 1) server_ids = [ str(row.source_id) for row in resources if row.source_table == "cmdb_servers" and row.source_id ] latest_metric: dict[str, date] = {} if server_ids: metric_rows = (await db.execute(select( CmdbServerMetricsDaily.server_id, func.max(CmdbServerMetricsDaily.metric_date), ).where( CmdbServerMetricsDaily.server_id.in_(server_ids), CmdbServerMetricsDaily.metric_date >= monitoring_cutoff, ).group_by(CmdbServerMetricsDaily.server_id))).all() latest_metric = {str(server_id): day for server_id, day in metric_rows} integration_ids = {str(row.integration_id) for row in resources} integration_rows = (await db.execute(select( Integration.id, Integration.enabled, ).where(Integration.id.in_(integration_ids)))).all() disabled_integrations = { str(integration_id) for integration_id, enabled in integration_rows if not enabled } resource_ids = {str(row.provider_resource_id) for row in resources if row.provider_resource_id} exact_cost_keys: set[tuple[str, str]] = set() if resource_ids: exact_rows = (await db.execute(select( CmdbCostFact.integration_id, CmdbCostFact.resource_id, ).where( CmdbCostFact.integration_id.in_(integration_ids), CmdbCostFact.resource_id.in_(resource_ids), CmdbCostFact.date >= cost_cutoff, ).distinct())).all() exact_cost_keys = {(str(integration_id), str(resource_id)) for integration_id, resource_id in exact_rows} scope_external_ids = {str(row.scope_external_id) for row in resources if row.scope_external_id} scope_key_to_id: dict[tuple[str, str], str] = {} if scope_external_ids: scope_rows = (await db.execute(select( CmdbCloudScope.integration_id, CmdbCloudScope.external_id, CmdbCloudScope.id, ).where( CmdbCloudScope.integration_id.in_(integration_ids), CmdbCloudScope.external_id.in_(scope_external_ids), CmdbCloudScope.deleted_at.is_(None), ))).all() scope_key_to_id = { (str(integration_id), str(external_id)): str(scope_id) for integration_id, external_id, scope_id in scope_rows } scope_ids = set(scope_key_to_id.values()) scopes_with_cost: set[str] = set() if scope_ids: scopes_with_cost = { str(value) for value in (await db.scalars(select(CmdbCostFact.scope_id).where( CmdbCostFact.scope_id.in_(scope_ids), CmdbCostFact.date >= cost_cutoff, ).distinct())).all() if value } integrations_with_cost = { str(value) for value in (await db.scalars(select(CmdbCostFact.integration_id).where( CmdbCostFact.integration_id.in_(integration_ids), CmdbCostFact.date >= cost_cutoff, ).distinct())).all() } result: dict[str, ResourceCoverage] = {} for resource in resources: if resource.resource_type in _MONITORING_NOT_APPLICABLE: monitoring_status = "not_applicable" latest_at = None elif resource.resource_type != "server": monitoring_status = "not_supported" latest_at = None elif resource.provider not in _METRICS_PROVIDERS: monitoring_status = "not_supported" latest_at = None elif str(resource.lifecycle_status or "").casefold() in { "stopped", "terminated", "deleted", "deallocated", }: monitoring_status = "not_expected" latest_at = None else: latest = latest_metric.get(str(resource.source_id)) monitoring_status = "covered" if latest else "missing" latest_at = latest.isoformat() if latest else None integration_id = str(resource.integration_id) provider_resource_id = str(resource.provider_resource_id) scope_id = scope_key_to_id.get((integration_id, str(resource.scope_external_id or ""))) if (integration_id, provider_resource_id) in exact_cost_keys: cost_status, cost_source = "covered", "resource" elif resource.resource_type in _COST_NOT_APPLICABLE: cost_status, cost_source = "not_applicable", None elif scope_id and scope_id in scopes_with_cost: cost_status, cost_source = "covered", "scope" elif integration_id in integrations_with_cost: cost_status, cost_source = "shared", "integration" elif integration_id in disabled_integrations: cost_status, cost_source = "not_expected", None else: cost_status, cost_source = "not_available", None result[str(resource.id)] = ResourceCoverage( monitoring_status=monitoring_status, monitoring_latest_at=latest_at, cost_status=cost_status, cost_source=cost_source, ) return result