forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDashboardLoaderSrv.ts
218 lines (197 loc) · 6.04 KB
/
DashboardLoaderSrv.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
import $ from 'jquery';
import _, { isFunction } from 'lodash'; // eslint-disable-line lodash/import-scope
import moment from 'moment'; // eslint-disable-line no-restricted-imports
import { AppEvents, dateMath, UrlQueryValue } from '@grafana/data';
import { getBackendSrv, locationService } from '@grafana/runtime';
import { backendSrv } from 'app/core/services/backend_srv';
import impressionSrv from 'app/core/services/impression_srv';
import kbn from 'app/core/utils/kbn';
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
import { getGrafanaStorage } from 'app/features/storage/storage';
import { DashboardDTO, DashboardRoutes } from 'app/types';
import { appEvents } from '../../../core/core';
import { getDashboardSrv } from './DashboardSrv';
export class DashboardLoaderSrv {
constructor() {}
_dashboardLoadFailed(title: string, snapshot?: boolean) {
snapshot = snapshot || false;
return {
meta: {
canStar: false,
isSnapshot: snapshot,
canDelete: false,
canSave: false,
canEdit: false,
dashboardNotFound: true,
},
dashboard: { title },
};
}
loadDashboard(type: UrlQueryValue, slug: any, uid: any, version: any) {
let promise;
if (type === 'script') {
promise = this._loadScriptedDashboard(slug);
} else if (type === 'snapshot') {
promise = backendSrv.get('/api/snapshots/' + slug).catch(() => {
return this._dashboardLoadFailed('Snapshot not found', true);
});
} else if (type === DashboardRoutes.Path) {
promise = getGrafanaStorage().getDashboard(slug!);
} else if (type === 'ds') {
promise = this._loadFromDatasource(slug); // explore dashboards as code
} else if (type === 'public') {
promise = backendSrv
.getPublicDashboardByUid(uid)
.then((result: any) => {
return result;
})
.catch(() => {
return this._dashboardLoadFailed('Public Dashboard Not found', true);
});
} else if (version !== undefined) {
promise = backendSrv
.getDashboardByUidVersion(uid, version)
.then((result: any) => {
if (result.meta.isFolder) {
appEvents.emit(AppEvents.alertError, ['Dashboard with version not found']);
throw new Error('Dashboard with version not found');
}
return result;
})
.catch(() => {
return this._dashboardLoadFailed('Not found', true);
});
} else {
promise = backendSrv
.getDashboardByUid(uid)
.then((result: any) => {
if (result.meta.isFolder) {
appEvents.emit(AppEvents.alertError, ['Dashboard not found']);
throw new Error('Dashboard not found');
}
return result;
})
.catch(() => {
return this._dashboardLoadFailed('Not found', true);
});
}
promise.then((result: DashboardDTO) => {
if (result.meta.dashboardNotFound !== true) {
impressionSrv.addDashboardImpression(result.dashboard.uid);
}
return result;
});
return promise;
}
_loadScriptedDashboard(file: string) {
const url = 'public/dashboards/' + file.replace(/\.(?!js)/, '/') + '?' + new Date().getTime();
return getBackendSrv()
.get(url)
.then(this._executeScript.bind(this))
.then(
(result: any) => {
return {
meta: {
fromScript: true,
canDelete: false,
canSave: false,
canStar: false,
},
dashboard: result.data,
};
},
(err: any) => {
console.error('Script dashboard error ' + err);
appEvents.emit(AppEvents.alertError, [
'Script Error',
'Please make sure it exists and returns a valid dashboard',
]);
return this._dashboardLoadFailed('Scripted dashboard');
}
);
}
/**
* This is a temporary solution to load dashboards dynamically from a datasource
* Eventually this should become a plugin type or a special handler in the dashboard
* loading code
*/
async _loadFromDatasource(dsid: string) {
const ds = await getDatasourceSrv().get(dsid);
if (!ds) {
return Promise.reject('can not find datasource: ' + dsid);
}
const params = new URLSearchParams(window.location.search);
const path = params.get('path');
if (!path) {
return Promise.reject('expecting path parameter');
}
const queryParams: { [key: string]: any } = {};
params.forEach((value, key) => {
queryParams[key] = value;
});
return getBackendSrv()
.get(`/api/datasources/${ds.id}/resources/${path}`, queryParams)
.then((data) => {
return {
meta: {
fromScript: true,
canDelete: false,
canSave: false,
canStar: false,
},
dashboard: data,
};
});
}
_executeScript(result: any) {
const services = {
dashboardSrv: getDashboardSrv(),
datasourceSrv: getDatasourceSrv(),
};
const scriptFunc = new Function(
'ARGS',
'kbn',
'dateMath',
'_',
'moment',
'window',
'document',
'$',
'jQuery',
'services',
result
);
const scriptResult = scriptFunc(
locationService.getSearchObject(),
kbn,
dateMath,
_,
moment,
window,
document,
$,
$,
services
);
// Handle async dashboard scripts
if (isFunction(scriptResult)) {
return new Promise((resolve) => {
scriptResult((dashboard: any) => {
resolve({ data: dashboard });
});
});
}
return { data: scriptResult };
}
}
let dashboardLoaderSrv = new DashboardLoaderSrv();
export { dashboardLoaderSrv };
/** @internal
* Used for tests only
*/
export const setDashboardLoaderSrv = (srv: DashboardLoaderSrv) => {
if (process.env.NODE_ENV !== 'test') {
throw new Error('dashboardLoaderSrv can be only overriden in test environment');
}
dashboardLoaderSrv = srv;
};