Skip to content

Commit

Permalink
Rename conversion function, extract to module scope and add tests. (#…
Browse files Browse the repository at this point in the history
…89018)

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
justinkambic and kibanamachine committed Jan 27, 2021
1 parent d801a7b commit 696466a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
*/

import { getUptimeESMockClient } from './helper';
import { getNetworkEvents } from './get_network_events';
import { getNetworkEvents, secondsToMillis } from './get_network_events';

describe('getNetworkEvents', () => {
describe('secondsToMillis conversion', () => {
it('returns -1 for -1 value', () => {
expect(secondsToMillis(-1)).toBe(-1);
});

it('returns a value of seconds as milliseconds', () => {
expect(secondsToMillis(10)).toBe(10_000);
});
});

let mockHits: any;

beforeEach(() => {
Expand Down
12 changes: 7 additions & 5 deletions x-pack/plugins/uptime/server/lib/requests/get_network_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ interface GetNetworkEventsParams {
stepIndex: string;
}

export const secondsToMillis = (seconds: number) =>
// -1 is a special case where a value was unavailable
seconds === -1 ? -1 : seconds * 1000;

export const getNetworkEvents: UMElasticsearchQueryFn<
GetNetworkEventsParams,
{ events: NetworkEvent[]; total: number }
Expand All @@ -35,17 +39,15 @@ export const getNetworkEvents: UMElasticsearchQueryFn<

const { body: result } = await uptimeEsClient.search({ body: params });

const microToMillis = (micro: number): number => (micro === -1 ? -1 : micro * 1000);

return {
total: result.hits.total.value,
events: result.hits.hits.map<NetworkEvent>((event: any) => {
const requestSentTime = microToMillis(event._source.synthetics.payload.request_sent_time);
const loadEndTime = microToMillis(event._source.synthetics.payload.load_end_time);
const requestSentTime = secondsToMillis(event._source.synthetics.payload.request_sent_time);
const loadEndTime = secondsToMillis(event._source.synthetics.payload.load_end_time);
const requestStartTime =
event._source.synthetics.payload.response &&
event._source.synthetics.payload.response.timing
? microToMillis(event._source.synthetics.payload.response.timing.request_time)
? secondsToMillis(event._source.synthetics.payload.response.timing.request_time)
: undefined;

return {
Expand Down

0 comments on commit 696466a

Please sign in to comment.