2026-11-18 05:18:55 +06:00
|
|
|
import { SettingsManager } from '../adminSettings/settingsManager';
|
2026-10-23 11:43:49 -05:00
|
|
|
import { SDKManager } from '../sdk/sdkManager';
|
|
|
|
|
import { CSVStorageManager } from '../Storage/csvStorage';
|
|
|
|
|
import { DataBufferEngine } from '../data/buffer';
|
2026-11-18 05:18:55 +06:00
|
|
|
import {
|
|
|
|
|
SecurityEngine,
|
|
|
|
|
getInternalIngestionHeaders,
|
|
|
|
|
} from '../Security/security';
|
2026-10-23 11:43:49 -05:00
|
|
|
|
|
|
|
|
export class ApiEngine {
|
|
|
|
|
/**
|
|
|
|
|
* Retrieves overall telemetry and health metrics for the dashboard
|
|
|
|
|
*/
|
|
|
|
|
public static async getDashboardMetrics() {
|
2026-11-18 05:18:55 +06:00
|
|
|
const settings = await SettingsManager.getSettingsAsync();
|
|
|
|
|
const [storageStats, localBufferStats, clients] = await Promise.all([
|
|
|
|
|
CSVStorageManager.getStorageStatsAsync(),
|
|
|
|
|
DataBufferEngine.getBufferStatsAsync(),
|
|
|
|
|
SDKManager.listClientsAsync(),
|
|
|
|
|
]);
|
2026-10-23 11:43:49 -05:00
|
|
|
const activeClients = clients.filter((c) => c.status === 'active').length;
|
|
|
|
|
let activeSockets = 0;
|
2026-11-18 05:18:55 +06:00
|
|
|
let ingestionLocalBufferSize = 0;
|
|
|
|
|
let ingestionRedisBufferSize = 0;
|
|
|
|
|
let ingestionRedisConnected = false;
|
|
|
|
|
let remoteSecurityStats: {
|
|
|
|
|
activeIdempotencyKeys?: number;
|
|
|
|
|
rateLimitedClientsCount?: number;
|
|
|
|
|
maxPayloadSizeBytes?: number;
|
|
|
|
|
} | null = null;
|
2026-11-17 20:05:00 +06:00
|
|
|
|
|
|
|
|
const ingestionBaseUrl = (
|
|
|
|
|
process.env.INGESTION_INTERNAL_URL || 'http://127.0.0.1:4000'
|
|
|
|
|
).replace(/\/$/, '');
|
2026-11-18 05:18:55 +06:00
|
|
|
const internalHeaders = getInternalIngestionHeaders();
|
2026-10-23 11:43:49 -05:00
|
|
|
|
|
|
|
|
try {
|
2026-11-18 05:18:55 +06:00
|
|
|
const res = await fetch(`${ingestionBaseUrl}/internal/metrics`, {
|
|
|
|
|
headers: internalHeaders,
|
2026-11-17 20:05:00 +06:00
|
|
|
cache: 'no-store',
|
2026-11-18 05:18:55 +06:00
|
|
|
signal: AbortSignal.timeout(1000),
|
2026-11-17 20:05:00 +06:00
|
|
|
});
|
2026-10-23 11:43:49 -05:00
|
|
|
if (res.ok) {
|
|
|
|
|
const data = await res.json();
|
2026-11-17 20:05:00 +06:00
|
|
|
activeSockets = Number(data.activeSockets) || 0;
|
2026-11-18 05:18:55 +06:00
|
|
|
ingestionRedisConnected = Boolean(data.redisConnected);
|
|
|
|
|
if (data.localBufferSize !== undefined) {
|
|
|
|
|
ingestionLocalBufferSize = Number(data.localBufferSize) || 0;
|
|
|
|
|
ingestionRedisBufferSize = Number(data.redisBufferSize) || 0;
|
|
|
|
|
} else {
|
|
|
|
|
ingestionLocalBufferSize = Number(data.bufferSize) || 0;
|
|
|
|
|
}
|
|
|
|
|
if (data.securityStats && typeof data.securityStats === 'object') {
|
|
|
|
|
remoteSecurityStats = data.securityStats;
|
|
|
|
|
}
|
2026-10-23 11:43:49 -05:00
|
|
|
}
|
|
|
|
|
} catch {
|
2026-11-17 20:05:00 +06:00
|
|
|
// Ingestion server may not be running in single-process dev mode
|
2026-10-23 11:43:49 -05:00
|
|
|
activeSockets = 0;
|
2026-11-18 05:18:55 +06:00
|
|
|
ingestionLocalBufferSize = 0;
|
|
|
|
|
ingestionRedisBufferSize = 0;
|
2026-10-23 11:43:49 -05:00
|
|
|
}
|
|
|
|
|
|
2026-11-18 05:18:55 +06:00
|
|
|
const localSecurityStats = SecurityEngine.getStats();
|
|
|
|
|
const securityStats = {
|
|
|
|
|
activeIdempotencyKeys:
|
|
|
|
|
localSecurityStats.activeIdempotencyKeys +
|
|
|
|
|
(Number(remoteSecurityStats?.activeIdempotencyKeys) || 0),
|
|
|
|
|
rateLimitedClientsCount:
|
|
|
|
|
localSecurityStats.rateLimitedClientsCount +
|
|
|
|
|
(Number(remoteSecurityStats?.rateLimitedClientsCount) || 0),
|
|
|
|
|
maxPayloadSizeBytes: Math.min(
|
|
|
|
|
localSecurityStats.maxPayloadSizeBytes,
|
|
|
|
|
Number(remoteSecurityStats?.maxPayloadSizeBytes) ||
|
|
|
|
|
localSecurityStats.maxPayloadSizeBytes,
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const effectiveRedisBufferSize = Math.max(
|
|
|
|
|
localBufferStats.redisBufferSize || 0,
|
|
|
|
|
ingestionRedisBufferSize,
|
|
|
|
|
);
|
2026-11-17 20:05:00 +06:00
|
|
|
const bufferStats = {
|
|
|
|
|
...localBufferStats,
|
2026-11-18 05:18:55 +06:00
|
|
|
redisConnected: localBufferStats.redisConnected || ingestionRedisConnected,
|
|
|
|
|
redisBufferSize: effectiveRedisBufferSize,
|
2026-11-17 20:05:00 +06:00
|
|
|
currentBufferSize:
|
2026-11-18 05:18:55 +06:00
|
|
|
localBufferStats.localBufferSize +
|
|
|
|
|
ingestionLocalBufferSize +
|
|
|
|
|
effectiveRedisBufferSize,
|
2026-11-17 20:05:00 +06:00
|
|
|
};
|
2026-10-23 11:43:49 -05:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
activeSockets,
|
|
|
|
|
totalClients: clients.length,
|
|
|
|
|
activeClients,
|
|
|
|
|
bufferStats,
|
|
|
|
|
storageStats,
|
|
|
|
|
securityStats,
|
|
|
|
|
runtime: {
|
2026-11-18 05:18:55 +06:00
|
|
|
serverPort: settings.serverPort || 4000,
|
|
|
|
|
webPort: settings.webPort || 3000,
|
2026-10-23 11:43:49 -05:00
|
|
|
uptimeSeconds: Math.floor(process.uptime()),
|
|
|
|
|
memoryUsageMb: (process.memoryUsage().rss / (1024 * 1024)).toFixed(1),
|
|
|
|
|
nodeVersion: process.version,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-11-18 05:18:55 +06:00
|
|
|
* Filters and lists stored CSV files for export
|
2026-10-23 11:43:49 -05:00
|
|
|
*/
|
|
|
|
|
public static getExportFiles(clientId?: string) {
|
|
|
|
|
const allFiles = CSVStorageManager.listAllFiles();
|
|
|
|
|
if (clientId && clientId !== 'all') {
|
|
|
|
|
return allFiles.filter((f) => f.clientId === clientId);
|
|
|
|
|
}
|
|
|
|
|
return allFiles;
|
|
|
|
|
}
|
2026-11-18 05:18:55 +06:00
|
|
|
|
|
|
|
|
public static async getExportFilesAsync(clientId?: string) {
|
|
|
|
|
const allFiles = await CSVStorageManager.listAllFilesAsync();
|
|
|
|
|
if (clientId && clientId !== 'all') {
|
|
|
|
|
return allFiles.filter((f) => f.clientId === clientId);
|
|
|
|
|
}
|
|
|
|
|
return allFiles;
|
|
|
|
|
}
|
2026-10-23 11:43:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default ApiEngine;
|