diff --git a/public/app/plugins/datasource/elasticsearch/tracking.test.ts b/public/app/plugins/datasource/elasticsearch/tracking.test.ts index 177c743700d..bc8f853b63f 100644 --- a/public/app/plugins/datasource/elasticsearch/tracking.test.ts +++ b/public/app/plugins/datasource/elasticsearch/tracking.test.ts @@ -1,9 +1,9 @@ -import { DashboardLoadedEvent } from '@grafana/data'; +import { CoreApp, DataQueryRequest, DataQueryResponse, DashboardLoadedEvent } from '@grafana/data'; import { reportInteraction } from '@grafana/runtime'; import { ElasticsearchDataQuery } from './dataquery.gen'; import pluginJson from './plugin.json'; -import { onDashboardLoadedHandler } from './tracking'; +import { onDashboardLoadedHandler, trackQuery } from './tracking'; jest.mock('@grafana/runtime', () => ({ ...jest.requireActual('@grafana/runtime'), @@ -61,3 +61,90 @@ describe('onDashboardLoadedHandler', () => { expect(console.error).not.toHaveBeenCalled(); }); }); + +describe('trackQuery', () => { + beforeEach(() => { + jest.mocked(reportInteraction).mockClear(); + }); + + test('tracks editor_type for code editor queries', () => { + const query: ElasticsearchDataQuery = { + refId: 'A', + editorType: 'code', + metrics: [{ id: '1', type: 'count' }], + bucketAggs: [], + }; + + const request: DataQueryRequest & { targets: ElasticsearchDataQuery[] } = { + app: CoreApp.Explore, + targets: [query], + } as DataQueryRequest; + + const response: DataQueryResponse = { + data: [{ length: 1 }], + }; + + trackQuery(response, request, new Date()); + + expect(reportInteraction).toHaveBeenCalledWith( + 'grafana_elasticsearch_query_executed', + expect.objectContaining({ + editor_type: 'code', + }) + ); + }); + + test('tracks editor_type as builder for builder queries', () => { + const query: ElasticsearchDataQuery = { + refId: 'A', + editorType: 'builder', + metrics: [{ id: '1', type: 'count' }], + bucketAggs: [], + }; + + const request: DataQueryRequest & { targets: ElasticsearchDataQuery[] } = { + app: CoreApp.Explore, + targets: [query], + } as DataQueryRequest; + + const response: DataQueryResponse = { + data: [{ length: 1 }], + }; + + trackQuery(response, request, new Date()); + + expect(reportInteraction).toHaveBeenCalledWith( + 'grafana_elasticsearch_query_executed', + expect.objectContaining({ + editor_type: 'builder', + }) + ); + }); + + test('defaults to builder when editor_type is not specified', () => { + const query: ElasticsearchDataQuery = { + refId: 'A', + query: 'test query', + metrics: [{ id: '1', type: 'count' }], + bucketAggs: [], + }; + + const request: DataQueryRequest & { targets: ElasticsearchDataQuery[] } = { + app: CoreApp.Explore, + targets: [query], + } as DataQueryRequest; + + const response: DataQueryResponse = { + data: [{ length: 1 }], + }; + + trackQuery(response, request, new Date()); + + expect(reportInteraction).toHaveBeenCalledWith( + 'grafana_elasticsearch_query_executed', + expect.objectContaining({ + editor_type: 'builder', + }) + ); + }); +}); diff --git a/public/app/plugins/datasource/elasticsearch/tracking.ts b/public/app/plugins/datasource/elasticsearch/tracking.ts index 8d98dc15e5d..508525c08e4 100644 --- a/public/app/plugins/datasource/elasticsearch/tracking.ts +++ b/public/app/plugins/datasource/elasticsearch/tracking.ts @@ -138,6 +138,7 @@ export function trackQuery( time_range_from: request?.range?.from?.toISOString(), time_range_to: request?.range?.to?.toISOString(), time_taken: Date.now() - startTime.getTime(), + editor_type: query.editorType || 'builder', }); } }