From 86679d2bf209314fdc021fcfffeeee821bf98371 Mon Sep 17 00:00:00 2001 From: li4nee Date: Fri, 10 Jul 2026 15:00:56 +0545 Subject: [PATCH] feat(analytics): enhance endpoint metrics calculations and queries for accuracy --- .../analytics/services/analytics.service.ts | 20 +++++++---- .../processor/repos/endpointMetrics.repo.ts | 34 +++++++++++++++++-- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/server/src/modules/analytics/services/analytics.service.ts b/server/src/modules/analytics/services/analytics.service.ts index e7f2952..147b0c8 100644 --- a/server/src/modules/analytics/services/analytics.service.ts +++ b/server/src/modules/analytics/services/analytics.service.ts @@ -36,18 +36,26 @@ export class AnalyticsService implements IAnalyticsService { private mapToEndpointSummary(rows: EndpointMetrics[]): EndpointSummary[] { return rows.map((row) => { - const avgLatency = row.total_hits > 0 ? parseFloat((row.total_latency / row.total_hits).toFixed(2)) : 0; - const errorRate = row.total_hits > 0 ? parseFloat(((row.error_hits / row.total_hits) * 100).toFixed(2)) : 0; + // Postgres SUM()/MIN()/MAX() on integer columns come back as strings + // over the wire (bigint safety) — coerce before doing arithmetic or + // handing them to the frontend as JSON numbers. + const totalHits = Number(row.total_hits); + const errorHits = Number(row.error_hits); + const totalLatency = Number(row.total_latency); + const minLatency = Number(row.min_latency); + const maxLatency = Number(row.max_latency); + const avgLatency = totalHits > 0 ? parseFloat((totalLatency / totalHits).toFixed(2)) : 0; + const errorRate = totalHits > 0 ? parseFloat(((errorHits / totalHits) * 100).toFixed(2)) : 0; return { service_name: row.service_name, endpoint: row.endpoint, method: row.method, - total_hits: row.total_hits, - error_hits: row.error_hits, + total_hits: totalHits, + error_hits: errorHits, error_rate: errorRate, avg_latency: avgLatency, - min_latency: row.min_latency, - max_latency: row.max_latency, + min_latency: minLatency, + max_latency: maxLatency, }; }); } diff --git a/server/src/modules/processor/repos/endpointMetrics.repo.ts b/server/src/modules/processor/repos/endpointMetrics.repo.ts index 76dfb0d..8f241b7 100644 --- a/server/src/modules/processor/repos/endpointMetrics.repo.ts +++ b/server/src/modules/processor/repos/endpointMetrics.repo.ts @@ -140,7 +140,21 @@ export class PgEndPointMetricsRepo extends EndPointMetricsBaseRepo`0`.as("id"), + sql`MAX(client_id)`.as("client_id"), + sql`MAX(time_bucket)`.as("time_bucket"), + sql`SUM(total_hits)`.as("total_hits"), + sql`SUM(error_hits)`.as("error_hits"), + sql`MIN(min_latency)`.as("min_latency"), + sql`MAX(max_latency)`.as("max_latency"), + sql`SUM(total_latency)`.as("total_latency"), + sql`MAX(created_at)`.as("created_at"), + sql`MAX(updated_at)`.as("updated_at"), + ]) .groupBy(["service_name", "endpoint", "method"]) .orderBy(metric, "desc") .limit(safeLimit); @@ -214,9 +228,23 @@ export class PgEndPointMetricsRepo extends EndPointMetricsBaseRepo`0`.as("id"), + sql`MAX(client_id)`.as("client_id"), + sql`MAX(time_bucket)`.as("time_bucket"), + sql`SUM(total_hits)`.as("total_hits"), + sql`SUM(error_hits)`.as("error_hits"), + sql`MIN(min_latency)`.as("min_latency"), + sql`MAX(max_latency)`.as("max_latency"), + sql`SUM(total_latency)`.as("total_latency"), + sql`MAX(created_at)`.as("created_at"), + sql`MAX(updated_at)`.as("updated_at"), + ]) .groupBy(["service_name", "endpoint", "method"]) - .orderBy(sql`total_latency::float / NULLIF(total_hits, 0)`, "desc") + .orderBy(sql`SUM(total_latency)::float / NULLIF(SUM(total_hits), 0)`, "desc") .limit(safeLimit); if (!startTime) {