119 lines
4.5 KiB
TypeScript
119 lines
4.5 KiB
TypeScript
import { Component, computed, input, signal } from '@angular/core';
|
|
import { MatButtonToggleModule } from '@angular/material/button-toggle';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatSlideToggleModule } from '@angular/material/slide-toggle';
|
|
import { MatTableModule } from '@angular/material/table';
|
|
import { MatTooltipModule } from '@angular/material/tooltip';
|
|
import { Row, RowError } from '../../api/api.types';
|
|
import { ImportFlow } from '../../domain/import-flow';
|
|
import { TypeIconPipe } from '../shared/type-icon.pipe';
|
|
|
|
type GridView = 'target' | 'source';
|
|
|
|
interface GridColumn {
|
|
id: string;
|
|
title: string;
|
|
subtitle: string | null;
|
|
type: string;
|
|
/** Destination view: source column feeding it; source view: the column itself. */
|
|
sourceColumn: string | null;
|
|
defaultValue: string | null;
|
|
errorRate: number | null;
|
|
}
|
|
|
|
interface GridRow {
|
|
index: number;
|
|
values: Row;
|
|
hasErrors: boolean;
|
|
}
|
|
|
|
/**
|
|
* The sample rows of one flow, Power Query-style: "Destino" shows them as
|
|
* they will land in the database (destination field names, defaults
|
|
* applied, cells that failed validation highlighted with the reason, a
|
|
* quality bar per field), "Origen" shows the raw source columns.
|
|
*/
|
|
@Component({
|
|
selector: 'app-data-grid',
|
|
imports: [MatButtonToggleModule, MatIconModule, MatSlideToggleModule, MatTableModule, MatTooltipModule, TypeIconPipe],
|
|
templateUrl: './data-grid.html',
|
|
styleUrl: './data-grid.scss',
|
|
})
|
|
export class DataGrid {
|
|
readonly flow = input.required<ImportFlow>();
|
|
|
|
readonly view = signal<GridView>('target');
|
|
readonly onlyErrors = signal(false);
|
|
|
|
readonly columns = computed<GridColumn[]>(() => {
|
|
const flow = this.flow();
|
|
if (this.view() === 'source') {
|
|
const bySource = flow.mapping.bySource();
|
|
return flow.sourceColumns().map((column) => ({
|
|
id: column.name,
|
|
title: column.name,
|
|
subtitle: bySource.get(column.name) ? `→ ${bySource.get(column.name)!.field}` : null,
|
|
type: column.inferred_type,
|
|
sourceColumn: column.name,
|
|
defaultValue: null,
|
|
errorRate: null,
|
|
}));
|
|
}
|
|
const byField = flow.mapping.byField();
|
|
const defaults = flow.defaults();
|
|
const quality = flow.quality();
|
|
return flow.target.fields
|
|
.filter((field) => byField.has(field.name) || defaults[field.name])
|
|
.map((field) => ({
|
|
id: field.name,
|
|
title: field.name,
|
|
subtitle: byField.get(field.name) ? `← ${byField.get(field.name)!.source}` : `= ${defaults[field.name]}`,
|
|
type: field.type,
|
|
sourceColumn: byField.get(field.name)?.source ?? null,
|
|
defaultValue: defaults[field.name] ?? null,
|
|
errorRate: quality.get(field.name)?.errorRate ?? null,
|
|
}));
|
|
});
|
|
|
|
readonly displayedColumns = computed(() => ['__row', ...this.columns().map((column) => column.id)]);
|
|
|
|
readonly rows = computed<GridRow[]>(() => {
|
|
const flow = this.flow();
|
|
const errors = flow.errorsByCell();
|
|
const erroredRows = new Set([...errors.keys()].map((key) => Number(key.split(':')[0])));
|
|
const rows = (flow.preview()?.sample_rows ?? []).map((values, index) => ({ index, values, hasErrors: erroredRows.has(index) }));
|
|
return this.onlyErrors() && this.view() === 'target' ? rows.filter((row) => row.hasErrors) : rows;
|
|
});
|
|
|
|
readonly shownOf = computed(() => {
|
|
const flow = this.flow();
|
|
return { shown: flow.preview()?.sample_rows.length ?? 0, total: flow.totalRows() };
|
|
});
|
|
|
|
cell(row: GridRow, column: GridColumn): { text: string; isDefault: boolean; isEmpty: boolean } {
|
|
const raw = column.sourceColumn ? row.values[column.sourceColumn] : null;
|
|
const blank = raw === null || raw === undefined || String(raw).trim() === '';
|
|
if (blank && column.defaultValue) return { text: column.defaultValue, isDefault: true, isEmpty: false };
|
|
return { text: blank ? 'null' : displayValue(raw), isDefault: false, isEmpty: blank };
|
|
}
|
|
|
|
errors(row: GridRow, column: GridColumn): RowError[] {
|
|
if (this.view() !== 'target') return [];
|
|
return this.flow().errorsByCell().get(`${row.index}:${column.id}`) ?? [];
|
|
}
|
|
|
|
errorText(errors: RowError[]): string {
|
|
return errors.map((error) => error.message).join('\n');
|
|
}
|
|
|
|
qualityPercent(rate: number): number {
|
|
return Math.round((1 - rate) * 100);
|
|
}
|
|
}
|
|
|
|
/** Excel/SQL dates arrive as midnight timestamps ("2024-01-10T00:00:00"); show them as plain dates. */
|
|
function displayValue(value: unknown): string {
|
|
const text = String(value);
|
|
return /^\d{4}-\d{2}-\d{2}T00:00:00$/.test(text) ? text.slice(0, 10) : text;
|
|
}
|