Skip to content

Commit

Permalink
add telemetry for Lens formula (elastic#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 authored May 18, 2021
1 parent a6a6fae commit c0d28a9
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/plugins/dashboard/server/usage/dashboard_telemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ const lensXYSeriesB = ({
visualization: {
preferredSeriesType: 'seriesB',
},
datasourceStates: {
indexpattern: {
layers: {
first: {
columns: {
first: {
operationType: 'terms',
},
second: {
operationType: 'formula',
},
},
},
},
},
},
},
},
},
Expand Down Expand Up @@ -144,6 +160,7 @@ describe('dashboard telemetry', () => {
expect(collectorData.lensByValue.a).toBe(3);
expect(collectorData.lensByValue.seriesA).toBe(2);
expect(collectorData.lensByValue.seriesB).toBe(1);
expect(collectorData.lensByValue.formula).toBe(1);
});

it('handles misshapen lens panels', () => {
Expand Down
23 changes: 23 additions & 0 deletions src/plugins/dashboard/server/usage/dashboard_telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ interface LensPanel extends SavedDashboardPanel730ToLatest {
visualization?: {
preferredSeriesType?: string;
};
datasourceStates?: {
indexpattern?: {
layers: Record<
string,
{
columns: Record<string, { operationType: string }>;
}
>;
};
};
};
};
};
Expand Down Expand Up @@ -105,6 +115,19 @@ export const collectByValueLensInfo: DashboardCollectorFunction = (panels, colle
}

collectorData.lensByValue[type] = collectorData.lensByValue[type] + 1;

const hasFormula = Object.values(
lensPanel.embeddableConfig.attributes.state?.datasourceStates?.indexpattern?.layers || {}
).some((layer) =>
Object.values(layer.columns).some((column) => column.operationType === 'formula')
);

if (hasFormula && !collectorData.lensByValue.formula) {
collectorData.lensByValue.formula = 0;
}
if (hasFormula) {
collectorData.lensByValue.formula++;
}
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
} from './math_completion';
import { LANGUAGE_ID } from './math_tokenization';
import { MemoizedFormulaHelp } from './formula_help';
import { trackUiEvent } from '../../../../../lens_ui_telemetry';

import './formula.scss';
import { FormulaIndexPatternColumn } from '../formula';
Expand Down Expand Up @@ -508,6 +509,7 @@ export function FormulaEditor({
<EuiButtonEmpty
onClick={() => {
toggleFullscreen();
trackUiEvent('toggle_formula_fullscreen');
}}
iconType={isFullscreen ? 'bolt' : 'fullScreen'}
size="xs"
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/lens/server/usage/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const eventsSchema: MakeSchemaFrom<LensUsage['events_30_days']> = {
type: 'long',
_meta: { description: 'Number of times the user opened one of the in-product help popovers.' },
},
toggle_fullscreen_formula: {
type: 'long',
_meta: {
description: 'Number of times the user toggled fullscreen mode on formula.',
},
},
indexpattern_field_info_click: { type: 'long' },
loaded: { type: 'long' },
app_filters_updated: { type: 'long' },
Expand Down Expand Up @@ -162,6 +168,10 @@ const eventsSchema: MakeSchemaFrom<LensUsage['events_30_days']> = {
type: 'long',
_meta: { description: 'Number of times the moving average function was selected' },
},
indexpattern_dimension_operation_formula: {
type: 'long',
_meta: { description: 'Number of times the formula function was selected' },
},
};

const suggestionEventsSchema: MakeSchemaFrom<LensUsage['suggestion_events_30_days']> = {
Expand All @@ -183,6 +193,7 @@ const savedSchema: MakeSchemaFrom<LensUsage['saved_overall']> = {
lnsDatatable: { type: 'long' },
lnsPie: { type: 'long' },
lnsMetric: { type: 'long' },
formula: { type: 'long' },
};

export const lensUsageSchema: MakeSchemaFrom<LensUsage> = {
Expand Down
36 changes: 32 additions & 4 deletions x-pack/plugins/lens/server/usage/visualization_counts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ export async function getVisualizationCounts(
size: 100,
},
},
usesFormula: {
filter: {
match: {
operation_type: 'formula',
},
},
},
},
},
},
runtime_mappings: {
operation_type: {
type: 'keyword',
script: {
lang: 'painless',
source: `try {
if(doc['lens.state'].size() == 0) return;
HashMap layers = params['_source'].get('lens').get('state').get('datasourceStates').get('indexpattern').get('layers');
for(layerId in layers.keySet()) {
HashMap columns = layers.get(layerId).get('columns');
for(columnId in columns.keySet()) {
emit(columns.get(columnId).get('operationType'))
}
}
} catch(Exception e) {}`,
},
},
},
Expand All @@ -56,16 +81,19 @@ export async function getVisualizationCounts(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function bucketsToObject(arg: any) {
const obj: Record<string, number> = {};
arg.buckets.forEach((bucket: { key: string; doc_count: number }) => {
arg.byType.buckets.forEach((bucket: { key: string; doc_count: number }) => {
obj[bucket.key] = bucket.doc_count + (obj[bucket.key] ?? 0);
});
if (arg.usesFormula.doc_count > 0) {
obj.formula = arg.usesFormula.doc_count;
}
return obj;
}

return {
saved_overall: bucketsToObject(buckets.overall.byType),
saved_30_days: bucketsToObject(buckets.last30.byType),
saved_90_days: bucketsToObject(buckets.last90.byType),
saved_overall: bucketsToObject(buckets.overall),
saved_30_days: bucketsToObject(buckets.last30),
saved_90_days: bucketsToObject(buckets.last90),
saved_overall_total: buckets.overall.doc_count,
saved_30_days_total: buckets.last30.doc_count,
saved_90_days_total: buckets.last90.doc_count,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2110,6 +2110,12 @@
"description": "Number of times the user opened one of the in-product help popovers."
}
},
"toggle_fullscreen_formula": {
"type": "long",
"_meta": {
"description": "Number of times the user toggled fullscreen mode on formula."
}
},
"indexpattern_field_info_click": {
"type": "long"
},
Expand Down Expand Up @@ -2319,6 +2325,12 @@
"_meta": {
"description": "Number of times the moving average function was selected"
}
},
"indexpattern_dimension_operation_formula": {
"type": "long",
"_meta": {
"description": "Number of times the formula function was selected"
}
}
}
},
Expand All @@ -2333,6 +2345,12 @@
"description": "Number of times the user opened one of the in-product help popovers."
}
},
"toggle_fullscreen_formula": {
"type": "long",
"_meta": {
"description": "Number of times the user toggled fullscreen mode on formula."
}
},
"indexpattern_field_info_click": {
"type": "long"
},
Expand Down Expand Up @@ -2542,6 +2560,12 @@
"_meta": {
"description": "Number of times the moving average function was selected"
}
},
"indexpattern_dimension_operation_formula": {
"type": "long",
"_meta": {
"description": "Number of times the formula function was selected"
}
}
}
},
Expand Down Expand Up @@ -2614,6 +2638,9 @@
},
"lnsMetric": {
"type": "long"
},
"formula": {
"type": "long"
}
}
},
Expand Down Expand Up @@ -2657,6 +2684,9 @@
},
"lnsMetric": {
"type": "long"
},
"formula": {
"type": "long"
}
}
},
Expand Down Expand Up @@ -2700,6 +2730,9 @@
},
"lnsMetric": {
"type": "long"
},
"formula": {
"type": "long"
}
}
}
Expand Down

0 comments on commit c0d28a9

Please sign in to comment.