Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Uptime] Rename conversion function, extract to module scope and add tests #89018

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have never in my life seen this underscore thing. Is that just syntactic sugar for formatting numbers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'tis. Isn't it freaky? Someone on Twitter told me about it the other day.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS @dominiqueclarke you only commented, not approved. Please ✅ if still LGTY.

});
});

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,
NetworkEvent[]
Expand All @@ -34,14 +38,12 @@ export const getNetworkEvents: UMElasticsearchQueryFn<

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

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

return 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