useServiceHealthMetrics.utils.ts104 lines · main
| 1 | import dayjs from 'dayjs' |
| 2 | |
| 3 | import type { LogsBarChartDatum } from '../ProjectHome/ProjectUsage.metrics' |
| 4 | import { |
| 5 | computeSuccessAndNonSuccessRates, |
| 6 | sumErrors, |
| 7 | sumTotal, |
| 8 | sumWarnings, |
| 9 | } from '../ProjectHome/ProjectUsage.metrics' |
| 10 | |
| 11 | /** |
| 12 | * Calculates the date range for fetching service health metrics |
| 13 | * based on the selected interval |
| 14 | */ |
| 15 | export const calculateDateRange = ( |
| 16 | interval: '1hr' | '1day' | '7day' |
| 17 | ): { startDate: string; endDate: string } => { |
| 18 | const now = dayjs() |
| 19 | const end = now.toISOString() |
| 20 | let start: string |
| 21 | |
| 22 | switch (interval) { |
| 23 | case '1hr': |
| 24 | start = now.subtract(1, 'hour').toISOString() |
| 25 | break |
| 26 | case '1day': |
| 27 | start = now.subtract(1, 'day').toISOString() |
| 28 | break |
| 29 | case '7day': |
| 30 | start = now.subtract(7, 'day').toISOString() |
| 31 | break |
| 32 | default: |
| 33 | start = now.subtract(1, 'hour').toISOString() |
| 34 | } |
| 35 | |
| 36 | return { startDate: start, endDate: end } |
| 37 | } |
| 38 | |
| 39 | export type RawChartData = { |
| 40 | timestamp: string | number |
| 41 | ok_count?: number | null |
| 42 | warning_count?: number | null |
| 43 | error_count?: number | null |
| 44 | [key: string]: string | number | null | undefined |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Transforms raw chart query results to LogsBarChartDatum format |
| 49 | */ |
| 50 | export const transformToBarChartData = (data: RawChartData[]): LogsBarChartDatum[] => { |
| 51 | return data.map((row) => ({ |
| 52 | timestamp: typeof row.timestamp === 'string' ? row.timestamp : String(row.timestamp), |
| 53 | ok_count: row.ok_count ?? 0, |
| 54 | warning_count: row.warning_count ?? 0, |
| 55 | error_count: row.error_count ?? 0, |
| 56 | })) |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Calculates health metrics from bar chart data |
| 61 | */ |
| 62 | export const calculateHealthMetrics = (eventChartData: LogsBarChartDatum[]) => { |
| 63 | const total = sumTotal(eventChartData) |
| 64 | const errorCount = sumErrors(eventChartData) |
| 65 | const warningCount = sumWarnings(eventChartData) |
| 66 | const okCount = total - errorCount - warningCount |
| 67 | const errorRate = total > 0 ? (errorCount / total) * 100 : 0 |
| 68 | const { successRate } = computeSuccessAndNonSuccessRates(total, warningCount, errorCount) |
| 69 | |
| 70 | return { |
| 71 | total, |
| 72 | errorRate, |
| 73 | successRate, |
| 74 | errorCount, |
| 75 | warningCount, |
| 76 | okCount, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Calculates aggregated metrics across all services |
| 82 | */ |
| 83 | export const calculateAggregatedMetrics = ( |
| 84 | services: { |
| 85 | total: number |
| 86 | errorCount: number |
| 87 | warningCount: number |
| 88 | }[] |
| 89 | ) => { |
| 90 | const totalRequests = services.reduce((sum, s) => sum + s.total, 0) |
| 91 | const totalErrors = services.reduce((sum, s) => sum + s.errorCount, 0) |
| 92 | const totalWarnings = services.reduce((sum, s) => sum + s.warningCount, 0) |
| 93 | |
| 94 | const { successRate: overallSuccessRate, nonSuccessRate: overallErrorRate } = |
| 95 | computeSuccessAndNonSuccessRates(totalRequests, totalWarnings, totalErrors) |
| 96 | |
| 97 | return { |
| 98 | totalRequests, |
| 99 | totalErrors, |
| 100 | totalWarnings, |
| 101 | overallErrorRate, |
| 102 | overallSuccessRate, |
| 103 | } |
| 104 | } |