Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions server/src/modules/analytics/services/analytics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
});
}
Expand Down
34 changes: 31 additions & 3 deletions server/src/modules/processor/repos/endpointMetrics.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,21 @@ export class PgEndPointMetricsRepo extends EndPointMetricsBaseRepo<EndpointMetri
try {
const safeLimit = Math.min(Math.max(limit, 1), 100);
let query = PostgresDB.selectFrom("endpoint_metrics")
.selectAll()
.select([
"service_name",
"endpoint",
"method",
sql<number>`0`.as("id"),
sql<string>`MAX(client_id)`.as("client_id"),
sql<Date>`MAX(time_bucket)`.as("time_bucket"),
sql<number>`SUM(total_hits)`.as("total_hits"),
sql<number>`SUM(error_hits)`.as("error_hits"),
sql<number>`MIN(min_latency)`.as("min_latency"),
sql<number>`MAX(max_latency)`.as("max_latency"),
sql<number>`SUM(total_latency)`.as("total_latency"),
sql<Date>`MAX(created_at)`.as("created_at"),
sql<Date>`MAX(updated_at)`.as("updated_at"),
])
.groupBy(["service_name", "endpoint", "method"])
.orderBy(metric, "desc")
.limit(safeLimit);
Expand Down Expand Up @@ -214,9 +228,23 @@ export class PgEndPointMetricsRepo extends EndPointMetricsBaseRepo<EndpointMetri
try {
const safeLimit = Math.min(Math.max(limit, 1), 100);
let query = PostgresDB.selectFrom("endpoint_metrics")
.selectAll()
.select([
"service_name",
"endpoint",
"method",
sql<number>`0`.as("id"),
sql<string>`MAX(client_id)`.as("client_id"),
sql<Date>`MAX(time_bucket)`.as("time_bucket"),
sql<number>`SUM(total_hits)`.as("total_hits"),
sql<number>`SUM(error_hits)`.as("error_hits"),
sql<number>`MIN(min_latency)`.as("min_latency"),
sql<number>`MAX(max_latency)`.as("max_latency"),
sql<number>`SUM(total_latency)`.as("total_latency"),
sql<Date>`MAX(created_at)`.as("created_at"),
sql<Date>`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) {
Expand Down
Loading