Skip to content

Commit

Permalink
Initial work on uptime homepage API (#70135)
Browse files Browse the repository at this point in the history
Co-authored-by: Shahzad <shahzad31comp@gmail.com>
  • Loading branch information
andrewvc and shahzad31 authored Jul 1, 2020
1 parent 5c98b11 commit e8cf08f
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface GetPingHistogramParams {
dateEnd: string;
filters?: string;
monitorId?: string;
bucketSize?: string;
}

export interface HistogramResult {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/uptime/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"configPath": ["xpack", "uptime"],
"id": "uptime",
"kibanaVersion": "kibana",
"optionalPlugins": ["capabilities", "data", "home"],
"optionalPlugins": ["capabilities", "data", "home", "observability"],
"requiredPlugins": [
"alerts",
"embeddable",
Expand Down
13 changes: 13 additions & 0 deletions x-pack/plugins/uptime/public/apps/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ import {
} from '../../../../../src/plugins/data/public';
import { alertTypeInitializers } from '../lib/alert_types';
import { kibanaService } from '../state/kibana_service';
import { fetchIndexStatus } from '../state/api';
import { ObservabilityPluginSetup } from '../../../observability/public';
import { fetchUptimeOverviewData } from './uptime_overview_fetcher';

export interface ClientPluginsSetup {
data: DataPublicPluginSetup;
home: HomePublicPluginSetup;
observability: ObservabilityPluginSetup;
triggers_actions_ui: TriggersAndActionsUIPublicPluginSetup;
}

Expand Down Expand Up @@ -63,6 +67,15 @@ export class UptimePlugin
});
}

plugins.observability.dashboard.register({
appName: 'uptime',
hasData: async () => {
const status = await fetchIndexStatus();
return status.docCount > 0;
},
fetchData: fetchUptimeOverviewData,
});

core.application.register({
appRoute: '/app/uptime#/',
id: PLUGIN.ID,
Expand Down
62 changes: 62 additions & 0 deletions x-pack/plugins/uptime/public/apps/uptime_overview_fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { fetchPingHistogram, fetchSnapshotCount } from '../state/api';
import { UptimeFetchDataResponse } from '../../../observability/public/typings/fetch_data_response';

export async function fetchUptimeOverviewData({
startTime,
endTime,
bucketSize,
}: {
startTime: string;
endTime: string;
bucketSize: string;
}) {
const snapshot = await fetchSnapshotCount({
dateRangeStart: startTime,
dateRangeEnd: endTime,
});

const pings = await fetchPingHistogram({ dateStart: startTime, dateEnd: endTime, bucketSize });

const response: UptimeFetchDataResponse = {
title: 'Uptime',
appLink: '/app/uptime#/',
stats: {
monitors: {
type: 'number',
label: 'Monitors',
value: snapshot.total,
},
up: {
type: 'number',
label: 'Up',
value: snapshot.up,
},
down: {
type: 'number',
label: 'Down',
value: snapshot.down,
},
},
series: {
up: {
label: 'Up',
coordinates: pings.histogram.map((p) => {
return { x: p.x!, y: p.upCount || 0 };
}),
},
down: {
label: 'Down',
coordinates: pings.histogram.map((p) => {
return { x: p.x!, y: p.downCount || 0 };
}),
},
},
};
return response;
}
2 changes: 2 additions & 0 deletions x-pack/plugins/uptime/public/state/api/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ export const fetchPingHistogram: APIFn<GetPingHistogramParams, HistogramResult>
dateStart,
dateEnd,
filters,
bucketSize,
}) => {
const queryParams = {
dateStart,
dateEnd,
monitorId,
filters,
bucketSize,
};

return await apiService.get(API_URLS.PING_HISTOGRAM, queryParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ describe('getPingHistogram', () => {
dynamicSettings: DYNAMIC_SETTINGS_DEFAULTS,
from: 'now-15m',
to: 'now',
filters: null,
});

expect(mockEsClient).toHaveBeenCalledTimes(1);
Expand All @@ -81,7 +80,7 @@ describe('getPingHistogram', () => {
dynamicSettings: DYNAMIC_SETTINGS_DEFAULTS,
from: 'now-15m',
to: 'now',
filters: null,
filters: '',
});

expect(mockEsClient).toHaveBeenCalledTimes(1);
Expand Down
29 changes: 22 additions & 7 deletions x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ export interface GetPingHistogramParams {
/** @member dateRangeEnd timestamp bounds */
to: string;
/** @member filters user-defined filters */
filters?: string | null;
filters?: string;
/** @member monitorId optional limit to monitorId */
monitorId?: string | null;
monitorId?: string;

bucketSize?: string;
}

export const getPingHistogram: UMElasticsearchQueryFn<
GetPingHistogramParams,
HistogramResult
> = async ({ callES, dynamicSettings, from, to, filters, monitorId }) => {
> = async ({ callES, dynamicSettings, from, to, filters, monitorId, bucketSize }) => {
const boolFilters = filters ? JSON.parse(filters) : null;
const additionalFilters = [];
if (monitorId) {
Expand All @@ -34,6 +36,22 @@ export const getPingHistogram: UMElasticsearchQueryFn<
}
const filter = getFilterClause(from, to, additionalFilters);

const seriesHistogram: any = {};

if (bucketSize) {
seriesHistogram.date_histogram = {
field: '@timestamp',
fixed_interval: bucketSize,
missing: 0,
};
} else {
seriesHistogram.auto_date_histogram = {
field: '@timestamp',
buckets: QUERY.DEFAULT_BUCKET_COUNT,
missing: 0,
};
}

const params = {
index: dynamicSettings.heartbeatIndices,
body: {
Expand All @@ -45,10 +63,7 @@ export const getPingHistogram: UMElasticsearchQueryFn<
size: 0,
aggs: {
timeseries: {
auto_date_histogram: {
field: '@timestamp',
buckets: QUERY.DEFAULT_BUCKET_COUNT,
},
...seriesHistogram,
aggs: {
down: {
filter: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ export const createGetPingHistogramRoute: UMRestApiRouteFactory = (libs: UMServe
dateEnd: schema.string(),
monitorId: schema.maybe(schema.string()),
filters: schema.maybe(schema.string()),
bucketSize: schema.maybe(schema.string()),
}),
},
handler: async ({ callES, dynamicSettings }, _context, request, response): Promise<any> => {
const { dateStart, dateEnd, monitorId, filters } = request.query;
const { dateStart, dateEnd, monitorId, filters, bucketSize } = request.query;

const result = await libs.requests.getPingHistogram({
callES,
Expand All @@ -30,6 +31,7 @@ export const createGetPingHistogramRoute: UMRestApiRouteFactory = (libs: UMServe
to: dateEnd,
monitorId,
filters,
bucketSize,
});

return response.ok({
Expand Down

0 comments on commit e8cf08f

Please sign in to comment.