integration layer; merge work on indicators from forks with existing ingestion pipeline
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
import { Signal, signal } from '@angular/core';
|
||||
import { Observable, catchError, map, of, tap } from 'rxjs';
|
||||
import { PreviewResponse, SourceLocator, apiErrorMessage } from '../api/api.types';
|
||||
import { SourceGateway } from '../api/source-gateway';
|
||||
|
||||
let sequence = 0;
|
||||
const nextId = (prefix: string) => `${prefix}-${++sequence}`;
|
||||
|
||||
export type ConnectionState = 'loading' | 'ready' | 'error';
|
||||
|
||||
/**
|
||||
* A place data comes from (a file, a database, a set of documents): the
|
||||
* "Conexiones" of the workspace. It exposes `SourceTable`s (Excel sheets,
|
||||
* database tables, a PDF's tables), which are what gets mapped.
|
||||
*
|
||||
* Each kind is one subclass (`file-source.ts`, `sql-source.ts`,
|
||||
* `pdf-source.ts`, `eeg-source.ts`, `document-source.ts`) that owns how its
|
||||
* tables are discovered and previewed; nothing else switches on the kind.
|
||||
* New kinds are wired to file extensions in `connection-registry.ts`.
|
||||
*/
|
||||
export abstract class DataConnection {
|
||||
readonly id = nextId('conn');
|
||||
abstract readonly icon: string;
|
||||
abstract readonly typeLabel: string;
|
||||
|
||||
private readonly _tables = signal<SourceTable[]>([]);
|
||||
private readonly _state = signal<ConnectionState>('loading');
|
||||
private readonly _error = signal<string | null>(null);
|
||||
|
||||
readonly tables: Signal<SourceTable[]> = this._tables.asReadonly();
|
||||
readonly state: Signal<ConnectionState> = this._state.asReadonly();
|
||||
readonly error: Signal<string | null> = this._error.asReadonly();
|
||||
|
||||
constructor(readonly label: string) {}
|
||||
|
||||
/** A connection that can only ever have one table (a CSV) is shown flat, without a table level. */
|
||||
get singleTable(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Discovers this connection's tables. Does not throw; failures are reported in `state`/`error`. */
|
||||
load(gateway: SourceGateway): Observable<void> {
|
||||
this._state.set('loading');
|
||||
this._error.set(null);
|
||||
return this.discover(gateway).pipe(
|
||||
tap((tables) => {
|
||||
this._tables.set(tables);
|
||||
this._state.set('ready');
|
||||
}),
|
||||
map(() => undefined),
|
||||
catchError((error) => {
|
||||
this._error.set(apiErrorMessage(error, `No se pudo abrir ${this.label}.`));
|
||||
this._state.set('error');
|
||||
return of(undefined);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
addTable(table: SourceTable): void {
|
||||
this._tables.update((tables) => [...tables, table]);
|
||||
}
|
||||
|
||||
removeTable(table: SourceTable): void {
|
||||
this._tables.update((tables) => tables.filter((existing) => existing !== table));
|
||||
}
|
||||
|
||||
protected abstract discover(gateway: SourceGateway): Observable<SourceTable[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
* One tabular source inside a connection, the unit dragged onto the canvas
|
||||
* to create an import flow. Previews itself against a target entity and
|
||||
* describes itself to the backend (`SourceLocator`); knows nothing about
|
||||
* mapping.
|
||||
*/
|
||||
export abstract class SourceTable {
|
||||
readonly id = nextId('table');
|
||||
abstract readonly icon: string;
|
||||
/** What kind of table this is, for the UI: "Hoja", "Tabla", "Consulta SQL"... */
|
||||
abstract readonly kindLabel: string;
|
||||
|
||||
constructor(
|
||||
readonly connection: DataConnection,
|
||||
readonly name: string,
|
||||
) {}
|
||||
|
||||
get label(): string {
|
||||
return this.connection.singleTable ? this.connection.label : `${this.connection.label} › ${this.name}`;
|
||||
}
|
||||
|
||||
/** Whether the user created this table (a custom query, drawn regions) and may remove it. */
|
||||
get removable(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
abstract preview(gateway: SourceGateway, target: string | null): Observable<PreviewResponse>;
|
||||
|
||||
/** `target`: the entity the rows go to, for sources whose reading depends on it. */
|
||||
abstract locator(target?: string): Promise<SourceLocator>;
|
||||
}
|
||||
|
||||
/** Lazily computed base64 of a file, shared by every table of a connection. */
|
||||
export class EncodedFile {
|
||||
private encoded: Promise<string> | null = null;
|
||||
|
||||
constructor(
|
||||
readonly file: File,
|
||||
private readonly encode: (file: File) => Promise<string>,
|
||||
) {}
|
||||
|
||||
base64(): Promise<string> {
|
||||
this.encoded ??= this.encode(this.file);
|
||||
return this.encoded;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Encoded `{name, file_base64}` entries, the shape multi-file payloads use. */
|
||||
export async function encodeFiles(files: EncodedFile[]): Promise<{ name: string; file_base64: string }[]> {
|
||||
return Promise.all(files.map(async (encoded) => ({ name: encoded.file.name, file_base64: await encoded.base64() })));
|
||||
}
|
||||
Reference in New Issue
Block a user