fix: refactor audit to SSE streaming, improve error handling
- Changed from blocking JSON endpoint to SSE streaming (like sync-all/repair-icons) - Progress updates during each audit phase with item counts - Proper error handling with typed AuditEvent for every failure path - AbortController support for the client - Shows real-time progress for each check section
This commit is contained in:
1 parent
19e4e10b1d
commit
bebd65c056
3 files changed
+444
-203
No files matched your search
@@ -6,6 +6,23 @@ import { listSources } from "@/lib/services/clone-sources";
|
||||
import { getFurniAssetDirs } from "@/lib/services/furni-asset-dirs";
|
||||
import { ensureDirectories } from "./furni-import";
|
||||
|
||||
export interface AuditEvent {
|
||||
type:
|
||||
| "progress"
|
||||
| "items_loaded"
|
||||
| "catalog_loaded"
|
||||
| "checking"
|
||||
| "batch_complete"
|
||||
| "error";
|
||||
message?: string;
|
||||
current?: number;
|
||||
total?: number;
|
||||
section?: string;
|
||||
issues?: AuditIssue[];
|
||||
missingFromSources?: MissingFromSource[];
|
||||
summary?: AuditSummary;
|
||||
}
|
||||
|
||||
export interface AuditIssue {
|
||||
type: string;
|
||||
severity: "error" | "warning" | "info";
|
||||
@@ -23,68 +40,106 @@ export interface MissingFromSource {
|
||||
itemName: string;
|
||||
}
|
||||
|
||||
export interface AuditResult {
|
||||
issues: AuditIssue[];
|
||||
missingFromSources: MissingFromSource[];
|
||||
summary: {
|
||||
totalItemsBase: number;
|
||||
totalCatalogItems: number;
|
||||
missingCatalog: number;
|
||||
orphanedCatalog: number;
|
||||
missingNitro: number;
|
||||
missingIcon: number;
|
||||
duplicates: number;
|
||||
missingFromSourcesTotal: number;
|
||||
};
|
||||
export interface AuditSummary {
|
||||
totalItemsBase: number;
|
||||
totalCatalogItems: number;
|
||||
missingCatalog: number;
|
||||
orphanedCatalog: number;
|
||||
missingNitro: number;
|
||||
missingIcon: number;
|
||||
duplicates: number;
|
||||
missingFromSourcesTotal: number;
|
||||
}
|
||||
|
||||
export async function runCatalogAudit(
|
||||
onProgress?: (msg: string) => void,
|
||||
): Promise<AuditResult> {
|
||||
onEvent?: (evt: AuditEvent) => void,
|
||||
): Promise<void> {
|
||||
const issues: AuditIssue[] = [];
|
||||
const missingFromSources: MissingFromSource[] = [];
|
||||
|
||||
onProgress?.("Loading directories…");
|
||||
await ensureDirectories();
|
||||
const { iconDir, nitroDir } = await getFurniAssetDirs();
|
||||
try {
|
||||
await ensureDirectories();
|
||||
} catch (err) {
|
||||
onEvent?.({
|
||||
type: "error",
|
||||
message: `ensureDirectories failed: ${(err as Error).message}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
onProgress?.("Loading items_base…");
|
||||
const items = await prisma.$queryRaw<
|
||||
Array<{ id: number; item_name: string; public_name: string; type: string }>
|
||||
>`SELECT id, item_name, public_name, type FROM items_base ORDER BY id`;
|
||||
let iconDir: string;
|
||||
let nitroDir: string;
|
||||
try {
|
||||
const dirs = await getFurniAssetDirs();
|
||||
iconDir = dirs.iconDir;
|
||||
nitroDir = dirs.nitroDir;
|
||||
} catch (err) {
|
||||
onEvent?.({
|
||||
type: "error",
|
||||
message: `getFurniAssetDirs failed: ${(err as Error).message}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
onProgress?.("Loading catalog_items…");
|
||||
const catalogItems = await prisma.$queryRaw<
|
||||
Array<{ id: number; item_ids: string; catalog_name: string }>
|
||||
>`SELECT id, item_ids, catalog_name FROM catalog_items ORDER BY id`;
|
||||
onEvent?.({ type: "progress", message: "Loading items_base…" });
|
||||
|
||||
const summary = {
|
||||
totalItemsBase: items.length,
|
||||
totalCatalogItems: catalogItems.length,
|
||||
missingCatalog: 0,
|
||||
orphanedCatalog: 0,
|
||||
missingNitro: 0,
|
||||
missingIcon: 0,
|
||||
duplicates: 0,
|
||||
missingFromSourcesTotal: 0,
|
||||
};
|
||||
let items: Array<{
|
||||
id: number;
|
||||
item_name: string;
|
||||
public_name: string;
|
||||
type: string;
|
||||
}>;
|
||||
try {
|
||||
items = await prisma.$queryRaw<
|
||||
Array<{
|
||||
id: number;
|
||||
item_name: string;
|
||||
public_name: string;
|
||||
type: string;
|
||||
}>
|
||||
>`SELECT id, item_name, public_name, type FROM items_base ORDER BY id`;
|
||||
} catch (err) {
|
||||
onEvent?.({
|
||||
type: "error",
|
||||
message: `items_base query failed: ${(err as Error).message}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
onEvent?.({ type: "items_loaded", total: items.length });
|
||||
|
||||
onEvent?.({ type: "progress", message: "Loading catalog_items…" });
|
||||
|
||||
let catalogItems: Array<{
|
||||
id: number;
|
||||
item_ids: string;
|
||||
catalog_name: string;
|
||||
}>;
|
||||
try {
|
||||
catalogItems = await prisma.$queryRaw<
|
||||
Array<{ id: number; item_ids: string; catalog_name: string }>
|
||||
>`SELECT id, item_ids, catalog_name FROM catalog_items ORDER BY id`;
|
||||
} catch (err) {
|
||||
onEvent?.({
|
||||
type: "error",
|
||||
message: `catalog_items query failed: ${(err as Error).message}`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
onEvent?.({ type: "catalog_loaded", total: catalogItems.length });
|
||||
|
||||
// Build lookup: set of items_base IDs that have catalog entries
|
||||
const itemIdsWithCatalog = new Set<number>();
|
||||
const itemNamesWithCatalog = new Set<string>();
|
||||
for (const ci of catalogItems) {
|
||||
const ids = ci.item_ids.split(";").map(Number).filter(Boolean);
|
||||
for (const id of ids) {
|
||||
itemIdsWithCatalog.add(id);
|
||||
}
|
||||
itemNamesWithCatalog.add(ci.catalog_name);
|
||||
}
|
||||
|
||||
// Build items_base lookup by id and name
|
||||
const itemById = new Map<number, (typeof items)[0]>();
|
||||
const itemByName = new Map<string, (typeof items)[0]>();
|
||||
const classnameCount = new Map<string, number>();
|
||||
|
||||
for (const item of items) {
|
||||
itemById.set(item.id, item);
|
||||
itemByName.set(item.item_name, item);
|
||||
@@ -94,10 +149,15 @@ export async function runCatalogAudit(
|
||||
);
|
||||
}
|
||||
|
||||
onProgress?.("Checking items_base entries…");
|
||||
for (const item of items) {
|
||||
onEvent?.({
|
||||
type: "checking",
|
||||
section: "missing_catalog",
|
||||
total: items.length,
|
||||
});
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const item = items[i];
|
||||
if (!itemIdsWithCatalog.has(item.id)) {
|
||||
summary.missingCatalog++;
|
||||
issues.push({
|
||||
type: "missing_catalog",
|
||||
severity: "warning",
|
||||
@@ -106,12 +166,20 @@ export async function runCatalogAudit(
|
||||
message: `"${item.item_name}" (ID ${item.id}) has no catalog_items entry — not purchasable`,
|
||||
});
|
||||
}
|
||||
if (i % 100 === 0) {
|
||||
onEvent?.({
|
||||
type: "checking",
|
||||
section: "missing_catalog",
|
||||
current: i,
|
||||
total: items.length,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onProgress?.("Checking for duplicate classnames…");
|
||||
onEvent?.({ type: "checking", section: "duplicates" });
|
||||
|
||||
for (const [name, count] of classnameCount) {
|
||||
if (count > 1) {
|
||||
summary.duplicates++;
|
||||
issues.push({
|
||||
type: "duplicate_classname",
|
||||
severity: "error",
|
||||
@@ -121,12 +189,17 @@ export async function runCatalogAudit(
|
||||
}
|
||||
}
|
||||
|
||||
onProgress?.("Checking catalog_items for orphaned references…");
|
||||
for (const ci of catalogItems) {
|
||||
onEvent?.({
|
||||
type: "checking",
|
||||
section: "orphaned_catalog",
|
||||
total: catalogItems.length,
|
||||
});
|
||||
|
||||
for (let i = 0; i < catalogItems.length; i++) {
|
||||
const ci = catalogItems[i];
|
||||
const ids = ci.item_ids.split(";").map(Number).filter(Boolean);
|
||||
for (const id of ids) {
|
||||
if (!itemById.has(id)) {
|
||||
summary.orphanedCatalog++;
|
||||
issues.push({
|
||||
type: "orphaned_catalog",
|
||||
severity: "error",
|
||||
@@ -135,13 +208,26 @@ export async function runCatalogAudit(
|
||||
});
|
||||
}
|
||||
}
|
||||
if (i % 200 === 0) {
|
||||
onEvent?.({
|
||||
type: "checking",
|
||||
section: "orphaned_catalog",
|
||||
current: i,
|
||||
total: catalogItems.length,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onProgress?.("Checking .nitro files…");
|
||||
for (const item of items) {
|
||||
onEvent?.({
|
||||
type: "checking",
|
||||
section: "missing_nitro",
|
||||
total: items.length,
|
||||
});
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const item = items[i];
|
||||
const nitroPath = path.join(nitroDir, `${item.item_name}.nitro`);
|
||||
if (!existsSync(nitroPath)) {
|
||||
summary.missingNitro++;
|
||||
issues.push({
|
||||
type: "missing_nitro",
|
||||
severity: "error",
|
||||
@@ -150,13 +236,22 @@ export async function runCatalogAudit(
|
||||
message: `"${item.item_name}" (ID ${item.id}) has no .nitro file`,
|
||||
});
|
||||
}
|
||||
if (i % 100 === 0) {
|
||||
onEvent?.({
|
||||
type: "checking",
|
||||
section: "missing_nitro",
|
||||
current: i,
|
||||
total: items.length,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onProgress?.("Checking icon files…");
|
||||
for (const item of items) {
|
||||
onEvent?.({ type: "checking", section: "missing_icon", total: items.length });
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const item = items[i];
|
||||
const iconPath = path.join(iconDir, `${item.item_name}_icon.png`);
|
||||
if (!existsSync(iconPath)) {
|
||||
summary.missingIcon++;
|
||||
issues.push({
|
||||
type: "missing_icon",
|
||||
severity: "warning",
|
||||
@@ -165,50 +260,62 @@ export async function runCatalogAudit(
|
||||
message: `"${item.item_name}" (ID ${item.id}) has no icon file`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onProgress?.("Comparing with clone sources…");
|
||||
try {
|
||||
const sources = await listSources();
|
||||
for (const source of sources) {
|
||||
try {
|
||||
const entries = await fetchSourceFurnidata(source.furnidataUrl);
|
||||
for (const entry of entries) {
|
||||
if (!itemByName.has(entry.classname)) {
|
||||
missingFromSources.push({
|
||||
sourceName: source.name,
|
||||
sourceId: source.id,
|
||||
classname: entry.classname,
|
||||
itemName: entry.name,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
issues.push({
|
||||
type: "source_fetch_failed",
|
||||
severity: "error",
|
||||
sourceName: source.name,
|
||||
message: `Failed to fetch furnidata from "${source.name}"`,
|
||||
});
|
||||
}
|
||||
if (i % 100 === 0) {
|
||||
onEvent?.({
|
||||
type: "checking",
|
||||
section: "missing_icon",
|
||||
current: i,
|
||||
total: items.length,
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
issues.push({
|
||||
type: "sources_list_failed",
|
||||
severity: "error",
|
||||
message: `Failed to list clone sources: ${(err as Error).message}`,
|
||||
});
|
||||
}
|
||||
|
||||
summary.missingFromSourcesTotal = missingFromSources.length;
|
||||
onEvent?.({ type: "progress", message: "Comparing with clone sources…" });
|
||||
|
||||
const sources = await listSources();
|
||||
for (const source of sources) {
|
||||
try {
|
||||
const entries = await fetchSourceFurnidata(source.furnidataUrl);
|
||||
for (const entry of entries) {
|
||||
if (!itemByName.has(entry.classname)) {
|
||||
missingFromSources.push({
|
||||
sourceName: source.name,
|
||||
sourceId: source.id,
|
||||
classname: entry.classname,
|
||||
itemName: entry.name,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
issues.push({
|
||||
type: "source_fetch_failed",
|
||||
severity: "error",
|
||||
sourceName: source.name,
|
||||
message: `Failed to fetch furnidata from "${source.name}"`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Sort issues: errors first, then by type
|
||||
issues.sort((a, b) => {
|
||||
const sev = { error: 0, warning: 1, info: 2 };
|
||||
const diff = (sev[a.severity] ?? 0) - (sev[b.severity] ?? 0);
|
||||
if (diff !== 0) return diff;
|
||||
return a.type.localeCompare(b.type);
|
||||
return (sev[a.severity] ?? 0) - (sev[b.severity] ?? 0);
|
||||
});
|
||||
|
||||
return { issues, missingFromSources, summary };
|
||||
const summary: AuditSummary = {
|
||||
totalItemsBase: items.length,
|
||||
totalCatalogItems: catalogItems.length,
|
||||
missingCatalog: issues.filter((i) => i.type === "missing_catalog").length,
|
||||
orphanedCatalog: issues.filter((i) => i.type === "orphaned_catalog").length,
|
||||
missingNitro: issues.filter((i) => i.type === "missing_nitro").length,
|
||||
missingIcon: issues.filter((i) => i.type === "missing_icon").length,
|
||||
duplicates: issues.filter((i) => i.type === "duplicate_classname").length,
|
||||
missingFromSourcesTotal: missingFromSources.length,
|
||||
};
|
||||
|
||||
onEvent?.({
|
||||
type: "batch_complete",
|
||||
issues,
|
||||
missingFromSources,
|
||||
summary,
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user