integration layer; merge work on indicators from forks with existing ingestion pipeline

This commit is contained in:
Roberto Treviño Cervantes
2026-10-23 00:00:14 -06:00
parent b0608fdc70
commit d9245fe9b7
194 changed files with 21773 additions and 848 deletions
+108
View File
@@ -0,0 +1,108 @@
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<ModelVersion> {
return this.http.post<ModelVersion>(`/governance/model-versions/${domain}/${scenario}/${version}/promote`, {});
}
retireModelVersion(domain: string, scenario: string, version: string): Observable<ModelVersion> {
return this.http.post<ModelVersion>(`/governance/model-versions/${domain}/${scenario}/${version}/retire`, {});
}
lineage(domain: string, scenario: string, version: string): Observable<ModelLineage> {
return this.http.get<ModelLineage>(`/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<Record<ReportField, string | null>>): Observable<ModelVersion> {
return this.http.patch<ModelVersion>(`/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<ModelVersion> {
const form = new FormData();
form.append('file', file);
form.append('kind', kind);
return this.http.post<ModelVersion>(`/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<CohortVersion> {
return this.http.post<CohortVersion>('/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<CohortExclusionsResponse> {
return this.http.get<CohortExclusionsResponse>(`/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<OcrRowsResponse> {
return this.http.get<OcrRowsResponse>(`/governance/ocr/${domain}/${entity}/rows`, {
params: toParams({ status, limit, offset }),
});
}
runOcr(domain: string, entity: string): Observable<OcrRunSummary> {
return this.http.post<OcrRunSummary>('/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<Blob> {
return this.http.get(`/governance/ocr/${domain}/${entity}/${id}/blob`, { responseType: 'blob' });
}
correctOcrRow(domain: string, entity: string, id: number, values: Record<string, string>, confianza?: number): Observable<Record<string, unknown>> {
return this.http.post<Record<string, unknown>>(`/governance/ocr/${domain}/${entity}/${id}/correct`, {
values,
...(confianza !== undefined ? { confianza } : {}),
});
}
}
function toParams(filters: Record<string, string | number | boolean | undefined>): 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;
}
+119
View File
@@ -0,0 +1,119 @@
/** Shapes of the `/governance/*` JSON contract. */
import { Domain } from '../../connect/api/api.types';
export type Environment = 'DEV' | 'PROD';
/** Derived from MLflow on every call, never stored. */
export type ModelStatus = 'desarrollo' | 'candidata' | 'produccion' | 'retirada';
/** Training, versioning and artifact storage live in MLflow; `name` is
* `<domain>-<scenario>` and `tags` carries our own promotion-relevant
* metadata (domain, scenario, environment, cohort_version_id, the three
* report URIs) — MLflow has no native columns for any of that. */
export interface ModelVersion {
name: string;
version: string;
run_id: string;
tags: Partial<Record<ReportField | 'domain' | 'scenario' | 'environment' | 'cohort_version_id' | 'retirada_en' | 'promovida_en', string>>;
metrics: Record<string, number>;
created_at: string;
status: ModelStatus;
}
export type ReportField = 'technical_validation_report_uri' | 'clinical_validation_report_uri' | 'interpretability_report_uri';
export interface PromotionCheck {
code: string;
requirement: string;
passed: boolean;
detail: string;
}
/** A logged artifact's path within the run, e.g. `ONNX_CHECKPOINT/model.onnx`
* — browsing/downloading the file itself goes through MLflow directly. */
export type ArtifactPath = string;
export interface ModelLineage {
model_version: ModelVersion;
cohort_version: CohortVersion | null;
cohort_exclusions: CohortExclusionCount[];
artifacts: ArtifactPath[];
predictions: number;
checks: PromotionCheck[];
}
export interface CohortScenario {
domain: string;
scenario: string;
criteria_version: string;
criteria: { code: string; reason_code: string; applicable_in_production: boolean }[];
}
export interface CohortVersion {
cohort_version_id: number;
domain: string;
scenario: string;
criteria_version: string;
version_hash: string;
source_table: string;
description: string | null;
n_source: number;
n_included: number;
created_at: string;
}
export interface CohortExclusionCount {
cohort_exclusion_count_id: number;
cohort_version_id: number;
criterion_code: string;
reason_code: string;
count: number;
created_at: string;
}
export interface CohortExclusionDetail {
cohort_exclusion_id: number;
cohort_version_id: number;
pac_id_hospital: string;
episodio_id: number | null;
criterion_code: string;
reason_code: string;
excluded_at: string;
}
export interface CohortExclusionsResponse {
counts: CohortExclusionCount[];
detail: CohortExclusionDetail[];
}
export interface OcrJob {
domain: Domain;
entity: string;
label: string;
result_fields: string[];
}
export type OcrRowStatus = 'review' | 'pending' | 'failed' | 'done' | 'all';
export interface OcrRow {
id: number;
pac_id_hospital: string | null;
values: Record<string, unknown>;
confianza: number | null;
estatus: string | null;
requiere_revision: boolean | null;
metodo_principal: string | null;
metodo_alternativo: string | null;
}
export interface OcrRowsResponse {
rows: OcrRow[];
total: number;
}
export interface OcrRunSummary {
processed: number;
skipped_already_done: number;
failed: number;
needs_review: number;
}