Skip to content

Commit 280690c

Browse files
[Uptime] Improve Telemetry test (#62428)
* 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>
1 parent 80e0b67 commit 280690c

8 files changed

Lines changed: 162 additions & 37 deletions

File tree

x-pack/legacy/plugins/uptime/common/constants/rest_api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export enum API_URLS {
1616
PING_HISTOGRAM = `/api/uptime/ping/histogram`,
1717
SNAPSHOT_COUNT = `/api/uptime/snapshot/count`,
1818
FILTERS = `/api/uptime/filters`,
19-
logPageView = `/api/uptime/logPageView`,
19+
LOG_PAGE_VIEW = `/api/uptime/log_page_view`,
2020

2121
ML_MODULE_JOBS = `/api/ml/modules/jobs_exist/`,
2222
ML_SETUP_MODULE = '/api/ml/modules/setup/',

x-pack/legacy/plugins/uptime/public/hooks/use_telemetry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ export const useUptimeTelemetry = (page?: UptimePage) => {
3030
dateEnd: dateRangeEnd,
3131
autoRefreshEnabled: !autorefreshIsPaused,
3232
};
33-
apiService.post(API_URLS.logPageView, params);
33+
apiService.post(API_URLS.LOG_PAGE_VIEW, params);
3434
}, [autorefreshInterval, autorefreshIsPaused, dateRangeEnd, dateRangeStart, page]);
3535
};

x-pack/plugins/uptime/server/lib/adapters/telemetry/kibana_telemetry_adapter.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import moment from 'moment';
8-
import { ISavedObjectsRepository } from 'kibana/server';
8+
import { ISavedObjectsRepository, SavedObjectsClientContract } from 'kibana/server';
99
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
1010
import { PageViewParams, UptimeTelemetry } from './types';
1111
import { APICaller } from '../framework';
@@ -54,6 +54,9 @@ export class KibanaTelemetryAdapter {
5454
}
5555

