package handler import ( "context" "encoding/json" "fmt" "net/http" "sort" "time" "github.com/shankar0123/certctl/internal/api/middleware" ) // MetricsService defines the service interface for metrics collection. type MetricsService interface { GetDashboardSummary(ctx context.Context) (interface{}, error) } // CounterSnapshotter is the minimum surface MetricsHandler consumes // from a counter table for the Prometheus exposer. The OCSPCounters // type in internal/service satisfies this; future per-area counter // tabs (CRL, cert-export, EST, SCEP, Intune) plug in the same way. // // Production hardening II Phase 8. type CounterSnapshotter interface { Snapshot() map[string]uint64 } // DeploySnapshotEntry is the per-target-type tuple emitted by the // deploy package's counter table. Avoids importing the service // package's DeploySnapshot directly so the handler stays // dependency-light (the interface uses primitives only). // // Phase 10 of the deploy-hardening I master bundle. type DeploySnapshotEntry struct { TargetType string AttemptsSuccess uint64 AttemptsFailure uint64 ValidateFailures uint64 ReloadFailures uint64 PostVerifyFails uint64 RollbackRestored uint64 RollbackAlsoFail uint64 IdempotentSkips uint64 } // DeployCounterSnapshotter is the surface MetricsHandler consumes // for the per-target-type deploy counters. The DeployCounters type // in internal/service satisfies this via an adapter. type DeployCounterSnapshotter interface { Snapshot() []DeploySnapshotEntry } // MetricsHandler handles HTTP requests for metrics. // Supports both JSON format (GET /api/v1/metrics) and Prometheus exposition format // (GET /api/v1/metrics/prometheus) for integration with Prometheus, Grafana, Datadog, etc. type MetricsHandler struct { svc MetricsService serverStarted time.Time // Production hardening II Phase 8 — per-area counter snapshotters. // nil values omit the corresponding metric block; cmd/server/main.go // wires the instances at startup. The naming convention is // certctl__