Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Canvas] Add telemetry around workpad variable usage #75683

Merged
merged 6 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion x-pack/plugins/canvas/__tests__/fixtures/workpads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { CanvasWorkpad, CanvasElement, CanvasPage } from '../../types';
import { CanvasWorkpad, CanvasElement, CanvasPage, CanvasVariable } from '../../types';

const BaseWorkpad: CanvasWorkpad = {
'@created': '2019-02-08T18:35:23.029Z',
Expand Down Expand Up @@ -50,6 +50,12 @@ const BaseElement: CanvasElement = {
filter: '',
};

const BaseVariable: CanvasVariable = {
name: 'my-var',
value: 'Hello World',
type: 'string',
};

export const workpads: CanvasWorkpad[] = [
{
...BaseWorkpad,
Expand All @@ -71,6 +77,11 @@ export const workpads: CanvasWorkpad[] = [
],
},
],
variables: [
{
...BaseVariable,
},
],
},
{
...BaseWorkpad,
Expand All @@ -82,6 +93,11 @@ export const workpads: CanvasWorkpad[] = [
],
},
],
variables: [
{
...BaseVariable,
},
],
},
{
...BaseWorkpad,
Expand All @@ -103,6 +119,11 @@ export const workpads: CanvasWorkpad[] = [
{ ...BasePage, elements: [{ ...BaseElement, expression: 'image | render' }] },
{ ...BasePage, elements: [{ ...BaseElement, expression: 'image | render' }] },
],
variables: [
{
...BaseVariable,
},
],
},
{
...BaseWorkpad,
Expand Down Expand Up @@ -136,6 +157,11 @@ export const workpads: CanvasWorkpad[] = [
],
},
],
variables: [
{
...BaseVariable,
},
],
},
{
...BaseWorkpad,
Expand Down Expand Up @@ -166,6 +192,17 @@ export const workpads: CanvasWorkpad[] = [
],
},
],
variables: [
{
...BaseVariable,
},
{
...BaseVariable,
},
{
...BaseVariable,
},
],
},
{
...BaseWorkpad,
Expand Down
42 changes: 42 additions & 0 deletions x-pack/plugins/canvas/server/collectors/workpad_collector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ describe('usage collector handle es response data', () => {
'shape',
],
},
variables: {
total: 7,
per_workpad: {
avg: 1.1666666666666667,
min: 0,
max: 3,
},
},
});
});

Expand All @@ -63,6 +71,7 @@ describe('usage collector handle es response data', () => {
pages: { total: 1, per_workpad: { avg: 1, min: 1, max: 1 } },
elements: { total: 1, per_page: { avg: 1, min: 1, max: 1 } },
functions: { total: 1, in_use: ['toast'], per_element: { avg: 1, min: 1, max: 1 } },
variables: { total: 1, per_workpad: { avg: 1, min: 1, max: 1 } },
});
});

Expand All @@ -76,6 +85,39 @@ describe('usage collector handle es response data', () => {
pages: { total: 0, per_workpad: { avg: 0, min: 0, max: 0 } },
elements: undefined,
functions: undefined,
variables: { total: 1, per_workpad: { avg: 1, min: 1, max: 1 } }, // Variables still possible even with no pages
});
});

it('should handle cases where the version workpad might not have variables', () => {
const workpad = cloneDeep(workpads[0]);
// @ts-ignore
workpad.variables = undefined;

const mockWorkpadsOld = [workpad];
const usage = summarizeWorkpads(mockWorkpadsOld);
expect(usage).toEqual({
workpads: { total: 1 },
pages: { total: 1, per_workpad: { avg: 1, min: 1, max: 1 } },
elements: { total: 1, per_page: { avg: 1, min: 1, max: 1 } },
functions: {
total: 7,
in_use: [
'demodata',
'ply',
'rowCount',
'as',
'staticColumn',
'math',
'mapColumn',
'sort',
'pointseries',
'plot',
'seriesStyle',
],
per_element: { avg: 7, min: 7, max: 7 },
},
variables: { total: 0, per_workpad: { avg: 0, min: 0, max: 0 } }, // Variables still possible even with no pages
});
});

Expand Down
39 changes: 36 additions & 3 deletions x-pack/plugins/canvas/server/collectors/workpad_collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ interface WorkpadTelemetry {
max: number;
};
};
variables?: {
total: number;
per_workpad: {
avg: number;
min: number;
max: number;
};
};
}

/**
Expand Down Expand Up @@ -81,7 +89,10 @@ export function summarizeWorkpads(workpadDocs: CanvasWorkpad[]): WorkpadTelemetr
});
}, []);

return { pages, elementCounts, functionCounts };
const variableCount =
workpad.variables && workpad.variables.length ? workpad.variables.length : 0;

return { pages, elementCounts, functionCounts, variableCount };
});

// combine together info from across the workpads
Expand All @@ -91,16 +102,18 @@ export function summarizeWorkpads(workpadDocs: CanvasWorkpad[]): WorkpadTelemetr
pageCounts: number[];
elementCounts: number[];
functionCounts: number[];
variableCounts: number[];
}>(
(accum, pageInfo) => {
const { pages, elementCounts, functionCounts } = pageInfo;
const { pages, elementCounts, functionCounts, variableCount } = pageInfo;

return {
pageMin: pages.count < accum.pageMin ? pages.count : accum.pageMin,
pageMax: pages.count > accum.pageMax ? pages.count : accum.pageMax,
pageCounts: accum.pageCounts.concat(pages.count),
elementCounts: accum.elementCounts.concat(elementCounts),
functionCounts: accum.functionCounts.concat(functionCounts),
variableCounts: accum.variableCounts.concat([variableCount]),
};
},
{
Expand All @@ -109,13 +122,23 @@ export function summarizeWorkpads(workpadDocs: CanvasWorkpad[]): WorkpadTelemetr
pageCounts: [],
elementCounts: [],
functionCounts: [],
variableCounts: [],
}
);
const { pageCounts, pageMin, pageMax, elementCounts, functionCounts } = combinedWorkpadsInfo;
const {
pageCounts,
pageMin,
pageMax,
elementCounts,
functionCounts,
variableCounts,
} = combinedWorkpadsInfo;

const pageTotal = arraySum(pageCounts);
const elementsTotal = arraySum(elementCounts);
const functionsTotal = arraySum(functionCounts);
const variableTotal = arraySum(variableCounts);

const pagesInfo =
workpadsInfo.length > 0
? {
Expand Down Expand Up @@ -151,11 +174,21 @@ export function summarizeWorkpads(workpadDocs: CanvasWorkpad[]): WorkpadTelemetr
}
: undefined;

const variableInfo = {
total: variableTotal,
per_workpad: {
avg: variableTotal / variableCounts.length,
min: arrayMin(variableCounts) || 0,
max: arrayMax(variableCounts) || 0,
},
};

return {
workpads: { total: workpadsInfo.length },
pages: pagesInfo,
elements: elementsInfo,
functions: functionsInfo,
variables: variableInfo,
};
}

Expand Down