Skip to content

Commit

Permalink
[Uptime] Improve Telemetry test (elastic#62428)
Browse files Browse the repository at this point in the history
* removed unnecessary filter

* update condition

* added a unit test for mix state

* fix types

* fix type

* updated test

* update

* updates test

* updates tests

* updates tests

* updated type

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
shahzad31 and elasticmachine committed Apr 7, 2020
1 parent 80e0b67 commit 280690c
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 37 deletions.
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/uptime/common/constants/rest_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export enum API_URLS {
PING_HISTOGRAM = `/api/uptime/ping/histogram`,
SNAPSHOT_COUNT = `/api/uptime/snapshot/count`,
FILTERS = `/api/uptime/filters`,
logPageView = `/api/uptime/logPageView`,
LOG_PAGE_VIEW = `/api/uptime/log_page_view`,

ML_MODULE_JOBS = `/api/ml/modules/jobs_exist/`,
ML_SETUP_MODULE = '/api/ml/modules/setup/',
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/uptime/public/hooks/use_telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ export const useUptimeTelemetry = (page?: UptimePage) => {
dateEnd: dateRangeEnd,
autoRefreshEnabled: !autorefreshIsPaused,
};
apiService.post(API_URLS.logPageView, params);
apiService.post(API_URLS.LOG_PAGE_VIEW, params);
}, [autorefreshInterval, autorefreshIsPaused, dateRangeEnd, dateRangeStart, page]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import moment from 'moment';
import { ISavedObjectsRepository } from 'kibana/server';
import { ISavedObjectsRepository, SavedObjectsClientContract } from 'kibana/server';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { PageViewParams, UptimeTelemetry } from './types';
import { APICaller } from '../framework';
Expand Down Expand Up @@ -54,6 +54,9 @@ export class KibanaTelemetryAdapter {
}

public static countPageView(pageView: PageViewParams) {
if (pageView.refreshTelemetryHistory) {
this.collector = {};
}
const bucketId = this.getBucketToIncrement();
const bucket = this.collector[bucketId];
if (pageView.page === 'Overview') {
Expand Down Expand Up @@ -94,7 +97,7 @@ export class KibanaTelemetryAdapter {

public static async countNoOfUniqueMonitorAndLocations(
callCluster: APICaller,
savedObjectsClient: ISavedObjectsRepository
savedObjectsClient: ISavedObjectsRepository | SavedObjectsClientContract
) {
const dynamicSettings = await savedObjectsAdapter.getUptimeDynamicSettings(savedObjectsClient);
const params = {
Expand Down Expand Up @@ -161,24 +164,27 @@ export class KibanaTelemetryAdapter {
const monitorNameStats: any = result?.aggregations?.monitor_name;
const locationNameStats: any = result?.aggregations?.observer_loc_name;
const uniqueMonitors: any = result?.aggregations?.monitors.buckets;
const bucket = this.getBucketToIncrement();

this.collector[bucket].no_of_unique_monitors = numberOfUniqueMonitors;
this.collector[bucket].no_of_unique_observer_locations = numberOfUniqueLocations;
this.collector[bucket].no_of_unique_observer_locations = numberOfUniqueLocations;
this.collector[bucket].monitor_name_stats = {
const bucketId = this.getBucketToIncrement();
const bucket = this.collector[bucketId];

bucket.no_of_unique_monitors = numberOfUniqueMonitors;
bucket.no_of_unique_observer_locations = numberOfUniqueLocations;
bucket.no_of_unique_observer_locations = numberOfUniqueLocations;
bucket.monitor_name_stats = {
min_length: monitorNameStats?.min_length ?? 0,
max_length: monitorNameStats?.max_length ?? 0,
avg_length: +monitorNameStats?.avg_length.toFixed(2),
avg_length: +(monitorNameStats?.avg_length?.toFixed(2) ?? 0),
};

this.collector[bucket].observer_location_name_stats = {
bucket.observer_location_name_stats = {
min_length: locationNameStats?.min_length ?? 0,
max_length: locationNameStats?.max_length ?? 0,
avg_length: +(locationNameStats?.avg_length?.toFixed(2) ?? 0),
};

this.collector[bucket].monitor_frequency = this.getMonitorsFrequency(uniqueMonitors);
bucket.monitor_frequency = this.getMonitorsFrequency(uniqueMonitors);
return bucket;
}

private static getMonitorsFrequency(uniqueMonitors = []) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface PageViewParams {
dateEnd: string;
autoRefreshEnabled: boolean;
autorefreshInterval: number;
refreshTelemetryHistory?: boolean;
}

export interface Stats {
Expand Down
17 changes: 13 additions & 4 deletions x-pack/plugins/uptime/server/rest_api/telemetry/log_page_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,32 @@ import { schema } from '@kbn/config-schema';
import { KibanaTelemetryAdapter } from '../../lib/adapters/telemetry';
import { UMRestApiRouteFactory } from '../types';
import { PageViewParams } from '../../lib/adapters/telemetry/types';
import { API_URLS } from '../../../../../legacy/plugins/uptime/common/constants';

export const createLogPageViewRoute: UMRestApiRouteFactory = () => ({
method: 'POST',
path: '/api/uptime/logPageView',
path: API_URLS.LOG_PAGE_VIEW,
validate: {
body: schema.object({
page: schema.string(),
dateStart: schema.string(),
dateEnd: schema.string(),
autoRefreshEnabled: schema.boolean(),
autorefreshInterval: schema.number(),
refreshTelemetryHistory: schema.maybe(schema.boolean()),
}),
},
handler: async ({ callES, dynamicSettings }, _context, _request, response): Promise<any> => {
const result = KibanaTelemetryAdapter.countPageView(_request.body as PageViewParams);
handler: async (
{ savedObjectsClient, callES, dynamicSettings },
_context,
request,
response
): Promise<any> => {
await KibanaTelemetryAdapter.countNoOfUniqueMonitorAndLocations(callES, savedObjectsClient);
const pageViewResult = KibanaTelemetryAdapter.countPageView(request.body as PageViewParams);

return response.ok({
body: result,
body: pageViewResult,
});
},
options: {
Expand Down
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/uptime/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default function({ getService, loadTestFile }: FtrProviderContext) {

loadTestFile(require.resolve('./snapshot'));
loadTestFile(require.resolve('./dynamic_settings'));
loadTestFile(require.resolve('./telemetry_collectors'));
});
describe('with real-world data', () => {
before('load heartbeat data', async () => await esArchiver.load('uptime/full_heartbeat'));
Expand Down
129 changes: 129 additions & 0 deletions x-pack/test/api_integration/apis/uptime/rest/telemetry_collectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* 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 expect from '@kbn/expect';
import { FtrProviderContext } from '../../../ftr_provider_context';
import { API_URLS } from '../../../../../legacy/plugins/uptime/common/constants';
import { makeChecksWithStatus } from '../graphql/helpers/make_checks';

export default function({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const es = getService('legacyEs');

describe('telemetry collectors', () => {
before('generating data', async () => {
await getService('esArchiver').load('uptime/blank');

const observer = {
geo: {
name: 'US-East',
location: '40.7128, -74.0060',
},
};

const observer2 = {
geo: {
name: 'US',
location: '40.7128, -74.0060',
},
};

await makeChecksWithStatus(
es,
'upMonitorId',
1,
1,
60 * 1000,
{
observer: {},
monitor: {
name: 'Elastic',
},
},
'up'
);

await makeChecksWithStatus(
es,
'downMonitorId',
1,
1,
120 * 1000,
{
observer,
monitor: {
name: 'Long Name with 22 Char',
},
},
'down'
);

await makeChecksWithStatus(es, 'noGeoNameMonitor', 1, 1, 60 * 1000, { observer: {} }, 'down');
await makeChecksWithStatus(
es,
'downMonitorId',
1,
1,
1,
{
observer,
monitor: {
name: 'Elastic',
},
},
'down'
);

await makeChecksWithStatus(es, 'mixMonitorId', 1, 1, 1, { observer: observer2 }, 'down');
});

after('unload heartbeat index', () => getService('esArchiver').unload('uptime/blank'));

it('should receive expected results after calling monitor/overview logging', async () => {
// call monitor page
await supertest
.post(API_URLS.LOG_PAGE_VIEW)
.set('kbn-xsrf', 'true')
.send({
page: 'Monitor',
autorefreshInterval: 100,
dateStart: 'now/d',
dateEnd: 'now/d',
autoRefreshEnabled: true,
refreshTelemetryHistory: true,
})
.expect(200);

// call overview page
const { body: result } = await supertest
.post(API_URLS.LOG_PAGE_VIEW)
.set('kbn-xsrf', 'true')
.send({
page: 'Overview',
autorefreshInterval: 60,
dateStart: 'now/d',
dateEnd: 'now-30',
autoRefreshEnabled: true,
})
.expect(200);

expect(result).to.eql({
overview_page: 1,
monitor_page: 1,
no_of_unique_monitors: 4,
settings_page: 0,
monitor_frequency: [120, 0.001, 60, 60],
monitor_name_stats: { min_length: 7, max_length: 22, avg_length: 12 },
no_of_unique_observer_locations: 3,
observer_location_name_stats: { min_length: 2, max_length: 7, avg_length: 4.8 },
dateRangeStart: ['now/d', 'now/d'],
dateRangeEnd: ['now/d', 'now-30'],
autoRefreshEnabled: true,
autorefreshInterval: [100, 60],
});
});
});
}
21 changes: 0 additions & 21 deletions x-pack/test/api_integration/apis/uptime/telemetry_collectors.ts

This file was deleted.

0 comments on commit 280690c

Please sign in to comment.