Skip to content

Commit

Permalink
Implemented better fix for Uptime type issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
justinkambic committed Sep 1, 2021
1 parent c569d3e commit a6c8e07
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,42 @@ describe('getPingHistogram', () => {
expect(mockEsClient.search).toHaveBeenCalledTimes(1);
expect(result).toMatchSnapshot();
});

it('returns an empty array if agg buckets are undefined', async () => {
const { esClient: mockEsClient, uptimeEsClient } = getUptimeESMockClient();

mockEsClient.search.mockResolvedValueOnce({
body: {
aggregations: {
timeseries: {
buckets: undefined,
interval: '1m',
},
},
},
} as any);

const result = await getPingHistogram({ uptimeEsClient, dateStart: 'now-15m', dateEnd: 'now' });

expect(result.histogram).toEqual([]);
});

it('returns an empty array if agg buckets are empty', async () => {
const { esClient: mockEsClient, uptimeEsClient } = getUptimeESMockClient();

mockEsClient.search.mockResolvedValueOnce({
body: {
aggregations: {
timeseries: {
buckets: [],
interval: '1m',
},
},
},
} as any);

const result = await getPingHistogram({ uptimeEsClient, dateStart: 'now-15m', dateEnd: 'now' });

expect(result.histogram).toEqual([]);
});
});
12 changes: 6 additions & 6 deletions x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ export const getPingHistogram: UMElasticsearchQueryFn<
});

const { body: result } = await uptimeEsClient.search(params);
const buckets: Array<{
key: number;
down: { value: number | null };
up: { value: number | null };
}> = result?.aggregations?.timeseries?.buckets ?? [];

const histogram = buckets.map((bucket) => {
const buckets = result?.aggregations?.timeseries?.buckets ?? [];
if (typeof buckets === 'undefined' || buckets.length === 0) {
return { histogram: [], minInterval };
}

const histogram = buckets.map((bucket: Pick<typeof buckets[0], 'key' | 'down' | 'up'>) => {
const x: number = bucket.key;
const downCount = bucket.down.value || 0;
const upCount = bucket.up.value || 0;
Expand Down

0 comments on commit a6c8e07

Please sign in to comment.