5656
public static countPageView(pageView: PageViewParams) {
57+
if (pageView.refreshTelemetryHistory) {
58+
this.collector = {};
59+
}
5760
const bucketId = this.getBucketToIncrement();
5861
const bucket = this.collector[bucketId];
5962
if (pageView.page === 'Overview') {
@@ -94,7 +97,7 @@ export class KibanaTelemetryAdapter {
9497

9598
public static async countNoOfUniqueMonitorAndLocations(
9699
callCluster: APICaller,
97-
savedObjectsClient: ISavedObjectsRepository
100+
savedObjectsClient: ISavedObjectsRepository | SavedObjectsClientContract
98101
) {
99102
const dynamicSettings = await savedObjectsAdapter.getUptimeDynamicSettings(savedObjectsClient);
100103
const params = {
@@ -161,24 +164,27 @@ export class KibanaTelemetryAdapter {
161164
const monitorNameStats: any = result?.aggregations?.monitor_name;
162165
const locationNameStats: any = result?.aggregations?.observer_loc_name;
163166
const uniqueMonitors: any = result?.aggregations?.monitors.buckets;
164-
const bucket = this.getBucketToIncrement();
165167

166-
this.collector[bucket].no_of_unique_monitors = numberOfUniqueMonitors;
167-
this.collector[bucket].no_of_unique_observer_locations = numberOfUniqueLocations;
168-
this.collector[bucket].no_of_unique_observer_locations = numberOfUniqueLocations;
169-
this.collector[bucket].monitor_name_stats = {
168+
const bucketId = this.getBucketToIncrement();
169+
const bucket = this.collector[bucketId];
170+
171+
bucket.no_of_unique_monitors = numberOfUniqueMonitors;
172+
bucket.no_of_unique_observer_locations = numberOfUniqueLocations;
173+
bucket.no_of_unique_observer_locations = numberOfUniqueLocations;
174+
bucket.monitor_name_stats = {
170175
min_length: monitorNameStats?.min_length ?? 0,
171176
max_length: monitorNameStats?.max_length ?? 0,
172-
avg_length: +monitorNameStats?.avg_length.toFixed(2),
177+
avg_length: +(monitorNameStats?.avg_length?.toFixed(2) ?? 0),
173178
};
174179

175-
this.collector[bucket].observer_location_name_stats = {
180+
bucket.observer_location_name_stats = {
176181
min_length: locationNameStats?.min_length ?? 0,
177182
max_length: locationNameStats?.max_length ?? 0,
178183
avg_length: +(locationNameStats?.avg_length?.toFixed(2) ?? 0),
179184
};
180185

181-
this.collector[bucket].monitor_frequency = this.getMonitorsFrequency(uniqueMonitors);
186+
bucket.monitor_frequency = this.getMonitorsFrequency(uniqueMonitors);
187+
return bucket;
182188
}
183189

184190
private static getMonitorsFrequency(uniqueMonitors = []) {

x-pack/plugins/uptime/server/lib/adapters/telemetry/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface PageViewParams {
1010
dateEnd: string;
1111
autoRefreshEnabled: boolean;
1212
autorefreshInterval: number;
13+
refreshTelemetryHistory?: boolean;
1314
}
1415

1516
export interface Stats {

x-pack/plugins/uptime/server/rest_api/telemetry/log_page_view.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,32 @@ import { schema } from '@kbn/config-schema';
88
import { KibanaTelemetryAdapter } from '../../lib/adapters/telemetry';
99
import { UMRestApiRouteFactory } from '../types';
1010
import { PageViewParams } from '../../lib/adapters/telemetry/types';
11+
import { API_URLS } from '../../../../../legacy/plugins/uptime/common/constants';
1112

1213
export const createLogPageViewRoute: UMRestApiRouteFactory = () => ({
1314
method: 'POST',
14-
path: '/api/uptime/logPageView',
15+
path: API_URLS.LOG_PAGE_VIEW,
1516
validate: {
1617
body: schema.object({
1718
page: schema.string(),
1819
dateStart: schema.string(),
1920
dateEnd: schema.string(),
2021
autoRefreshEnabled: schema.boolean(),
2122
autorefreshInterval: schema.number(),
23+
refreshTelemetryHistory: schema.maybe(schema.boolean()),
2224
}),
2325
},
24-
handler: async ({ callES, dynamicSettings }, _context, _request, response): Promise<any> => {
25-
const result = KibanaTelemetryAdapter.countPageView(_request.body as PageViewParams);
26+
handler: async (
27+
{ savedObjectsClient, callES, dynamicSettings },
28+
_context,
29+
request,
30+
response
31+
): Promise<any> => {
32+
await KibanaTelemetryAdapter.countNoOfUniqueMonitorAndLocations(callES, savedObjectsClient);
33+
const pageViewResult = KibanaTelemetryAdapter.countPageView(request.body as PageViewParams);
34+
2635
return response.ok({
27-
body: result,
36+
body: pageViewResult,
2837
});
2938
},
3039
options: {

x-pack/test/api_integration/apis/uptime/rest/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default function({ getService, loadTestFile }: FtrProviderContext) {
4242

4343
loadTestFile(require.resolve('./snapshot'));
4444
loadTestFile(require.resolve('./dynamic_settings'));
45+
loadTestFile(require.resolve('./telemetry_collectors'));
4546
});
4647
describe('with real-world data', () => {
4748
before('load heartbeat data', async () => await esArchiver.load('uptime/full_heartbeat'));
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import expect from '@kbn/expect';
8+
import { FtrProviderContext } from '../../../ftr_provider_context';
9+
import { API_URLS } from '../../../../../legacy/plugins/uptime/common/constants';
10+
import { makeChecksWithStatus } from '../graphql/helpers/make_checks';
11+
12+
export default function({ getService }: FtrProviderContext) {
13+
const supertest = getService('supertest');
14+
const es = getService('legacyEs');
15+
16+
describe('telemetry collectors', () => {
17+
before('generating data', async () => {
18+
await getService('esArchiver').load('uptime/blank');
19+
20+
const observer = {
21+
geo: {
22+
name: 'US-East',
23+
location: '40.7128, -74.0060',
24+
},
25+
};
26+
27+
const observer2 = {
28+
geo: {
29+
name: 'US',
30+
location: '40.7128, -74.0060',
31+
},
32+
};
33+
34+
await makeChecksWithStatus(
35+
es,
36+
'upMonitorId',
37+
1,
38+
1,
39+
60 * 1000,
40+
{
41+
observer: {},
42+
monitor: {
43+
name: 'Elastic',
44+
},
45+
},
46+
'up'
47+
);
48+
49+
await makeChecksWithStatus(
50+
es,
51+
'downMonitorId',
52+
1,
53+
1,
54+
120 * 1000,
55+
{
56+
observer,
57+
monitor: {
58+
name: 'Long Name with 22 Char',
59+
},
60+
},
61+
'down'
62+
);
63+
64+
await makeChecksWithStatus(es, 'noGeoNameMonitor', 1, 1, 60 * 1000, { observer: {} }, 'down');
65+
await makeChecksWithStatus(
66+
es,
67+
'downMonitorId',
68+
1,
69+
1,
70+
1,
71+
{
72+
observer,
73+
monitor: {
74+
name: 'Elastic',
75+
},
76+
},
77+
'down'
78+
);
79+
80+
await makeChecksWithStatus(es, 'mixMonitorId', 1, 1, 1, { observer: observer2 }, 'down');
81+
});
82+
83+
after('unload heartbeat index', () => getService('esArchiver').unload('uptime/blank'));
84+
85+
it('should receive expected results after calling monitor/overview logging', async () => {
86+
// call monitor page
87+
await supertest
88+
.post(API_URLS.LOG_PAGE_VIEW)
89+
.set('kbn-xsrf', 'true')
90+
.send({
91+
page: 'Monitor',
92+
autorefreshInterval: 100,
93+
dateStart: 'now/d',
94+
dateEnd: 'now/d',
95+
autoRefreshEnabled: true,
96+
refreshTelemetryHistory: true,
97+
})
98+
.expect(200);
99+
100+
// call overview page
101+
const { body: result } = await supertest
102+
.post(API_URLS.LOG_PAGE_VIEW)
103+
.set('kbn-xsrf', 'true')
104+
.send({
105+
page: 'Overview',
106+
autorefreshInterval: 60,
107+
dateStart: 'now/d',
108+
dateEnd: 'now-30',
109+
autoRefreshEnabled: true,
110+
})
111+
.expect(200);
112+
113+
expect(result).to.eql({
114+
overview_page: 1,
115+
monitor_page: 1,
116+
no_of_unique_monitors: 4,
117+
settings_page: 0,
118+
monitor_frequency: [120, 0.001, 60, 60],
119+
monitor_name_stats: { min_length: 7, max_length: 22, avg_length: 12 },
120+
no_of_unique_observer_locations: 3,
121+
observer_location_name_stats: { min_length: 2, max_length: 7, avg_length: 4.8 },
122+
dateRangeStart: ['now/d', 'now/d'],
123+
dateRangeEnd: ['now/d', 'now-30'],
124+
autoRefreshEnabled: true,
125+
autorefreshInterval: [100, 60],
126+
});
127+
});
128+
});
129+
}

x-pack/test/api_integration/apis/uptime/telemetry_collectors.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)