[7.12] [Uptime] Update query for ping histogram (#95495) (#96243)

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

* fix type

* fix types
This commit is contained in:
Shahzad 2021-04-06 11:47:55 +02:00 committed by GitHub
parent 2ab9a89294
commit fde0fe31df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 58 deletions

View file

@ -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;
};
}

View file

@ -76,7 +76,7 @@ Object {
"y": 1,
},
Object {
"downCount": undefined,
"downCount": 0,
"upCount": 2,
"x": 2,
"y": 1,

View file

@ -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,
@ -53,10 +53,10 @@ describe('getPingHistogram', () => {
{
key: 1,
up: {
doc_count: 2,
value: 2,
},
down: {
doc_count: 1,
value: 1,
},
},
],
@ -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);
@ -89,8 +89,8 @@ describe('getPingHistogram', () => {
const result = await getPingHistogram({
uptimeEsClient,
from: 'now-15m',
to: 'now',
dateStart: 'now-15m',
dateEnd: 'now',
filters: '',
});
@ -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,
},
},
],
@ -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,
});
@ -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,
},
},
],
@ -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,
});

View file

@ -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 */
@ -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) {
@ -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,
@ -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',
},
},
},
@ -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,

View file

@ -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,