mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
@@ -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));
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user