2026-10-23 11:43:49 -05:00
|
|
|
import { NextRequest, NextResponse } from 'next/server';
|
2026-11-18 05:18:55 +06:00
|
|
|
import { SettingsManager } from '@/core/adminSettings/settingsManager';
|
2026-11-19 20:22:01 +06:00
|
|
|
import { SDKManager } from '@/core/sdk/sdkManager';
|
2026-11-18 05:18:55 +06:00
|
|
|
import { getInternalIngestionHeaders } from '@/core/Security/security';
|
2026-10-23 11:43:49 -05:00
|
|
|
import { CSVStorageManager } from '@/core/Storage/csvStorage';
|
|
|
|
|
import { DataBufferEngine } from '@/core/data/buffer';
|
2026-11-19 20:22:01 +06:00
|
|
|
import { requireAdminApiSession } from '@/lib/adminAuth';
|
2026-10-23 11:43:49 -05:00
|
|
|
|
|
|
|
|
export async function GET(req: NextRequest) {
|
2026-11-19 20:22:01 +06:00
|
|
|
const auth = await requireAdminApiSession(req);
|
|
|
|
|
if (!auth.valid) {
|
|
|
|
|
return auth.response;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-23 11:43:49 -05:00
|
|
|
try {
|
|
|
|
|
const { searchParams } = new URL(req.url);
|
2026-11-19 20:22:01 +06:00
|
|
|
const rawClientId = searchParams.get('clientId');
|
|
|
|
|
const clientId =
|
|
|
|
|
rawClientId && rawClientId !== 'all'
|
|
|
|
|
? SDKManager.normalizeClientId(rawClientId)
|
|
|
|
|
: undefined;
|
|
|
|
|
const sanitizedClientId = clientId
|
|
|
|
|
? CSVStorageManager.sanitizeClientId(clientId)
|
|
|
|
|
: undefined;
|
2026-10-23 11:43:49 -05:00
|
|
|
|
2026-11-19 20:22:01 +06:00
|
|
|
const allFiles = await CSVStorageManager.listAllFilesAsync();
|
2026-11-18 05:18:55 +06:00
|
|
|
let files = allFiles;
|
2026-10-23 11:43:49 -05:00
|
|
|
|
2026-11-19 20:22:01 +06:00
|
|
|
if (clientId) {
|
|
|
|
|
files = allFiles.filter((f) => {
|
|
|
|
|
const normFolder = SDKManager.normalizeClientId(f.clientId);
|
|
|
|
|
return (
|
|
|
|
|
normFolder === clientId ||
|
|
|
|
|
f.clientId === clientId ||
|
|
|
|
|
f.clientId === sanitizedClientId
|
|
|
|
|
);
|
|
|
|
|
});
|
2026-10-23 11:43:49 -05:00
|
|
|
}
|
|
|
|
|
|
2026-11-19 20:22:01 +06:00
|
|
|
const stats = CSVStorageManager.formatStorageStats(files);
|
|
|
|
|
|
2026-10-23 11:43:49 -05:00
|
|
|
return NextResponse.json({ stats, files });
|
|
|
|
|
} catch (err: unknown) {
|
|
|
|
|
const message = err instanceof Error ? err.message : 'Storage stats failed';
|
|
|
|
|
return NextResponse.json({ error: message }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-19 20:22:01 +06:00
|
|
|
export async function POST(req: NextRequest) {
|
|
|
|
|
const auth = await requireAdminApiSession(req);
|
|
|
|
|
if (!auth.valid) {
|
|
|
|
|
return auth.response;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-23 11:43:49 -05:00
|
|
|
try {
|
2026-11-18 05:18:55 +06:00
|
|
|
await SettingsManager.getSettingsAsync();
|
|
|
|
|
|
|
|
|
|
// 1. Flush local / shared Next.js DataBufferEngine
|
2026-11-19 20:22:01 +06:00
|
|
|
const localRes = await DataBufferEngine.flush({
|
|
|
|
|
waitMs: 2500,
|
|
|
|
|
throwOnLockTimeout: true,
|
|
|
|
|
});
|
2026-11-17 20:05:00 +06:00
|
|
|
|
|
|
|
|
// 2. Also flush Port 4000 Socket.IO ingestion buffer
|
|
|
|
|
let remoteCount = 0;
|
|
|
|
|
let remoteFiles: string[] = [];
|
2026-11-18 05:18:55 +06:00
|
|
|
let ingestionFlushed = false;
|
|
|
|
|
let warning: string | undefined;
|
|
|
|
|
const explicitIngestionUrl = Boolean(process.env.INGESTION_INTERNAL_URL?.trim());
|
2026-11-17 20:05:00 +06:00
|
|
|
const ingestionBaseUrl = (
|
|
|
|
|
process.env.INGESTION_INTERNAL_URL || 'http://127.0.0.1:4000'
|
|
|
|
|
).replace(/\/$/, '');
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const ingestRes = await fetch(`${ingestionBaseUrl}/internal/flush`, {
|
|
|
|
|
method: 'POST',
|
2026-11-19 20:22:01 +06:00
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
...getInternalIngestionHeaders(),
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({ waitMs: 2500, throwOnLockTimeout: true }),
|
2026-11-17 20:05:00 +06:00
|
|
|
cache: 'no-store',
|
2026-11-19 20:22:01 +06:00
|
|
|
signal: AbortSignal.timeout(4000),
|
2026-11-17 20:05:00 +06:00
|
|
|
});
|
2026-11-18 05:18:55 +06:00
|
|
|
if (!ingestRes.ok) {
|
|
|
|
|
const errPayload = await ingestRes.json().catch(() => ({}));
|
|
|
|
|
const remoteErr =
|
|
|
|
|
typeof errPayload?.error === 'string'
|
|
|
|
|
? errPayload.error
|
|
|
|
|
: `Ingestion flush returned HTTP ${ingestRes.status}`;
|
2026-11-19 20:22:01 +06:00
|
|
|
if (
|
|
|
|
|
ingestRes.status === 503 &&
|
|
|
|
|
errPayload?.code === 'FLUSH_LOCK_TIMEOUT'
|
|
|
|
|
) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{
|
|
|
|
|
success: false,
|
|
|
|
|
error: remoteErr,
|
|
|
|
|
code: 'FLUSH_LOCK_TIMEOUT',
|
|
|
|
|
flushedCount: localRes.count,
|
|
|
|
|
filesUpdated: localRes.files,
|
|
|
|
|
ingestionFlushed: false,
|
|
|
|
|
ingestionStatus: ingestRes.status,
|
|
|
|
|
},
|
|
|
|
|
{ status: 503 },
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-11-18 05:18:55 +06:00
|
|
|
return NextResponse.json(
|
|
|
|
|
{
|
|
|
|
|
success: false,
|
|
|
|
|
error: remoteErr,
|
|
|
|
|
code: 'INGESTION_FLUSH_FAILED',
|
|
|
|
|
flushedCount: localRes.count,
|
|
|
|
|
filesUpdated: localRes.files,
|
|
|
|
|
ingestionFlushed: false,
|
|
|
|
|
ingestionStatus: ingestRes.status,
|
|
|
|
|
},
|
|
|
|
|
{ status: 502 },
|
|
|
|
|
);
|
2026-11-17 20:05:00 +06:00
|
|
|
}
|
2026-11-18 05:18:55 +06:00
|
|
|
|
|
|
|
|
ingestionFlushed = true;
|
|
|
|
|
const ingestData = await ingestRes.json();
|
|
|
|
|
remoteCount = Number(ingestData.flushedCount) || 0;
|
|
|
|
|
if (Array.isArray(ingestData.filesUpdated)) {
|
|
|
|
|
remoteFiles = ingestData.filesUpdated;
|
|
|
|
|
}
|
|
|
|
|
} catch (err: unknown) {
|
|
|
|
|
const errMsg =
|
|
|
|
|
err instanceof Error ? err.message : 'Ingestion server unreachable';
|
|
|
|
|
if (explicitIngestionUrl) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{
|
|
|
|
|
success: false,
|
|
|
|
|
error: `Ingestion server flush failed: ${errMsg}`,
|
|
|
|
|
code: 'INGESTION_UNREACHABLE',
|
|
|
|
|
flushedCount: localRes.count,
|
|
|
|
|
filesUpdated: localRes.files,
|
|
|
|
|
ingestionFlushed: false,
|
|
|
|
|
},
|
|
|
|
|
{ status: 502 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
warning = `Port 4000 ingestion server is unreachable (${errMsg}); flushed Next.js buffer only.`;
|
2026-11-17 20:05:00 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const combinedFiles = Array.from(
|
|
|
|
|
new Set([...localRes.files, ...remoteFiles]),
|
|
|
|
|
);
|
|
|
|
|
|
2026-10-23 11:43:49 -05:00
|
|
|
return NextResponse.json({
|
|
|
|
|
success: true,
|
2026-11-17 20:05:00 +06:00
|
|
|
flushedCount: localRes.count + remoteCount,
|
|
|
|
|
filesUpdated: combinedFiles,
|
2026-11-18 05:18:55 +06:00
|
|
|
ingestionFlushed,
|
|
|
|
|
...(warning ? { warning } : {}),
|
2026-10-23 11:43:49 -05:00
|
|
|
});
|
|
|
|
|
} catch (err: unknown) {
|
|
|
|
|
const message = err instanceof Error ? err.message : 'Manual flush failed';
|
2026-11-19 20:22:01 +06:00
|
|
|
if (message.includes('FLUSH_LOCK_TIMEOUT')) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{
|
|
|
|
|
success: false,
|
|
|
|
|
error: message,
|
|
|
|
|
code: 'FLUSH_LOCK_TIMEOUT',
|
|
|
|
|
},
|
|
|
|
|
{ status: 503 },
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-10-23 11:43:49 -05:00
|
|
|
return NextResponse.json({ error: message }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
}
|