import { HttpClient, HttpParams } from '@angular/common/http'; import { Injectable, inject } from '@angular/core'; import { Observable } from 'rxjs'; import { CohortBuildFile, Experiment, ExperimentDetail, ExperimentRequest, ModelLineage, ModelVersion, PromotionCheck, ReportField, OcrJob, OcrRowStatus, OcrRowsResponse, OcrRunSummary, } from './governance.types'; /** `/governance/*`: model versions and their files, notebooks run on a cohort build, 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`, {}); } /** Exports the scenario's latest dbt cohort build, trains a PROD version and tries to promote it, the same * one-shot pipeline the `entrenar` compose profile runs. 409 when it trained but needs its reports attached * before it can be promoted; 503 with no dbt manifest configured or an operational failure. */ trainModelVersion(domain: string, scenario: string): Observable<{ output: string }> { return this.http.post<{ output: string }>(`/governance/model-versions/${domain}/${scenario}/train`, {}); } 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); } /** Where a file the version's run stored downloads from; a folder comes as a zip. */ artifactUrl(domain: string, scenario: string, version: string, path: string): string { return `/governance/model-versions/${domain}/${scenario}/${version}/artifacts/${encodePath(path)}`; } /** The domain's cohort builds that have their file. */ cohortFiles(domain: string): Observable<{ builds: CohortBuildFile[] }> { return this.http.get<{ builds: CohortBuildFile[] }>('/governance/cohort-files', { params: toParams({ domain }) }); } listExperiments(filters: { domain?: string; scenario?: string } = {}): Observable<{ experiments: Experiment[] }> { return this.http.get<{ experiments: Experiment[] }>('/governance/experiments', { params: toParams(filters) }); } /** Starts the notebook on the build in the background; answers with the run while it is still `en_curso`. */ runExperiment(request: ExperimentRequest): Observable { const form = new FormData(); form.append('file', request.file); form.append('domain', request.domain); form.append('scenario', request.scenario); form.append('url', request.build.url); form.append('source', request.build.source); form.append('build', String(request.build.build)); if (request.version) form.append('version', request.version); return this.http.post('/governance/experiments', form); } getExperiment(runId: string): Observable { return this.http.get(`/governance/experiments/${runId}`); } /** The rendered page, served sandboxed; opened in its own tab. */ experimentPageUrl(runId: string): string { return `/governance/experiments/${runId}/page`; } experimentArtifactUrl(runId: string, path: string): string { return `/governance/experiments/${runId}/artifacts/${encodePath(path)}`; } 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 encodePath(path: string): string { return path.split('/').map(encodeURIComponent).join('/'); } 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; }