Skip to content

Commit

Permalink
[Uptime] Fix: Last successful screenshot should be from same location (
Browse files Browse the repository at this point in the history
…#116906)

* update get_last_successful_step query to include location logic

* adjust types
  • Loading branch information
dominiqueclarke authored Nov 2, 2021
1 parent 39d79f7 commit 4e294e3
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 6 deletions.
5 changes: 5 additions & 0 deletions x-pack/plugins/uptime/common/runtime_types/ping/synthetics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export const JourneyStepType = t.intersection([
lt: t.string,
}),
}),
observer: t.type({
geo: t.type({
name: t.string,
}),
}),
synthetics: t.partial({
error: t.partial({
message: t.string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const StepScreenshots = ({ step }: Props) => {
timestamp: step['@timestamp'],
monitorId: step.monitor.id,
stepIndex: step.synthetics?.step?.index!,
location: step.observer?.geo?.name,
});
}
}, [step._id, step['@timestamp']]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ const browserConsoleStep = {
_id: 'IvT1oXwB5ds00bB_FVXP',
observer: {
hostname: '16Elastic',
geo: {
name: 'au-heartbeat',
},
},
agent: {
name: '16Elastic',
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/uptime/public/state/api/journey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,20 @@ export async function fetchLastSuccessfulStep({
monitorId,
timestamp,
stepIndex,
location,
}: {
monitorId: string;
timestamp: string;
stepIndex: number;
location?: string;
}): Promise<JourneyStep> {
return await apiService.get(
`/api/uptime/synthetics/step/success/`,
{
monitorId,
timestamp,
stepIndex,
location,
},
JourneyStepType
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { getLastSuccessfulStepParams } from './get_last_successful_step';

describe('getLastSuccessfulStep', () => {
describe('getLastSuccessfulStepParams', () => {
it('formats ES params with location', () => {
const monitorId = 'my-monitor';
const stepIndex = 1;
const location = 'au-heartbeat';
const timestamp = '2021-10-31T19:47:52.392Z';
const params = getLastSuccessfulStepParams({
monitorId,
stepIndex,
location,
timestamp,
});

expect(params).toEqual({
query: {
bool: {
filter: [
{
range: {
'@timestamp': {
lte: '2021-10-31T19:47:52.392Z',
},
},
},
{
term: {
'monitor.id': monitorId,
},
},
{
term: {
'synthetics.type': 'step/end',
},
},
{
term: {
'synthetics.step.status': 'succeeded',
},
},
{
term: {
'synthetics.step.index': stepIndex,
},
},
{
term: {
'observer.geo.name': location,
},
},
],
},
},
size: 1,
sort: [
{
'@timestamp': {
order: 'desc',
},
},
],
});
});

it('formats ES params without location', () => {
const params = getLastSuccessfulStepParams({
monitorId: 'my-monitor',
stepIndex: 1,
location: undefined,
timestamp: '2021-10-31T19:47:52.392Z',
});

expect(params).toEqual({
query: {
bool: {
filter: [
{
range: {
'@timestamp': {
lte: '2021-10-31T19:47:52.392Z',
},
},
},
{
term: {
'monitor.id': 'my-monitor',
},
},
{
term: {
'synthetics.type': 'step/end',
},
},
{
term: {
'synthetics.step.status': 'succeeded',
},
},
{
term: {
'synthetics.step.index': 1,
},
},
],
must_not: {
exists: {
field: 'observer.geo.name',
},
},
},
},
size: 1,
sort: [
{
'@timestamp': {
order: 'desc',
},
},
],
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ export interface GetStepScreenshotParams {
monitorId: string;
timestamp: string;
stepIndex: number;
location?: string;
}

export const getStepLastSuccessfulStep: UMElasticsearchQueryFn<
GetStepScreenshotParams,
JourneyStep | null
> = async ({ uptimeEsClient, monitorId, stepIndex, timestamp }) => {
const lastSuccessCheckParams: estypes.SearchRequest['body'] = {
export const getLastSuccessfulStepParams = ({
monitorId,
stepIndex,
timestamp,
location,
}: GetStepScreenshotParams): estypes.SearchRequest['body'] => {
return {
size: 1,
sort: [
{
Expand Down Expand Up @@ -58,10 +61,40 @@ export const getStepLastSuccessfulStep: UMElasticsearchQueryFn<
'synthetics.step.index': stepIndex,
},
},
...(location
? [
{
term: {
'observer.geo.name': location,
},
},
]
: []),
],
...(!location
? {
must_not: {
exists: {
field: 'observer.geo.name',
},
},
}
: {}),
},
},
};
};

export const getStepLastSuccessfulStep: UMElasticsearchQueryFn<
GetStepScreenshotParams,
JourneyStep | null
> = async ({ uptimeEsClient, monitorId, stepIndex, timestamp, location }) => {
const lastSuccessCheckParams = getLastSuccessfulStepParams({
monitorId,
stepIndex,
timestamp,
location,
});

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ export const createLastSuccessfulStepRoute: UMRestApiRouteFactory = (libs: UMSer
monitorId: schema.string(),
stepIndex: schema.number(),
timestamp: schema.string(),
location: schema.maybe(schema.string()),
}),
},
handler: async ({ uptimeEsClient, request, response }) => {
const { timestamp, monitorId, stepIndex } = request.query;
const { timestamp, monitorId, stepIndex, location } = request.query;

const step: JourneyStep | null = await libs.requests.getStepLastSuccessfulStep({
uptimeEsClient,
monitorId,
stepIndex,
timestamp,
location,
});

if (step === null) {
Expand Down

0 comments on commit 4e294e3

Please sign in to comment.