|
|
|
|
@@ -34,48 +34,78 @@ export class DashboardGenerator {
|
|
|
|
|
* - Resource data
|
|
|
|
|
*/
|
|
|
|
|
public async serveMetrics(): Promise<Response> {
|
|
|
|
|
const metrics = getMetricsCollector();
|
|
|
|
|
const persistentStore = getPersistentStore();
|
|
|
|
|
await persistentStore.init();
|
|
|
|
|
const requestLogStore = getRequestLogStore();
|
|
|
|
|
try {
|
|
|
|
|
const metrics = getMetricsCollector();
|
|
|
|
|
const persistentStore = getPersistentStore();
|
|
|
|
|
await persistentStore.init();
|
|
|
|
|
const requestLogStore = getRequestLogStore();
|
|
|
|
|
|
|
|
|
|
// Get event data
|
|
|
|
|
const eventResult = await persistentStore.getEventLog({ limit: 50 });
|
|
|
|
|
const oneHourAgo = Date.now() - 3600000;
|
|
|
|
|
const eventCountLastHour = await persistentStore.getEventCount(oneHourAgo);
|
|
|
|
|
// Get event data
|
|
|
|
|
const eventResult = await persistentStore.getEventLog({ limit: 50 });
|
|
|
|
|
const oneHourAgo = Date.now() - 3600000;
|
|
|
|
|
const eventCountLastHour = await persistentStore.getEventCount(oneHourAgo);
|
|
|
|
|
|
|
|
|
|
// Build comprehensive initial response
|
|
|
|
|
const data = {
|
|
|
|
|
// Core metrics
|
|
|
|
|
...metrics.getMetrics(),
|
|
|
|
|
cacheHitRate: metrics.getCacheHitRate(),
|
|
|
|
|
networkSuccessRate: metrics.getNetworkSuccessRate(),
|
|
|
|
|
resourceCount: metrics.getResourceCount(),
|
|
|
|
|
summary: metrics.getSummary(),
|
|
|
|
|
// Build comprehensive initial response
|
|
|
|
|
const data = {
|
|
|
|
|
// Core metrics
|
|
|
|
|
...metrics.getMetrics(),
|
|
|
|
|
cacheHitRate: metrics.getCacheHitRate(),
|
|
|
|
|
networkSuccessRate: metrics.getNetworkSuccessRate(),
|
|
|
|
|
resourceCount: metrics.getResourceCount(),
|
|
|
|
|
summary: metrics.getSummary(),
|
|
|
|
|
|
|
|
|
|
// Resources data
|
|
|
|
|
resources: metrics.getCachedResources(),
|
|
|
|
|
domains: metrics.getDomainStats(),
|
|
|
|
|
contentTypes: metrics.getContentTypeStats(),
|
|
|
|
|
// Resources data
|
|
|
|
|
resources: metrics.getCachedResources(),
|
|
|
|
|
domains: metrics.getDomainStats(),
|
|
|
|
|
contentTypes: metrics.getContentTypeStats(),
|
|
|
|
|
|
|
|
|
|
// Events data (initial 50)
|
|
|
|
|
events: eventResult.events,
|
|
|
|
|
eventTotalCount: eventResult.totalCount,
|
|
|
|
|
eventCountLastHour,
|
|
|
|
|
// Events data (initial 50)
|
|
|
|
|
events: eventResult.events,
|
|
|
|
|
eventTotalCount: eventResult.totalCount,
|
|
|
|
|
eventCountLastHour,
|
|
|
|
|
|
|
|
|
|
// Request logs data (initial 50)
|
|
|
|
|
requestLogs: requestLogStore.getEntries({ limit: 50 }),
|
|
|
|
|
requestTotalCount: requestLogStore.getTotalCount(),
|
|
|
|
|
requestStats: requestLogStore.getStats(),
|
|
|
|
|
requestMethods: requestLogStore.getMethods(),
|
|
|
|
|
};
|
|
|
|
|
// Request logs data (initial 50)
|
|
|
|
|
requestLogs: requestLogStore.getEntries({ limit: 50 }),
|
|
|
|
|
requestTotalCount: requestLogStore.getTotalCount(),
|
|
|
|
|
requestStats: requestLogStore.getStats(),
|
|
|
|
|
requestMethods: requestLogStore.getMethods(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new Response(JSON.stringify(data), {
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Cache-Control': 'no-store',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return new Response(JSON.stringify(data), {
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Cache-Control': 'no-store',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('[SW Dashboard] serveMetrics error:', error);
|
|
|
|
|
// Return error response with valid JSON structure so client doesn't crash
|
|
|
|
|
return new Response(JSON.stringify({
|
|
|
|
|
error: String(error),
|
|
|
|
|
cache: { hits: 0, misses: 0, errors: 0, bytesServedFromCache: 0, bytesFetched: 0, averageResponseTime: 0 },
|
|
|
|
|
network: { totalRequests: 0, successfulRequests: 0, failedRequests: 0, timeouts: 0, averageLatency: 0, totalBytesTransferred: 0 },
|
|
|
|
|
update: { totalChecks: 0, successfulChecks: 0, failedChecks: 0, updatesFound: 0, updatesApplied: 0, lastCheckTimestamp: 0, lastUpdateTimestamp: 0 },
|
|
|
|
|
connection: { connectedClients: 0, totalConnectionAttempts: 0, successfulConnections: 0, failedConnections: 0 },
|
|
|
|
|
speedtest: { lastDownloadSpeedMbps: 0, lastUploadSpeedMbps: 0, lastLatencyMs: 0, lastTestTimestamp: 0, testCount: 0, isOnline: false },
|
|
|
|
|
startTime: Date.now(),
|
|
|
|
|
uptime: 0,
|
|
|
|
|
cacheHitRate: 0,
|
|
|
|
|
networkSuccessRate: 0,
|
|
|
|
|
resourceCount: 0,
|
|
|
|
|
events: [],
|
|
|
|
|
eventTotalCount: 0,
|
|
|
|
|
eventCountLastHour: 0,
|
|
|
|
|
requestLogs: [],
|
|
|
|
|
requestTotalCount: 0,
|
|
|
|
|
requestStats: { totalRequests: 0, totalResponses: 0, methodCounts: {}, errorCount: 0, avgDurationMs: 0 },
|
|
|
|
|
requestMethods: [],
|
|
|
|
|
}), {
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Cache-Control': 'no-store',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|