Loki/Elasticsearch: prevent errors in onDashboardLoadedHandler (#79451)
* Loki: prevent errors in onDashboardLoadedHandler * Elasticsearch: prevent errors in onDashboardLoadedHandler
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { DashboardLoadedEvent } from '@grafana/data';
|
||||
import { reportInteraction } from '@grafana/runtime';
|
||||
|
||||
import pluginJson from './plugin.json';
|
||||
import { onDashboardLoadedHandler } from './tracking';
|
||||
import { ElasticsearchQuery } from './types';
|
||||
|
||||
jest.mock('@grafana/runtime', () => ({
|
||||
...jest.requireActual('@grafana/runtime'),
|
||||
reportInteraction: jest.fn(),
|
||||
}));
|
||||
|
||||
const targets: ElasticsearchQuery[] = [
|
||||
{
|
||||
refId: 'test',
|
||||
alias: '$varAlias',
|
||||
bucketAggs: [],
|
||||
metrics: [],
|
||||
query: 'test',
|
||||
},
|
||||
];
|
||||
|
||||
afterAll(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('onDashboardLoadedHandler', () => {
|
||||
beforeEach(() => {
|
||||
jest.mocked(reportInteraction).mockClear();
|
||||
jest.spyOn(console, 'error');
|
||||
});
|
||||
test('Reports dashboard loaded interactions', () => {
|
||||
const event = new DashboardLoadedEvent({
|
||||
dashboardId: 'test',
|
||||
orgId: 1,
|
||||
userId: 2,
|
||||
grafanaVersion: '11',
|
||||
queries: {
|
||||
[pluginJson.id]: targets,
|
||||
},
|
||||
});
|
||||
onDashboardLoadedHandler(event);
|
||||
|
||||
expect(reportInteraction).toHaveBeenCalled();
|
||||
expect(console.error).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('Does not report or fails when the dashboard id has no queries', () => {
|
||||
const event = new DashboardLoadedEvent({
|
||||
dashboardId: 'test',
|
||||
orgId: 1,
|
||||
userId: 2,
|
||||
grafanaVersion: '11',
|
||||
queries: {
|
||||
'not elasticsearch': targets,
|
||||
},
|
||||
});
|
||||
onDashboardLoadedHandler(event);
|
||||
|
||||
expect(reportInteraction).not.toHaveBeenCalled();
|
||||
expect(console.error).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -41,7 +41,7 @@ export const onDashboardLoadedHandler = ({
|
||||
}: DashboardLoadedEvent<ElasticsearchQuery>) => {
|
||||
try {
|
||||
// We only want to track visible ElasticSearch queries
|
||||
const elasticsearchQueries = queries[pluginJson.id].filter((query) => !query.hide);
|
||||
const elasticsearchQueries = queries[pluginJson.id]?.filter((query) => !query.hide);
|
||||
if (!elasticsearchQueries?.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import { getQueryOptions } from 'test/helpers/getQueryOptions';
|
||||
|
||||
import { dateTime } from '@grafana/data';
|
||||
import { DashboardLoadedEvent, dateTime } from '@grafana/data';
|
||||
import { reportInteraction } from '@grafana/runtime';
|
||||
|
||||
import { QueryEditorMode } from '../prometheus/querybuilder/shared/types';
|
||||
|
||||
import pluginJson from './plugin.json';
|
||||
import { partitionTimeRange } from './querySplitting';
|
||||
import { trackGroupedQueries, trackQuery } from './tracking';
|
||||
import { onDashboardLoadedHandler, trackGroupedQueries, trackQuery } from './tracking';
|
||||
import { LokiGroupedRequest, LokiQuery } from './types';
|
||||
|
||||
jest.mock('@grafana/runtime', () => ({
|
||||
@@ -63,6 +64,7 @@ beforeAll(() => {
|
||||
});
|
||||
afterAll(() => {
|
||||
jest.useRealTimers();
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
beforeEach(() => {
|
||||
jest.mocked(reportInteraction).mockClear();
|
||||
@@ -182,3 +184,41 @@ test('Tracks grouped queries', () => {
|
||||
predefined_operations_applied: 'n/a',
|
||||
});
|
||||
});
|
||||
|
||||
describe('onDashboardLoadedHandler', () => {
|
||||
beforeEach(() => {
|
||||
jest.mocked(reportInteraction).mockClear();
|
||||
jest.spyOn(console, 'error');
|
||||
});
|
||||
test('Reports dashboard loaded interactions', () => {
|
||||
const event = new DashboardLoadedEvent({
|
||||
dashboardId: 'test',
|
||||
orgId: 1,
|
||||
userId: 2,
|
||||
grafanaVersion: '11',
|
||||
queries: {
|
||||
[pluginJson.id]: originalRequest.targets,
|
||||
},
|
||||
});
|
||||
onDashboardLoadedHandler(event);
|
||||
|
||||
expect(reportInteraction).toHaveBeenCalled();
|
||||
expect(console.error).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('Does not report or fails when the dashboard id has no queries', () => {
|
||||
const event = new DashboardLoadedEvent({
|
||||
dashboardId: 'test',
|
||||
orgId: 1,
|
||||
userId: 2,
|
||||
grafanaVersion: '11',
|
||||
queries: {
|
||||
'not loki': originalRequest.targets,
|
||||
},
|
||||
});
|
||||
onDashboardLoadedHandler(event);
|
||||
|
||||
expect(reportInteraction).not.toHaveBeenCalled();
|
||||
expect(console.error).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -63,8 +63,8 @@ export const onDashboardLoadedHandler = ({
|
||||
try {
|
||||
// We only want to track visible Loki queries
|
||||
const lokiQueries = queries[pluginJson.id]
|
||||
.filter((query) => !query.hide)
|
||||
.map((query) => getNormalizedLokiQuery(query));
|
||||
?.filter((query) => !query.hide)
|
||||
?.map((query) => getNormalizedLokiQuery(query));
|
||||
|
||||
if (!lokiQueries?.length) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user