Datasources: Emit event on dashboard load with queries info (#52052)

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>
Co-authored-by: Andres Martinez <andres.martinez@grafana.com>
This commit is contained in:
Yaelle Chaudy
2022-08-11 09:30:14 +02:00
committed by GitHub
parent d7556bd189
commit dfe33a63fb
5 changed files with 256 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
import { locationUtil, setWeekStart } from '@grafana/data';
import { DataQuery, locationUtil, setWeekStart, DashboardLoadedEvent } from '@grafana/data';
import { config, isFetchError, locationService } from '@grafana/runtime';
import { notifyApp } from 'app/core/actions';
import appEvents from 'app/core/app_events';
import { createErrorNotification } from 'app/core/copy/appNotification';
import { backendSrv } from 'app/core/services/backend_srv';
import { keybindingSrv } from 'app/core/services/keybindingSrv';
@@ -17,6 +18,7 @@ import { initVariablesTransaction } from '../../variables/state/actions';
import { getIfExistsLastKey } from '../../variables/state/selectors';
import { DashboardModel } from './DashboardModel';
import { PanelModel } from './PanelModel';
import { emitDashboardViewEvent } from './analyticsProcessor';
import { dashboardInitCompleted, dashboardInitFailed, dashboardInitFetching, dashboardInitServices } from './reducers';
@@ -106,6 +108,28 @@ async function fetchDashboard(
}
}
const getQueriesByDatasource = (
panels: PanelModel[],
queries: { [datasourceId: string]: DataQuery[] } = {}
): { [datasourceId: string]: DataQuery[] } => {
panels.forEach((panel) => {
if (panel.panels) {
getQueriesByDatasource(panel.panels, queries);
} else {
panel.targets.forEach((target) => {
if (target.datasource?.type) {
if (queries[target.datasource.type]) {
queries[target.datasource.type].push(target);
} else {
queries[target.datasource.type] = [target];
}
}
});
}
});
return queries;
};
/**
* This action (or saga) does everything needed to bootstrap a dashboard & dashboard model.
* First it handles the process of fetching the dashboard, correcting the url if required (causing redirects/url updates)
@@ -213,6 +237,17 @@ export function initDashboard(args: InitDashboardArgs): ThunkResult<void> {
setWeekStart(config.bootData.user.weekStart);
}
// Propagate an app-wide event about the dashboard being loaded
appEvents.publish(
new DashboardLoadedEvent({
dashboardId: dashboard.uid,
orgId: storeState.user.orgId,
userId: storeState.user.user?.id,
grafanaVersion: config.buildInfo.version,
queries: getQueriesByDatasource(dashboard.panels),
})
);
// yay we are done
dispatch(dashboardInitCompleted(dashboard));
};