Skip to content

Commit

Permalink
Report summarized version of task data when requested
Browse files Browse the repository at this point in the history
  • Loading branch information
wylieconlon committed Oct 11, 2019
1 parent 8c818e4 commit 5281cbd
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 119 deletions.
9 changes: 4 additions & 5 deletions x-pack/legacy/plugins/lens/public/app_plugin/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { KibanaContextProvider } from '../../../../../../src/plugins/kibana_reac
import { Document, SavedObjectStore } from '../persistence';
import { EditorFrameInstance } from '../types';
import { NativeRenderer } from '../native_renderer';
import { useLensTelemetry } from '../lens_ui_telemetry';
import { trackUiEvent } from '../lens_ui_telemetry';

interface State {
isLoading: boolean;
Expand Down Expand Up @@ -65,7 +65,6 @@ export function App({
const timeDefaults = core.uiSettings.get('timepicker:timeDefaults');
const language =
store.get('kibana.userQueryLanguage') || core.uiSettings.get('search:queryLanguage');
const { trackClick } = useLensTelemetry();

const [state, setState] = useState<State>({
isLoading: !!docId,
Expand All @@ -87,7 +86,7 @@ export function App({
next: () => {
setState(s => ({ ...s, filters: dataShim.filter.filterManager.getFilters() }));

trackClick('app_filters_updated');
trackUiEvent('app_filters_updated');
},
});
return () => {
Expand Down Expand Up @@ -202,7 +201,7 @@ export function App({
}
})
.catch(() => {
trackClick('save_failed');
trackUiEvent('save_failed');
core.notifications.toasts.addDanger(
i18n.translate('xpack.lens.editorFrame.docSavingError', {
defaultMessage: 'Error saving document',
Expand All @@ -228,7 +227,7 @@ export function App({
query: query || s.query,
}));

trackClick('date_or_query_change');
trackUiEvent('date_or_query_change');
}}
appName={'lens'}
indexPatterns={state.indexPatternsForTopNav}
Expand Down
57 changes: 26 additions & 31 deletions x-pack/legacy/plugins/lens/public/app_plugin/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from '../datatable_visualization_plugin';
import { App } from './app';
import { EditorFrameInstance } from '../types';
import { LensReportManager, LensTelemetryContext } from '../lens_ui_telemetry';
import { LensReportManager, setReportManager, trackUiEvent } from '../lens_ui_telemetry';

export interface LensPluginStartDependencies {
data: DataPublicPluginStart;
Expand Down Expand Up @@ -65,44 +65,39 @@ export class AppPlugin {

this.instance = editorFrameStartInterface.createInstance({});

this.reporter = new LensReportManager({
storage: new Storage(localStorage),
basePath: core.http.basePath.get(),
http: core.http,
});
// Sets it in memory for future click tracking
setReportManager(
new LensReportManager({
storage: new Storage(localStorage),
basePath: core.http.basePath.get(),
http: core.http,
})
);

const renderEditor = (routeProps: RouteComponentProps<{ id?: string }>) => {
if (this.reporter) {
this.reporter.trackClick('loaded');
}
trackUiEvent('loaded');
return (
<LensTelemetryContext.Provider
value={{
trackClick: name => this.reporter && this.reporter.trackClick(name),
trackSuggestionClick: name => this.reporter && this.reporter.trackSuggestionClick(name),
<App
core={core}
data={data}
dataShim={dataShim}
editorFrame={this.instance!}
store={new Storage(localStorage)}
docId={routeProps.match.params.id}
docStorage={store}
redirectTo={id => {
if (!id) {
routeProps.history.push('/');
} else {
routeProps.history.push(`/edit/${id}`);
}
}}
>
<App
core={core}
data={data}
dataShim={dataShim}
editorFrame={this.instance!}
store={new Storage(localStorage)}
docId={routeProps.match.params.id}
docStorage={store}
redirectTo={id => {
if (!id) {
routeProps.history.push('/');
} else {
routeProps.history.push(`/edit/${id}`);
}
}}
/>
</LensTelemetryContext.Provider>
/>
);
};

function NotFound() {
trackUiEvent('loaded-404');
return <FormattedMessage id="xpack.lens.app404" defaultMessage="404 Not Found" />;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { i18n } from '@kbn/i18n';
import { Visualization, FramePublicAPI, Datasource } from '../../types';
import { Action } from './state_management';
import { getSuggestions, switchToSuggestion, Suggestion } from './suggestion_helpers';
import { trackUiEvent } from '../../lens_ui_telemetry';

interface VisualizationSelection {
visualizationId: string;
Expand Down Expand Up @@ -76,6 +77,8 @@ export function ChartSwitch(props: Props) {
const commitSelection = (selection: VisualizationSelection) => {
setFlyoutOpen(false);

trackUiEvent(`chart-switch-${selection.subVisualizationId || selection.visualizationId}`);

switchToSuggestion(
props.framePublicAPI,
props.dispatch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { getSuggestions, switchToSuggestion } from './suggestion_helpers';
import { ExpressionRenderer } from '../../../../../../../src/legacy/core_plugins/expressions/public';
import { prependDatasourceExpression, prependKibanaContext } from './expression_helpers';
import { debouncedComponent } from '../../debounced_component';
import { trackSuggestionEvent } from '../../lens_ui_telemetry';

const MAX_SUGGESTIONS_DISPLAYED = 5;

Expand Down Expand Up @@ -226,6 +227,7 @@ export function SuggestionPanel({
}

function rollbackToCurrentVisualization() {
trackSuggestionEvent('rollback');
if (lastSelectedSuggestion !== -1) {
setLastSelectedSuggestion(-1);
dispatch({
Expand Down Expand Up @@ -310,6 +312,7 @@ export function SuggestionPanel({
if (lastSelectedSuggestion === index) {
rollbackToCurrentVisualization();
} else {
trackSuggestionEvent(`${suggestion.visualizationId}-${suggestion.changeType}`);
setLastSelectedSuggestion(index);
switchToSuggestion(frame, dispatch, suggestion);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { DragDrop, DragContext } from '../../drag_drop';
import { getSuggestions, switchToSuggestion } from './suggestion_helpers';
import { buildExpression } from './expression_helpers';
import { debouncedComponent } from '../../debounced_component';
import { trackUiEvent } from '../../lens_ui_telemetry';

export interface WorkspacePanelProps {
activeVisualizationId: string | null;
Expand Down Expand Up @@ -84,12 +85,15 @@ export function InnerWorkspacePanel({

function onDrop() {
if (suggestionForDraggedField) {
trackUiEvent('workspace-drop-success');
switchToSuggestion(
framePublicAPI,
dispatch,
suggestionForDraggedField,
'SWITCH_VISUALIZATION'
);
} else {
trackUiEvent('workspace-drop-failure');
}
}

Expand Down
41 changes: 18 additions & 23 deletions x-pack/legacy/plugins/lens/public/editor_frame_plugin/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { EditorFrame } from './editor_frame';
import { mergeTables } from './merge_tables';
import { EmbeddableFactory } from './embeddable/embeddable_factory';
import { getActiveDatasourceIdFromDoc } from './editor_frame/state_management';
import { LensTelemetryContext, useLensTelemetry } from '../lens_ui_telemetry';

export interface EditorFrameSetupPlugins {
data: typeof dataSetup;
Expand Down Expand Up @@ -84,28 +83,24 @@ export class EditorFramePlugin {

render(
<I18nProvider>
<LensTelemetryContext>
<EditorFrame
data-test-subj="lnsEditorFrame"
onError={onError}
datasourceMap={this.datasources}
visualizationMap={this.visualizations}
initialDatasourceId={
getActiveDatasourceIdFromDoc(doc) || firstDatasourceId || null
}
initialVisualizationId={
(doc && doc.visualizationType) || firstVisualizationId || null
}
core={core}
ExpressionRenderer={plugins.expressions.ExpressionRenderer}
doc={doc}
dateRange={dateRange}
query={query}
filters={filters}
savedQuery={savedQuery}
onChange={onChange}
/>
</LensTelemetryContext>
<EditorFrame
data-test-subj="lnsEditorFrame"
onError={onError}
datasourceMap={this.datasources}
visualizationMap={this.visualizations}
initialDatasourceId={getActiveDatasourceIdFromDoc(doc) || firstDatasourceId || null}
initialVisualizationId={
(doc && doc.visualizationType) || firstVisualizationId || null
}
core={core}
ExpressionRenderer={plugins.expressions.ExpressionRenderer}
doc={doc}
dateRange={dateRange}
query={query}
filters={filters}
savedQuery={savedQuery}
onChange={onChange}
/>
</I18nProvider>,
domElement
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,5 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { createContext, useContext } from 'react';

export const LensTelemetryContext = createContext<{
trackClick: (name: string) => void;
trackSuggestionClick: (name: string, suggestionData: unknown) => void;
}>({
trackClick: jest.fn(),
trackSuggestionClick: jest.fn(),
});

export const useLensTelemetry = () => useContext(LensTelemetryContext);
export const trackClick = jest.fn();
export const trackSuggestionClick = jest.fn();
24 changes: 21 additions & 3 deletions x-pack/legacy/plugins/lens/public/lens_ui_telemetry/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ export class LensReportManager {
}, 10000);
}

public trackClick(name: string) {
public trackEvent(name: string) {
this.clicks.push({
name,
date: new Date().toISOString(),
});
this.write();
}

public trackSuggestionClick(name: string) {
public trackSuggestionEvent(name: string) {
this.suggestionClicks.push({
name,
date: new Date().toISOString(),
Expand All @@ -75,7 +75,7 @@ export class LensReportManager {
await this.http.post(`${this.basePath}${BASE_API_URL}/telemetry`, {
body: JSON.stringify({
clicks: this.clicks,
suggestionClicks: this.clicks,
suggestionClicks: this.suggestionClicks,
}),
});
this.clicks = [];
Expand All @@ -87,3 +87,21 @@ export class LensReportManager {
}
}
}

let reportManager: LensReportManager;

export function setReportManager(newManager: LensReportManager) {
reportManager = newManager;
}

export function trackUiEvent(name: string) {
if (reportManager) {
reportManager.trackEvent(name);
}
}

export function trackSuggestionEvent(name: string) {
if (reportManager) {
reportManager.trackSuggestionEvent(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
*/

export * from './factory';
export * from './provider';
17 changes: 0 additions & 17 deletions x-pack/legacy/plugins/lens/public/lens_ui_telemetry/provider.ts

This file was deleted.

6 changes: 5 additions & 1 deletion x-pack/legacy/plugins/lens/server/routes/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ export async function initLensUsageRoute(
},
}));

await client.bulkCreate(clickEvents.concat(suggestionEvents));
const events = clickEvents.concat(suggestionEvents);

if (events.length > 0) {
await client.bulkCreate(events);
}

return res.ok({ body: {} });
} catch (e) {
Expand Down
Loading

0 comments on commit 5281cbd

Please sign in to comment.