Skip to content

Commit

Permalink
[Synthetics] Simplify journey API query (#162188)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 authored Jul 19, 2023
1 parent 46403f1 commit 73695ef
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { SYNTHETICS_API_URLS } from '../../../../../common/constants';

export interface FetchJourneyStepsParams {
checkGroup: string;
syntheticEventTypes?: string[];
}

export async function fetchScreenshotBlockSet(params: string[]): Promise<ScreenshotBlockDoc[]> {
Expand All @@ -35,7 +34,7 @@ export async function fetchBrowserJourney(
): Promise<SyntheticsJourneyApiResponse> {
return apiService.get(
SYNTHETICS_API_URLS.JOURNEY.replace('{checkGroup}', params.checkGroup),
{ syntheticEventTypes: params.syntheticEventTypes },
undefined,
SyntheticsJourneyApiResponseType
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,10 @@
*/

import { JourneyStep } from '../../../../common/runtime_types/ping/synthetics';
import { getJourneySteps, formatSyntheticEvents } from './get_journey_steps';
import { getJourneySteps } from './get_journey_steps';
import { getUptimeESMockClient } from './test_helpers';

describe('getJourneySteps request module', () => {
describe('formatStepTypes', () => {
it('returns default steps if none are provided', () => {
expect(formatSyntheticEvents()).toMatchInlineSnapshot(`
Array [
"cmd/status",
"journey/browserconsole",
"step/end",
"step/screenshot",
"step/screenshot_ref",
]
`);
});

it('returns provided step array if isArray', () => {
expect(formatSyntheticEvents(['step/end', 'stderr'])).toMatchInlineSnapshot(`
Array [
"step/end",
"stderr",
]
`);
});

it('returns provided step string in an array', () => {
expect(formatSyntheticEvents('step/end')).toMatchInlineSnapshot(`
Array [
"step/end",
]
`);
});
});

describe('getJourneySteps', () => {
let data: any;
beforeEach(() => {
Expand Down Expand Up @@ -178,7 +147,6 @@ describe('getJourneySteps request module', () => {
const result: JourneyStep[] = await getJourneySteps({
uptimeEsClient,
checkGroup: '2bf952dc-64b5-11eb-8b3b-42010a84000d',
syntheticEventTypes: ['stderr', 'step/end'],
});

const call: any = mockEsClient.search.mock.calls[0][0];
Expand All @@ -188,8 +156,11 @@ describe('getJourneySteps request module', () => {
Object {
"terms": Object {
"synthetics.type": Array [
"stderr",
"cmd/status",
"journey/browserconsole",
"step/end",
"step/screenshot",
"step/screenshot_ref",
],
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,13 @@ import { JourneyStep } from '../../../../common/runtime_types/ping/synthetics';

export interface GetJourneyStepsParams {
checkGroup: string;
syntheticEventTypes?: string | string[];
}

const defaultEventTypes = [
'cmd/status',
'journey/browserconsole',
'step/end',
'step/screenshot',
'step/screenshot_ref',
];

export const formatSyntheticEvents = (eventTypes?: string | string[]) => {
if (!eventTypes) {
return defaultEventTypes;
} else {
return Array.isArray(eventTypes) ? eventTypes : [eventTypes];
}
};

type ResultType = JourneyStep & { '@timestamp': string };

export const getJourneySteps = async ({
uptimeEsClient,
checkGroup,
syntheticEventTypes,
}: GetJourneyStepsParams & {
uptimeEsClient: UptimeEsClient;
}): Promise<JourneyStep[]> => {
Expand All @@ -46,7 +28,13 @@ export const getJourneySteps = async ({
filter: [
{
terms: {
'synthetics.type': formatSyntheticEvents(syntheticEventTypes),
'synthetics.type': [
'cmd/status',
'journey/browserconsole',
'step/end',
'step/screenshot',
'step/screenshot_ref',
],
},
},
{
Expand Down
46 changes: 17 additions & 29 deletions x-pack/plugins/synthetics/server/routes/pings/journeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { schema } from '@kbn/config-schema';
import { SyntheticsJourneyApiResponse } from '../../../common/runtime_types';
import { getJourneyFailedSteps } from '../../legacy_uptime/lib/requests/get_journey_failed_steps';
import { SyntheticsRestApiRouteFactory } from '../types';
import { SYNTHETICS_API_URLS } from '../../../common/constants';
Expand All @@ -19,39 +20,26 @@ export const createJourneyRoute: SyntheticsRestApiRouteFactory = () => ({
params: schema.object({
checkGroup: schema.string(),
}),
query: schema.object({
// provides a filter for the types of synthetic events to include
// when fetching a journey's data
syntheticEventTypes: schema.maybe(
schema.oneOf([schema.arrayOf(schema.string()), schema.string()])
),
}),
},
handler: async ({ uptimeEsClient, request, response }): Promise<any> => {
handler: async ({ uptimeEsClient, request, response }): Promise<SyntheticsJourneyApiResponse> => {
const { checkGroup } = request.params;
const { syntheticEventTypes } = request.query;

try {
const [result, details] = await Promise.all([
await getJourneySteps({
uptimeEsClient,
checkGroup,
syntheticEventTypes,
}),
await getJourneyDetails({
uptimeEsClient,
checkGroup,
}),
]);

return {
const [steps, details] = await Promise.all([
getJourneySteps({
uptimeEsClient,
checkGroup,
steps: result,
details,
};
} catch (e: unknown) {
return response.custom({ statusCode: 500, body: { message: e } });
}
}),
getJourneyDetails({
uptimeEsClient,
checkGroup,
}),
]);

return {
steps,
details,
checkGroup,
};
},
});

Expand Down

0 comments on commit 73695ef

Please sign in to comment.