forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Synthetics] Update pings query to not use last 30 days time range !! (…
…elastic#208366) ## Summary Fixes elastic#198443 !! Instead of using last 30 days, it will now uses last 24 hours otherwise it will fall back to last 7 days and then last 30 days. Last run should still work as expected <img width="1728" alt="image" src="https://github.com/user-attachments/assets/abad5d83-5456-440e-b5b5-147915315120" /> --------- Co-authored-by: Justin Kambic <[email protected]>
- Loading branch information
1 parent
cd9096c
commit 97e8bad
Showing
12 changed files
with
130 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
x-pack/solutions/observability/plugins/synthetics/server/queries/get_latest_test_run.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* 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 { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; | ||
import { Ping } from '../../common/runtime_types'; | ||
import { SyntheticsEsClient } from '../lib'; | ||
import { getRangeFilter, SUMMARY_FILTER } from '../../common/constants/client_defaults'; | ||
|
||
export async function getLatestTestRun<F>({ | ||
syntheticsEsClient, | ||
monitorId, | ||
locationLabel, | ||
from = 'now-1d', | ||
to = 'now', | ||
}: { | ||
syntheticsEsClient: SyntheticsEsClient; | ||
monitorId: string; | ||
locationLabel?: string; | ||
from?: string; | ||
to?: string; | ||
}): Promise<Ping | undefined> { | ||
const response = await syntheticsEsClient.search({ | ||
body: { | ||
query: { | ||
bool: { | ||
filter: [ | ||
SUMMARY_FILTER, | ||
getRangeFilter({ from, to }), | ||
{ term: { 'monitor.id': monitorId } }, | ||
...(locationLabel ? [{ term: { 'observer.geo.name': locationLabel } }] : []), | ||
] as QueryDslQueryContainer[], | ||
}, | ||
}, | ||
sort: [{ '@timestamp': { order: 'desc' } }], | ||
}, | ||
}); | ||
|
||
return response.body.hits.hits[0]?._source as Ping | undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
x-pack/solutions/observability/plugins/synthetics/server/routes/pings/get_latest_test_run.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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 { schema, TypeOf } from '@kbn/config-schema'; | ||
import { Ping } from '../../../common/runtime_types'; | ||
import { getLatestTestRun } from '../../queries/get_latest_test_run'; | ||
import { SyntheticsRestApiRouteFactory } from '../types'; | ||
import { SYNTHETICS_API_URLS } from '../../../common/constants'; | ||
|
||
export const getLatestTestRunRouteQuerySchema = schema.object({ | ||
from: schema.maybe(schema.string()), | ||
to: schema.maybe(schema.string()), | ||
locationLabel: schema.maybe(schema.string()), | ||
monitorId: schema.string(), | ||
}); | ||
|
||
type GetPingsRouteRequest = TypeOf<typeof getLatestTestRunRouteQuerySchema>; | ||
|
||
export const syntheticsGetLatestTestRunRoute: SyntheticsRestApiRouteFactory = () => ({ | ||
method: 'GET', | ||
path: SYNTHETICS_API_URLS.LATEST_TEST_RUN, | ||
validate: {}, | ||
validation: { | ||
request: { | ||
query: getLatestTestRunRouteQuerySchema, | ||
}, | ||
}, | ||
handler: async ({ syntheticsEsClient, request, response }): Promise<{ ping?: Ping }> => { | ||
const { from, to, monitorId, locationLabel } = request.query as GetPingsRouteRequest; | ||
|
||
const getPing = (fromVal: string) => { | ||
return getLatestTestRun({ | ||
syntheticsEsClient, | ||
from: fromVal, | ||
to: to || 'now', | ||
monitorId, | ||
locationLabel, | ||
}); | ||
}; | ||
|
||
// we will try to get the latest ping from the last day, | ||
// if it doesn't exist we will try to get the latest ping from the last week | ||
const ping = await getPing(from || 'now-1d'); | ||
|
||
// if from is provided, we will only try to get the latest ping from the provided time range | ||
if (ping && from) { | ||
return { ping }; | ||
} | ||
// fall back to 1w and then max 30 days | ||
return { ping: (await getPing('now-1w')) || (await getPing('now-30d')) }; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters