Skip to content

Commit

Permalink
[7.12] [Uptime] Update query for ping histogram (elastic#95495) (elas…
Browse files Browse the repository at this point in the history
…tic#96243)

* [Uptime] Update query for ping histogram (elastic#95495)

* fix type

* fix types
  • Loading branch information
shahzad31 authored Apr 6, 2021
1 parent 2ab9a89 commit fde0fe3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 58 deletions.
12 changes: 0 additions & 12 deletions x-pack/plugins/uptime/common/runtime_types/ping/histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,3 @@ export interface HistogramResult {
histogram: HistogramDataPoint[];
minInterval: number;
}

export interface HistogramQueryResult {
key: number;
key_as_string: string;
doc_count: number;
down: {
doc_count: number;
};
up: {
doc_count: number;
};
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ describe('getPingHistogram', () => {
{
key: 1,
up: {
doc_count: 2,
value: 2,
},
down: {
doc_count: 1,
value: 1,
},
},
{
key: 2,
up: {
doc_count: 2,
value: 2,
},
down: {
bucket_count: 1,
Expand All @@ -53,10 +53,10 @@ describe('getPingHistogram', () => {
{
key: 1,
up: {
doc_count: 2,
value: 2,
},
down: {
doc_count: 1,
value: 1,
},
},
],
Expand All @@ -68,8 +68,8 @@ describe('getPingHistogram', () => {

const result = await getPingHistogram({
uptimeEsClient,
from: 'now-15m',
to: 'now',
dateStart: 'now-15m',
dateEnd: 'now',
});

expect(mockEsClient.search).toHaveBeenCalledTimes(1);
Expand All @@ -89,8 +89,8 @@ describe('getPingHistogram', () => {

const result = await getPingHistogram({
uptimeEsClient,
from: 'now-15m',
to: 'now',
dateStart: 'now-15m',
dateEnd: 'now',
filters: '',
});

Expand All @@ -111,28 +111,28 @@ describe('getPingHistogram', () => {
{
key: 1,
up: {
doc_count: 2,
value: 2,
},
down: {
doc_count: 1,
value: 1,
},
},
{
key: 2,
up: {
doc_count: 2,
value: 2,
},
down: {
doc_count: 2,
value: 2,
},
},
{
key: 3,
up: {
doc_count: 3,
value: 3,
},
down: {
doc_count: 1,
value: 1,
},
},
],
Expand All @@ -153,8 +153,8 @@ describe('getPingHistogram', () => {

const result = await getPingHistogram({
uptimeEsClient,
from: 'now-15m',
to: 'now',
dateStart: 'now-15m',
dateEnd: 'now',
filters: JSON.stringify(searchFilter),
monitorId: undefined,
});
Expand All @@ -175,28 +175,28 @@ describe('getPingHistogram', () => {
{
key: 1,
up: {
doc_count: 2,
value: 2,
},
down: {
doc_count: 1,
value: 1,
},
},
{
key: 2,
up: {
doc_count: 1,
value: 1,
},
down: {
doc_count: 2,
value: 2,
},
},
{
key: 3,
up: {
doc_count: 3,
value: 3,
},
down: {
doc_count: 1,
value: 1,
},
},
],
Expand All @@ -209,8 +209,8 @@ describe('getPingHistogram', () => {
const filters = `{"bool":{"must":[{"simple_query_string":{"query":"http"}}]}}`;
const result = await getPingHistogram({
uptimeEsClient,
from: 'now-15m',
to: 'now',
dateStart: 'now-15m',
dateEnd: 'now',
filters,
});

Expand Down
39 changes: 21 additions & 18 deletions x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
*/

import { getFilterClause } from '../helper';
import { HistogramResult, HistogramQueryResult } from '../../../common/runtime_types';
import { HistogramResult } from '../../../common/runtime_types';
import { QUERY } from '../../../common/constants';
import { getHistogramInterval } from '../helper/get_histogram_interval';
import { UMElasticsearchQueryFn } from '../adapters/framework';

export interface GetPingHistogramParams {
/** @member dateRangeStart timestamp bounds */
from: string;
dateStart: string;
/** @member dateRangeEnd timestamp bounds */
to: string;
dateEnd: string;
/** @member filters user-defined filters */
filters?: string;
/** @member monitorId optional limit to monitorId */
Expand All @@ -27,7 +27,7 @@ export interface GetPingHistogramParams {
export const getPingHistogram: UMElasticsearchQueryFn<
GetPingHistogramParams,
HistogramResult
> = async ({ uptimeEsClient, from, to, filters, monitorId, bucketSize }) => {
> = async ({ uptimeEsClient, dateStart, dateEnd, filters, monitorId, bucketSize }) => {
const boolFilters = filters ? JSON.parse(filters) : null;
const additionalFilters = [];
if (monitorId) {
Expand All @@ -36,14 +36,21 @@ export const getPingHistogram: UMElasticsearchQueryFn<
if (boolFilters) {
additionalFilters.push(boolFilters);
}
const filter = getFilterClause(from, to, additionalFilters);
const filter = getFilterClause(dateStart, dateEnd, additionalFilters);

const minInterval = getHistogramInterval(from, to, QUERY.DEFAULT_BUCKET_COUNT);
const minInterval = getHistogramInterval(dateStart, dateEnd, QUERY.DEFAULT_BUCKET_COUNT);

const params = {
query: {
bool: {
filter,
filter: [
...filter,
{
exists: {
field: 'summary',
},
},
],
},
},
size: 0,
Expand All @@ -56,17 +63,13 @@ export const getPingHistogram: UMElasticsearchQueryFn<
},
aggs: {
down: {
filter: {
term: {
'monitor.status': 'down',
},
sum: {
field: 'summary.down',
},
},
up: {
filter: {
term: {
'monitor.status': 'up',
},
sum: {
field: 'summary.up',
},
},
},
Expand All @@ -75,11 +78,11 @@ export const getPingHistogram: UMElasticsearchQueryFn<
};

const { body: result } = await uptimeEsClient.search({ body: params });
const buckets: HistogramQueryResult[] = result?.aggregations?.timeseries?.buckets ?? [];
const buckets = result?.aggregations?.timeseries?.buckets ?? [];
const histogram = buckets.map((bucket) => {
const x: number = bucket.key;
const downCount: number = bucket.down.doc_count;
const upCount: number = bucket.up.doc_count;
const downCount = bucket.down.value || 0;
const upCount = bucket.up.value || 0;
return {
x,
downCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export const createGetPingHistogramRoute: UMRestApiRouteFactory = (libs: UMServe

return await libs.requests.getPingHistogram({
uptimeEsClient,
from: dateStart,
to: dateEnd,
dateStart,
dateEnd,
monitorId,
filters,
bucketSize,
Expand Down

0 comments on commit fde0fe3

Please sign in to comment.