forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitDashboard.ts
310 lines (270 loc) · 10.2 KB
/
initDashboard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
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';
import store from 'app/core/store';
import { dashboardLoaderSrv } from 'app/features/dashboard/services/DashboardLoaderSrv';
import { DashboardSrv, getDashboardSrv } from 'app/features/dashboard/services/DashboardSrv';
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { dashboardWatcher } from 'app/features/live/dashboard/dashboardWatcher';
import { playlistSrv } from 'app/features/playlist/PlaylistSrv';
import { toStateKey } from 'app/features/variables/utils';
import {
DashboardDTO,
DashboardInitPhase,
DashboardMeta,
DashboardRoutes,
StoreState,
ThunkDispatch,
ThunkResult,
} from 'app/types';
import { createDashboardQueryRunner } from '../../query/state/DashboardQueryRunner/DashboardQueryRunner';
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';
export interface InitDashboardArgs {
urlUid?: string;
urlSlug?: string;
urlType?: string;
urlFolderUid?: string;
version?: number;
panelType?: string;
accessToken?: string;
routeName?: string;
fixUrl: boolean;
keybindingSrv?: KeybindingSrv;
}
async function fetchDashboard(
args: InitDashboardArgs,
dispatch: ThunkDispatch,
getState: () => StoreState
): Promise<DashboardDTO | null> {
// When creating new or adding panels to a dashboard from explore we load it from local storage
const model = store.getObject<DashboardDTO>(DASHBOARD_FROM_LS_KEY);
if (model) {
removeDashboardToFetchFromLocalStorage();
return model;
}
try {
switch (args.routeName) {
case DashboardRoutes.Home: {
// load home dash
const dashDTO: DashboardDTO = await backendSrv.get('/api/dashboards/home');
// if user specified a custom home dashboard redirect to that
if (dashDTO.redirectUri) {
const newUrl = locationUtil.stripBaseFromUrl(dashDTO.redirectUri);
locationService.replace(newUrl);
return null;
}
// disable some actions on the default home dashboard
dashDTO.meta.canSave = false;
dashDTO.meta.canShare = false;
dashDTO.meta.canStar = false;
return dashDTO;
}
case DashboardRoutes.Public: {
return await dashboardLoaderSrv.loadDashboard('public', args.urlSlug, args.accessToken, args.version);
}
case DashboardRoutes.Normal: {
const dashDTO: DashboardDTO = await dashboardLoaderSrv.loadDashboard(args.urlType, args.urlSlug, args.urlUid, args.version);
if (args.fixUrl && dashDTO.meta.url && !playlistSrv.isPlaying) {
// check if the current url is correct (might be old slug)
const dashboardUrl = locationUtil.stripBaseFromUrl(dashDTO.meta.url);
const currentPath = locationService.getLocation().pathname;
if (dashboardUrl !== currentPath) {
// Spread current location to persist search params used for navigation
locationService.replace({
...locationService.getLocation(),
pathname: dashboardUrl,
});
console.log('not correct url correcting', dashboardUrl, currentPath);
}
}
return dashDTO;
}
case DashboardRoutes.New: {
return getNewDashboardModelData(args.urlFolderUid, args.panelType);
}
case DashboardRoutes.Path: {
const path = args.urlSlug ?? '';
return await dashboardLoaderSrv.loadDashboard(DashboardRoutes.Path, path, path, args.version);
}
default:
throw { message: 'Unknown route ' + args.routeName };
}
} catch (err) {
// Ignore cancelled errors
if (isFetchError(err) && err.cancelled) {
return null;
}
dispatch(dashboardInitFailed({ message: 'Failed to fetch dashboard', error: err }));
console.error(err);
return null;
}
}
const getQueriesByDatasource = (
panels: PanelModel[],
queries: { [datasourceId: string]: DataQuery[] } = {}
): { [datasourceId: string]: DataQuery[] } => {
panels.forEach((panel) => {
if (panel.panels) {
getQueriesByDatasource(panel.panels, queries);
} else if (panel.targets) {
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)
*
* This is used both for single dashboard & solo panel routes, home & new dashboard routes.
*
* Then it handles the initializing of the old angular services that the dashboard components & panels still depend on
*
*/
export function initDashboard(args: InitDashboardArgs): ThunkResult<void> {
return async (dispatch, getState) => {
// set fetching state
dispatch(dashboardInitFetching());
// fetch dashboard data
const dashDTO = await fetchDashboard(args, dispatch, getState);
// returns null if there was a redirect or error
if (!dashDTO) {
return;
}
// set initializing state
dispatch(dashboardInitServices());
// create model
let dashboard: DashboardModel;
try {
dashboard = new DashboardModel(dashDTO.dashboard, dashDTO.meta);
} catch (err) {
dispatch(dashboardInitFailed({ message: 'Failed create dashboard model', error: err }));
console.error(err);
return;
}
// add missing orgId query param
const storeState = getState();
const queryParams = locationService.getSearchObject();
if (!queryParams.orgId) {
// TODO this is currently not possible with the LocationService API
locationService.partial({ orgId: storeState.user.orgId }, true);
}
// init services
const timeSrv: TimeSrv = getTimeSrv(); // FN: We might need to return this to main app so that we can render it elsewhere
const dashboardSrv: DashboardSrv = getDashboardSrv();
// legacy srv state, we need this value updated for built-in annotations
dashboardSrv.setCurrent(dashboard);
timeSrv.init(dashboard);
const dashboardUid = toStateKey(args.urlUid ?? dashboard.uid);
// template values service needs to initialize completely before the rest of the dashboard can load
await dispatch(initVariablesTransaction(dashboardUid, dashboard));
// DashboardQueryRunner needs to run after all variables have been resolved so that any annotation query including a variable
// will be correctly resolved
const runner = createDashboardQueryRunner({ dashboard, timeSrv });
runner.run({ dashboard, range: timeSrv.timeRange() });
if (getIfExistsLastKey(getState()) !== dashboardUid) {
// if a previous dashboard has slow running variable queries the batch uid will be the new one
// but the args.urlUid will be the same as before initVariablesTransaction was called so then we can't continue initializing
// the previous dashboard.
return;
}
// If dashboard is in a different init phase it means it cancelled during service init
if (getState().dashboard.initPhase !== DashboardInitPhase.Services) {
return;
}
try {
dashboard.processRepeats();
// handle auto fix experimental feature
if (queryParams.autofitpanels) {
dashboard.autoFitPanels(window.innerHeight, queryParams.kiosk);
}
args.keybindingSrv?.setupDashboardBindings(dashboard);
} catch (err) {
if (err instanceof Error) {
dispatch(notifyApp(createErrorNotification('Dashboard init failed', err)));
}
console.error(err);
}
// send open dashboard event
if (args.routeName !== DashboardRoutes.New) {
emitDashboardViewEvent(dashboard);
// Listen for changes on the current dashboard
dashboardWatcher.watch(dashboard.uid);
} else {
dashboardWatcher.leave();
}
// set week start
if (dashboard.weekStart !== '') {
setWeekStart(dashboard.weekStart);
} else {
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));
};
}
export function getNewDashboardModelData(
urlFolderUid?: string,
panelType?: string
): { dashboard: any; meta: DashboardMeta } {
const panels = config.featureToggles.emptyDashboardPage
? []
: [
{
type: panelType ?? 'add-panel',
gridPos: { x: 0, y: 0, w: 12, h: 9 },
title: 'Panel Title',
},
];
const data = {
meta: {
canStar: false,
canShare: false,
canDelete: false,
isNew: true,
folderUid: '',
},
dashboard: {
title: 'New dashboard',
panels,
},
};
if (urlFolderUid) {
data.meta.folderUid = urlFolderUid;
}
return data;
}
const DASHBOARD_FROM_LS_KEY = 'DASHBOARD_FROM_LS_KEY';
export function setDashboardToFetchFromLocalStorage(model: DashboardDTO) {
store.setObject(DASHBOARD_FROM_LS_KEY, model);
}
export function removeDashboardToFetchFromLocalStorage() {
store.delete(DASHBOARD_FROM_LS_KEY);
}