mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Dashboard: Select the last used data source by default when adding a panel to a dashboard (#71777)
Co-authored-by: Juan Cabanas <juan.cabanas@grafana.com>
This commit is contained in:
@@ -4,9 +4,16 @@ import { DataQuery, getDataSourceRef } from '@grafana/data';
|
||||
import { locationService } from '@grafana/runtime';
|
||||
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
|
||||
import { QueryGroup } from 'app/features/query/components/QueryGroup';
|
||||
import { MIXED_DATASOURCE_NAME } from 'app/plugins/datasource/mixed/MixedDataSource';
|
||||
import { QueryGroupDataSource, QueryGroupOptions } from 'app/types';
|
||||
|
||||
import { getDashboardSrv } from '../../services/DashboardSrv';
|
||||
import { PanelModel } from '../../state';
|
||||
import {
|
||||
getLastUsedDatasourceFromStorage,
|
||||
initLastUsedDatasourceKeyForDashboard,
|
||||
setLastUsedDatasourceKeyForDashboard,
|
||||
} from '../../utils/dashboard';
|
||||
|
||||
interface Props {
|
||||
/** Current panel */
|
||||
@@ -20,12 +27,28 @@ export class PanelEditorQueries extends PureComponent<Props> {
|
||||
super(props);
|
||||
}
|
||||
|
||||
// store last used datasource in local storage
|
||||
updateLastUsedDatasource = (datasource: QueryGroupDataSource) => {
|
||||
if (!datasource.uid) {
|
||||
return;
|
||||
}
|
||||
|
||||
const dashboardUid = getDashboardSrv().getCurrent()?.uid ?? '';
|
||||
// if datasource is MIXED reset datasource uid in storage, because Mixed datasource can contain multiple ds
|
||||
if (datasource.uid === MIXED_DATASOURCE_NAME) {
|
||||
return initLastUsedDatasourceKeyForDashboard(dashboardUid!);
|
||||
}
|
||||
setLastUsedDatasourceKeyForDashboard(dashboardUid, datasource.uid);
|
||||
};
|
||||
|
||||
buildQueryOptions(panel: PanelModel): QueryGroupOptions {
|
||||
const dataSource: QueryGroupDataSource = panel.datasource ?? {
|
||||
default: true,
|
||||
};
|
||||
const datasourceSettings = getDatasourceSrv().getInstanceSettings(dataSource);
|
||||
|
||||
// store last datasource used in local storage
|
||||
this.updateLastUsedDatasource(dataSource);
|
||||
return {
|
||||
cacheTimeout: datasourceSettings?.meta.queryOptions?.cacheTimeout ? panel.cacheTimeout : undefined,
|
||||
dataSource: {
|
||||
@@ -51,7 +74,20 @@ export class PanelEditorQueries extends PureComponent<Props> {
|
||||
// If the panel model has no datasource property load the default data source property and update the persisted model
|
||||
// Because this part of the panel model is not in redux yet we do a forceUpdate.
|
||||
if (!panel.datasource) {
|
||||
const ds = getDatasourceSrv().getInstanceSettings(null);
|
||||
let ds;
|
||||
// check if we have last used datasource from local storage
|
||||
// get dashboard uid
|
||||
const dashboardUid = getDashboardSrv().getCurrent()?.uid ?? '';
|
||||
const lastUsedDatasource = getLastUsedDatasourceFromStorage(dashboardUid!);
|
||||
// do we have a last used datasource for this dashboard
|
||||
if (lastUsedDatasource?.datasourceUid !== null) {
|
||||
// get datasource from uid
|
||||
ds = getDatasourceSrv().getInstanceSettings(lastUsedDatasource?.datasourceUid);
|
||||
}
|
||||
// else load default datasource
|
||||
if (!ds) {
|
||||
ds = getDatasourceSrv().getInstanceSettings(null);
|
||||
}
|
||||
panel.datasource = getDataSourceRef(ds!);
|
||||
this.forceUpdate();
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ import { saveDashboard as saveDashboardApiCall } from 'app/features/manage-dashb
|
||||
import { useDispatch } from 'app/types';
|
||||
import { DashboardSavedEvent } from 'app/types/events';
|
||||
|
||||
import { updateDashboardUidLastUsedDatasource } from '../../utils/dashboard';
|
||||
|
||||
import { SaveDashboardOptions } from './types';
|
||||
|
||||
const saveDashboard = async (
|
||||
@@ -60,6 +62,10 @@ export const useDashboardSave = (dashboard: DashboardModel, isCopy = false) => {
|
||||
// important that these happen before location redirect below
|
||||
appEvents.publish(new DashboardSavedEvent());
|
||||
notifyApp.success('Dashboard saved');
|
||||
|
||||
//Update local storage dashboard to handle things like last used datasource
|
||||
updateDashboardUidLastUsedDatasource(result.uid);
|
||||
|
||||
if (isCopy) {
|
||||
reportInteraction('grafana_dashboard_copied', {
|
||||
name: dashboard.title,
|
||||
|
||||
Reference in New Issue
Block a user