Skip to content

Commit 66ffbc5

Browse files
committed
testing with grafana 6
1 parent df45bfe commit 66ffbc5

File tree

6 files changed

+163
-8157
lines changed

6 files changed

+163
-8157
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ The AJAX Panel is a general way to load external content into a grafana dashboar
3535
* removing dist from master build (only add it on release branches)
3636
* Support empty text response (#9)
3737
* webpack build
38+
* Show query results
39+
* tested with grafana 6
40+
3841

3942
##### v0.0.5
4043

src/examples.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const examples = [
66
name: 'Simple',
77
text: 'loads static content from github',
88
config: {
9+
request: 'http',
910
method: 'GET',
1011
mode: RenderMode.html,
1112
template: '',

src/module.ts

Lines changed: 89 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class AjaxCtrl extends MetricsPanelCtrl {
4141
public $rootScope,
4242
public $q,
4343
public $timeout,
44-
public $http,
4544
public $sce,
4645
public templateSrv,
4746
public datasourceSrv,
@@ -50,6 +49,11 @@ class AjaxCtrl extends MetricsPanelCtrl {
5049
) {
5150
super($scope, $injector);
5251

52+
// Migrate old settings
53+
if (this.panel.useDatasource) {
54+
this.panel.request = 'datasource';
55+
delete this.panel.useDatasource;
56+
}
5357
_.defaults(this.panel, examples[0].config);
5458

5559
$scope.$on('$destroy', () => {
@@ -58,11 +62,29 @@ class AjaxCtrl extends MetricsPanelCtrl {
5862
}
5963
});
6064

65+
this.events.on('data-received', this.onDataReceived.bind(this));
66+
this.events.on('data-snapshot-load', this.onDataSnapshotLoad.bind(this));
67+
this.events.on('data-error', this.onDataError.bind(this));
68+
6169
this.events.on('init-edit-mode', this.onInitEditMode.bind(this));
6270
this.events.on('panel-initialized', this.onPanelInitalized.bind(this));
6371
this.events.on('render', this.notifyWhenRenderingCompleted.bind(this));
6472
}
6573

74+
onDataSnapshotLoad(snapshotData) {
75+
this.onDataReceived(snapshotData);
76+
}
77+
78+
onDataError(err) {
79+
console.log('onDataError', err);
80+
}
81+
82+
// Having this function pust ths query sidebar on
83+
onDataReceived(dataList) {
84+
this.process(dataList);
85+
this.loading = false;
86+
}
87+
6688
// This checks that all requests have completed before saying
6789
notifyWhenRenderingCompleted() {
6890
if (this.timer) {
@@ -93,6 +115,10 @@ class AjaxCtrl extends MetricsPanelCtrl {
93115
return examples;
94116
}
95117

118+
isUsingMetricQuery() {
119+
return this.panel.request.startsWith('query');
120+
}
121+
96122
loadExample(example: any, evt?: any) {
97123
if (evt) {
98124
evt.stopPropagation();
@@ -120,7 +146,6 @@ class AjaxCtrl extends MetricsPanelCtrl {
120146
this.$scope.response = null;
121147
this.updateFN();
122148
this.updateTemplate();
123-
this.datasourceChanged(null);
124149
this.refresh();
125150
}
126151

@@ -170,19 +195,23 @@ class AjaxCtrl extends MetricsPanelCtrl {
170195
* @override
171196
*/
172197
updateTimeRange(datasource?) {
173-
// Keep the timeinfo even after updating the range
174198
const before = this.timeInfo;
175-
super.updateTimeRange();
199+
const v = super.updateTimeRange(datasource);
176200
if (this.panel.showTime && before) {
177201
this.timeInfo = before;
178202
}
203+
return v;
179204
}
180205

181206
/**
182207
* Rather than issue a datasource query, we will call our ajax request
183208
* @override
184209
*/
185210
issueQueries(datasource) {
211+
if (this.isUsingMetricQuery()) {
212+
return super.issueQueries(datasource);
213+
}
214+
186215
if (this.fn_error) {
187216
this.loading = false;
188217
this.error = this.fn_error;
@@ -239,14 +268,26 @@ class AjaxCtrl extends MetricsPanelCtrl {
239268
};
240269
options.headers = options.headers || {};
241270

242-
if (this.dsInfo) {
243-
if (this.dsInfo.basicAuth || this.dsInfo.withCredentials) {
244-
options.withCredentials = true;
245-
}
246-
if (this.dsInfo.basicAuth) {
247-
options.headers.Authorization = this.dsInfo.basicAuth;
248-
}
249-
options.url = this.dsInfo.baseURL + url;
271+
let helper = Promise.resolve({});
272+
if (this.panel.request === 'datasource') {
273+
helper = this.datasourceSrv.get(this.panel.datasource).then(ds => {
274+
console.log('DDDD', ds, this);
275+
if (ds) {
276+
this.dsInfo = new DSInfo(ds);
277+
278+
// Change the URL
279+
if (this.dsInfo.basicAuth || this.dsInfo.withCredentials) {
280+
options.withCredentials = true;
281+
}
282+
if (this.dsInfo.basicAuth) {
283+
options.headers.Authorization = this.dsInfo.basicAuth;
284+
}
285+
286+
options.url = this.dsInfo.baseURL + url;
287+
} else {
288+
this.dsInfo = null;
289+
}
290+
});
250291
} else if (!options.url) {
251292
this.error = 'Missing URL';
252293
this.showError(this.error, null);
@@ -260,43 +301,50 @@ class AjaxCtrl extends MetricsPanelCtrl {
260301
// Now make the call
261302
this.requestCount++;
262303
this.loading = true;
263-
this.backendSrv.datasourceRequest(options).then(
264-
response => {
265-
this.lastRequestTime = sent;
266-
this.process(response);
267-
this.loading = false;
268-
},
269-
err => {
270-
console.log('ERR', err);
271-
this.lastRequestTime = sent;
272-
this.loading = false;
273-
274-
this.error = err; //.data.error + " ["+err.status+"]";
275-
this.inspector = {error: err};
276-
this.showError('Request Error', err);
277-
}
278-
);
304+
helper.then(() => {
305+
this.backendSrv.datasourceRequest(options).then(
306+
response => {
307+
this.lastRequestTime = sent;
308+
this.process(response);
309+
this.loading = false;
310+
},
311+
err => {
312+
console.log('ERR', err);
313+
this.lastRequestTime = sent;
314+
this.loading = false;
315+
316+
this.error = err; //.data.error + " ["+err.status+"]";
317+
this.inspector = {error: err};
318+
this.showError('Request Error', err);
319+
}
320+
);
321+
});
279322
}
280323

281324
// Return empty results
282325
return null; //this.$q.when( [] );
283326
}
284327

285-
// Overrides the default handling
328+
// Overrides the default handling (error for null result)
286329
handleQueryResult(result) {
287-
//console.log('handleQueryResult', Date.now(), this.loading);
330+
// console.log('handleQueryResult', result, Date.now(), this.loading);
331+
this.loading = false;
332+
if (result) {
333+
if (this.isUsingMetricQuery()) {
334+
return super.handleQueryResult(result);
335+
}
336+
}
288337
this.render();
289338
}
290339

291340
onPanelInitalized() {
292341
this.updateFN();
293342
this.updateTemplate();
294-
this.datasourceChanged(null);
295343
$(window).on(
296344
'resize',
297345
_.debounce(fn => {
298346
this.refresh();
299-
}, 150)
347+
}, 250)
300348
);
301349
}
302350

@@ -307,62 +355,25 @@ class AjaxCtrl extends MetricsPanelCtrl {
307355

308356
onInitEditMode() {
309357
this.debugParams = {};
310-
this.editorTabs.splice(1, 1); // remove the 'Metrics Tab'
311358
this.addEditorTab(
312359
'Request',
313360
'public/plugins/' + this.pluginId + '/partials/editor.request.html',
314-
1
361+
2
315362
);
316363
this.addEditorTab(
317364
'Display',
318365
'public/plugins/' + this.pluginId + '/partials/editor.display.html',
319-
2
366+
3
320367
);
321368
this.addEditorTab(
322369
'Examples',
323370
'public/plugins/' + this.pluginId + '/partials/editor.examples.html',
324-
4
371+
5
325372
);
326-
this.editorTabIndex = 1;
373+
this.editorTabIndex = 2;
327374
this.updateFN();
328375
}
329376

330-
getDatasourceOptions() {
331-
return Promise.resolve(
332-
this.datasourceSrv
333-
.getMetricSources()
334-
// .filter(value => {
335-
// return !value.meta.builtIn; // skip mixed and 'grafana'?
336-
// })
337-
.map(ds => {
338-
return {value: ds.value, text: ds.name, datasource: ds};
339-
})
340-
);
341-
}
342-
343-
// This saves the info we need from the datasouce
344-
datasourceChanged(option) {
345-
if (option && option.datasource) {
346-
this.setDatasource(option.datasource);
347-
}
348-
349-
if (this.panel.useDatasource) {
350-
if (!this.panel.datasource) {
351-
this.panel.datasource = 'default';
352-
}
353-
354-
this.datasourceSrv.get(this.panel.datasource).then(ds => {
355-
if (ds) {
356-
this.dsInfo = new DSInfo(ds);
357-
}
358-
this.onConfigChanged();
359-
});
360-
} else {
361-
this.dsInfo = undefined;
362-
this.onConfigChanged();
363-
}
364-
}
365-
366377
updateFN() {
367378
this.fn_error = null;
368379
this.params_fn = undefined;
@@ -410,7 +421,11 @@ class AjaxCtrl extends MetricsPanelCtrl {
410421
if (!this.panel.mode) {
411422
this.panel.mode = RenderMode.html;
412423
}
413-
switch (this.panel.mode) {
424+
let mode = this.panel.mode;
425+
if (mode === RenderMode.html && this.isUsingMetricQuery()) {
426+
mode = RenderMode.pre; // don't show [object object]!
427+
}
428+
switch (mode) {
414429
case RenderMode.html:
415430
txt = '<div ng-bind-html="response"></div>';
416431
break;

0 commit comments

Comments
 (0)