Skip to content

Commit

Permalink
[Telemetry] fix bug where uiStatsMetrics is not getting report… (#54045)
Browse files Browse the repository at this point in the history
* fix bug where uiStatsMetrics is not getting reported

* fix tests
  • Loading branch information
Bamieh committed Jan 6, 2020
1 parent 01e2172 commit 11c0671
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
24 changes: 12 additions & 12 deletions packages/kbn-analytics/src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class ReportManager {
}
assignReports(newMetrics: Metric | Metric[]) {
wrapArray(newMetrics).forEach(newMetric => this.assignReport(this.report, newMetric));
return { report: this.report };
}
static createMetricKey(metric: Metric): string {
switch (metric.type) {
Expand All @@ -101,7 +102,7 @@ export class ReportManager {
case METRIC_TYPE.USER_AGENT: {
const { appName, type, userAgent } = metric;
if (userAgent) {
this.report.userAgent = {
report.userAgent = {
[key]: {
key,
appName,
Expand All @@ -110,23 +111,22 @@ export class ReportManager {
},
};
}

return;
}
case METRIC_TYPE.CLICK:
case METRIC_TYPE.LOADED:
case METRIC_TYPE.COUNT: {
const { appName, type, eventName, count } = metric;
if (report.uiStatsMetrics) {
const existingStats = (report.uiStatsMetrics[key] || {}).stats;
this.report.uiStatsMetrics = this.report.uiStatsMetrics || {};
this.report.uiStatsMetrics[key] = {
key,
appName,
eventName,
type,
stats: this.incrementStats(count, existingStats),
};
}
report.uiStatsMetrics = report.uiStatsMetrics || {};
const existingStats = (report.uiStatsMetrics[key] || {}).stats;
report.uiStatsMetrics[key] = {
key,
appName,
eventName,
type,
stats: this.incrementStats(count, existingStats),
};
return;
}
default:
Expand Down
26 changes: 9 additions & 17 deletions test/api_integration/apis/ui_metric/ui_metric.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ export default function({ getService }) {
const es = getService('legacyEs');

const createStatsMetric = eventName => ({
key: ReportManager.createMetricKey({ appName: 'myApp', type: METRIC_TYPE.CLICK, eventName }),
eventName,
appName: 'myApp',
type: METRIC_TYPE.CLICK,
stats: { sum: 1, avg: 1, min: 1, max: 1 },
count: 1,
});

const createUserAgentMetric = appName => ({
key: ReportManager.createMetricKey({ appName, type: METRIC_TYPE.USER_AGENT }),
appName,
type: METRIC_TYPE.USER_AGENT,
userAgent:
Expand All @@ -42,12 +40,9 @@ export default function({ getService }) {

describe('ui_metric API', () => {
it('increments the count field in the document defined by the {app}/{action_type} path', async () => {
const reportManager = new ReportManager();
const uiStatsMetric = createStatsMetric('myEvent');
const report = {
uiStatsMetrics: {
[uiStatsMetric.key]: uiStatsMetric,
},
};
const { report } = reportManager.assignReports([uiStatsMetric]);
await supertest
.post('/api/ui_metric/report')
.set('kbn-xsrf', 'kibana')
Expand All @@ -61,21 +56,18 @@ export default function({ getService }) {
});

it('supports multiple events', async () => {
const reportManager = new ReportManager();
const userAgentMetric = createUserAgentMetric('kibana');
const uiStatsMetric1 = createStatsMetric('myEvent');
const hrTime = process.hrtime();
const nano = hrTime[0] * 1000000000 + hrTime[1];
const uniqueEventName = `myEvent${nano}`;
const uiStatsMetric2 = createStatsMetric(uniqueEventName);
const report = {
userAgent: {
[userAgentMetric.key]: userAgentMetric,
},
uiStatsMetrics: {
[uiStatsMetric1.key]: uiStatsMetric1,
[uiStatsMetric2.key]: uiStatsMetric2,
},
};
const { report } = reportManager.assignReports([
userAgentMetric,
uiStatsMetric1,
uiStatsMetric2,
]);
await supertest
.post('/api/ui_metric/report')
.set('kbn-xsrf', 'kibana')
Expand Down

0 comments on commit 11c0671

Please sign in to comment.