Files
WebApp/src/app/governance/components/tea-eeg-panel/baselines-step/baselines-step.ts
T

108 lines
4.3 KiB
TypeScript

import { HttpClient, HttpEventType } from '@angular/common/http';
import { Component, DestroyRef, computed, inject, signal } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { MatButtonModule } from '@angular/material/button';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatIconModule } from '@angular/material/icon';
import { MatProgressBarModule } from '@angular/material/progress-bar';
import { MatTableModule } from '@angular/material/table';
import { apiErrorMessage } from '../../../../connect/api/api.types';
import { TeaEegApi } from '../../../api/tea-eeg.api';
import { BaselineImport } from '../../../api/tea-eeg.types';
/** Hex of a digest, lowercase. */
export function sha256Hex(digest: ArrayBuffer): string {
return Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, '0')).join('');
}
/**
* The baselines step: the promoted baselines download as a zip from the export route (optionally without
* the production versions), and the import route reads a bundle back, showing its sha256 and the imported
* scenario/version pairs. After exporting, the sha256 can be recomputed from the downloaded bytes and
* compared with the response's `X-Baselines-Sha256` header.
*/
@Component({
selector: 'app-baselines-step',
imports: [MatButtonModule, MatCheckboxModule, MatIconModule, MatProgressBarModule, MatTableModule],
templateUrl: './baselines-step.html',
styleUrl: './baselines-step.scss',
})
export class BaselinesStep {
private readonly tea = inject(TeaEegApi);
private readonly http = inject(HttpClient);
private readonly destroyRef = inject(DestroyRef);
/** Checked: the zip includes the production versions; unchecked asks for `production=0`. */
readonly includeProduction = signal(true);
readonly exportUrl = computed(() => this.tea.baselinesExportUrl('prod', this.includeProduction() ? undefined : false));
readonly exportClicked = signal(false);
readonly hashing = signal(false);
readonly hash = signal<{ sha256: string; matches: boolean | null } | null>(null);
readonly hashError = signal<string | null>(null);
readonly importFile = signal<File | null>(null);
readonly importing = signal(false);
readonly importResult = signal<BaselineImport | null>(null);
readonly importError = signal<string | null>(null);
readonly displayedColumns = ['scenario', 'version'];
onExportClicked(): void {
this.exportClicked.set(true);
}
/** Downloads the same zip and hashes it, to compare against the header the export answers with. */
computeHash(): void {
this.hashing.set(true);
this.hashError.set(null);
this.http
.get(this.exportUrl(), { responseType: 'blob', observe: 'response' })
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe({
next: (response) => void this.digest(response.body!, response.headers.get('X-Baselines-Sha256')),
error: (error) => {
this.hashing.set(false);
this.hashError.set(apiErrorMessage(error, 'No se pudo descargar el paquete.'));
},
});
}
private async digest(blob: Blob, header: string | null): Promise<void> {
try {
const digest = await crypto.subtle.digest('SHA-256', await blob.arrayBuffer());
this.hash.set({ sha256: sha256Hex(digest), matches: header ? header === sha256Hex(digest) : null });
} catch {
this.hashError.set('No se pudo calcular la huella en este navegador.');
}
this.hashing.set(false);
}
onImportPicked(event: Event): void {
const input = event.target as HTMLInputElement;
this.importFile.set(input.files?.[0] ?? null);
this.importResult.set(null);
this.importError.set(null);
}
import(): void {
const file = this.importFile();
if (!file || this.importing()) return;
this.importing.set(true);
this.importError.set(null);
this.importResult.set(null);
this.tea
.importBaselines(file)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe({
next: (event) => {
if (event.type === HttpEventType.Response) {
this.importing.set(false);
this.importResult.set(event.body!);
}
},
error: (error) => {
this.importing.set(false);
this.importError.set(apiErrorMessage(error, 'No se pudo importar el paquete.'));
},
});
}
}