Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/api/src/export-progress.ts
T

36 lines
1.4 KiB
TypeScript

import { getSharedStore } from "./shared-store.js";
export interface ExportProgressSnapshot {
processedRows: number;
totalEstimate: number | null;
updatedAt: string | null;
}
/**
* Reads the latest export progress published by the worker job into the
* shared store. The worker writes `export:progress:<jobId>` every N rows
* (default 5_000) with a 1h TTL; the API surfaces that snapshot to the
* front-end so users can poll for a row-count counter without going to
* Postgres.
*
* Returns `null` when no progress entry exists (job not started, expired,
* or never wrote any rows). Returns malformed payloads as `null` to keep
* polling resilient against future schema changes.
*/
export async function readExportProgressFromSharedStore(args: { exportJobId: string; tenantId: string }): Promise<ExportProgressSnapshot | null> {
void args.tenantId; // tenant scoping is enforced by the caller; key is global per jobId.
const raw = await getSharedStore().get(`export:progress:${args.exportJobId}`);
if (!raw) return null;
try {
const parsed = JSON.parse(raw) as Partial<ExportProgressSnapshot>;
if (typeof parsed.processedRows !== "number") return null;
return {
processedRows: parsed.processedRows,
totalEstimate: typeof parsed.totalEstimate === "number" ? parsed.totalEstimate : null,
updatedAt: typeof parsed.updatedAt === "string" ? parsed.updatedAt : null,
};
} catch {
return null;
}
}