Skip to content

Commit

Permalink
test(Metrics): ✅ Update unit tests for latency
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoval committed Oct 19, 2023
1 parent a92a001 commit 2e56d1c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/pages/shared/Metrics/Metrics.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface ConfigMetricFilters {
export enum QueriesMetrics {
GetTraffic = 'get-metric-traffic-query',
GetLatency = 'get-metric-latency-query',
GetLatencyBuckets = 'get-metric-latency-buckets',
GetRequest = 'get-metric-request-query',
GetResponse = 'get-metric-response-query'
}
Expand Down
4 changes: 3 additions & 1 deletion src/pages/shared/Metrics/__tests__/Latency.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ describe('Latency component', () => {
});

it('should render the Latency section and display the no metric found message', async () => {
jest.spyOn(ReactQuery, 'useQuery').mockImplementation(jest.fn().mockReturnValue({ data: null }));
jest
.spyOn(ReactQuery, 'useQueries')
.mockImplementation(jest.fn().mockReturnValue([{ data: null }, { data: null }]));

render(
<Wrapper>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/shared/Metrics/components/Latency.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const Latency: FC<LatencyProps> = function ({
keepPreviousData: true
},
{
queryKey: ['QueriesMetrics.GetLatencyBuckets', selectedFilters],
queryKey: [QueriesMetrics.GetLatencyBuckets, selectedFilters],
queryFn: () => MetricsController.getLatencyBuckets(selectedFilters),
refetchInterval,
keepPreviousData: true
Expand Down
16 changes: 11 additions & 5 deletions src/pages/shared/Metrics/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,24 @@ const MetricsController = {
};

try {
const buckets = (await PrometheusApi.fetchLatencyBuckets(params))
.map(({ values, metric }) => ({ values, metric: bucketLabels[metric.le] }))
const distributionBuckets = await PrometheusApi.fetchLatencyBuckets(params);

if (!distributionBuckets.length) {
return null;
}

const buckets = distributionBuckets
.map(({ values, metric }) => ({ values, metric: bucketLabels[metric?.le] }))
.sort((a, b) => a.metric.position - b.metric.position);

const bucketsNormalized = buckets?.map(({ metric, values }, index) => ({
data: [
{
x: metric.le,
x: metric?.le,
y: Number(values[values.length - 1][1]) - Number(buckets[index - 1]?.values[values.length - 1][1] || 0)
}
],
label: metric.le
label: metric?.le || ''
}));

const lastBucket = buckets[buckets.length - 1].values;
Expand All @@ -123,7 +129,7 @@ const MetricsController = {
const greaterThanCount = total - Number(values[values.length - 1][1]) || 0;
const greaterThanPerc = Math.round((greaterThanCount / (total || 1)) * 100);

return { bound: metric.le, lessThanCount, lessThanPerc, greaterThanCount, greaterThanPerc };
return { bound: metric?.le, lessThanCount, lessThanPerc, greaterThanCount, greaterThanPerc };
});

return { distribution: bucketsNormalized, summary };
Expand Down

0 comments on commit 2e56d1c

Please sign in to comment.