import { HttpClient, HttpParams } from '@angular/common/http'; import { Injectable, inject } from '@angular/core'; import { Observable } from 'rxjs'; import { CohortExclusionsResponse, CohortScenario, CohortVersion, ModelLineage, ModelVersion, PromotionCheck, ReportField, OcrJob, OcrRowStatus, OcrRowsResponse, OcrRunSummary, } from './governance.types'; /** `/governance/*`: model and cohort versions, artifacts, and the OCR review queue. */ @Injectable({ providedIn: 'root' }) export class GovernanceApi { private readonly http = inject(HttpClient); /** `scenario` needs `domain`; `domain` alone matches every scenario of it; * neither matches everything. */ listModelVersions(filters: { domain?: string; scenario?: string } = {}): Observable<{ model_versions: ModelVersion[] }> { return this.http.get<{ model_versions: ModelVersion[] }>('/governance/model-versions', { params: toParams(filters) }); } /** 409 with `checks` when the version doesn't meet the promotion requirements. */ promoteModelVersion(domain: string, scenario: string, version: string): Observable { return this.http.post(`/governance/model-versions/${domain}/${scenario}/${version}/promote`, {}); } retireModelVersion(domain: string, scenario: string, version: string): Observable { return this.http.post(`/governance/model-versions/${domain}/${scenario}/${version}/retire`, {}); } lineage(domain: string, scenario: string, version: string): Observable { return this.http.get(`/governance/model-versions/${domain}/${scenario}/${version}/lineage`); } checks(domain: string, scenario: string, version: string): Observable<{ ready: boolean; checks: PromotionCheck[] }> { return this.http.get<{ ready: boolean; checks: PromotionCheck[] }>(`/governance/model-versions/${domain}/${scenario}/${version}/checks`); } attachReports(domain: string, scenario: string, version: string, reports: Partial>): Observable { return this.http.patch(`/governance/model-versions/${domain}/${scenario}/${version}/reports`, reports); } /** Logs the file to the version's MLflow run. Browsing/downloading a * logged artifact goes through the MLflow tracking server directly. */ uploadArtifact(domain: string, scenario: string, version: string, kind: string, file: File): Observable { const form = new FormData(); form.append('file', file); form.append('kind', kind); return this.http.post(`/governance/model-versions/${domain}/${scenario}/${version}/artifacts`, form); } listCohortScenarios(): Observable<{ scenarios: CohortScenario[] }> { return this.http.get<{ scenarios: CohortScenario[] }>('/governance/cohort-scenarios'); } buildCohortVersion(domain: string, scenario: string, description?: string): Observable { return this.http.post('/governance/cohort-versions', { domain, scenario, ...(description ? { description } : {}) }); } listCohortVersions(filters: { domain?: string; scenario?: string } = {}): Observable<{ cohort_versions: CohortVersion[] }> { return this.http.get<{ cohort_versions: CohortVersion[] }>('/governance/cohort-versions', { params: toParams(filters) }); } getCohortExclusions(cohortVersionId: number): Observable { return this.http.get(`/governance/cohort-versions/${cohortVersionId}/exclusions`); } listOcrJobs(): Observable<{ jobs: OcrJob[] }> { return this.http.get<{ jobs: OcrJob[] }>('/governance/ocr/jobs'); } listOcrRows(domain: string, entity: string, status: OcrRowStatus, limit = 100, offset = 0): Observable { return this.http.get(`/governance/ocr/${domain}/${entity}/rows`, { params: toParams({ status, limit, offset }), }); } runOcr(domain: string, entity: string): Observable { return this.http.post('/governance/ocr/run', { domain, entity }); } /** The stored PDF, wrapped by the caller in a `File` for the page viewer. */ getOcrBlob(domain: string, entity: string, id: number): Observable { return this.http.get(`/governance/ocr/${domain}/${entity}/${id}/blob`, { responseType: 'blob' }); } correctOcrRow(domain: string, entity: string, id: number, values: Record, confianza?: number): Observable> { return this.http.post>(`/governance/ocr/${domain}/${entity}/${id}/correct`, { values, ...(confianza !== undefined ? { confianza } : {}), }); } } function toParams(filters: Record): HttpParams { let params = new HttpParams(); for (const [key, value] of Object.entries(filters)) { if (value !== undefined && value !== null && value !== '') params = params.set(key, String(value)); } return params; }