package httpapi import ( "context" "knowledge-governance/internal/dobotaiops" "knowledge-governance/internal/qaopsmirror" "net/http" "time" ) type departmentStatisticsReader interface { ReadDepartmentStatistics(context.Context, qaopsmirror.DepartmentStatisticsQuery, time.Time) (qaopsmirror.DepartmentStatisticsResult, error) } func qaDepartmentStatistics(w http.ResponseWriter, r *http.Request, c Config) { if !authorizeQAOps(w, r) || !authorizeQAIntent(w, r, "qa-department-statistics") { return } var q qaopsmirror.DepartmentStatisticsQuery if r.URL.RawQuery != "" || decodeQualityJSON(w, r, &q, "period") != nil { qaOpsError(w, dobotaiops.ErrInvalidQuery) return } now := c.Now() if _, _, e := q.Bounds(now); e != nil { qaOpsError(w, e) return } reader, ok := c.QAOps.(departmentStatisticsReader) if !ok || c.QAOpsTenantKey == "" { qaOpsError(w, dobotaiops.ErrUnavailable) return } data, e := reader.ReadDepartmentStatistics(r.Context(), q, now) if e != nil { qaOpsError(w, e) return } status := http.StatusOK if data.State == "pending" { status = http.StatusAccepted w.Header().Set("Retry-After", "1") } writeJSON(w, status, map[string]any{"data": data}) }