@@ -41,7 +41,6 @@ class AjaxCtrl extends MetricsPanelCtrl {
41
41
public $rootScope ,
42
42
public $q ,
43
43
public $timeout ,
44
- public $http ,
45
44
public $sce ,
46
45
public templateSrv ,
47
46
public datasourceSrv ,
@@ -50,6 +49,11 @@ class AjaxCtrl extends MetricsPanelCtrl {
50
49
) {
51
50
super ( $scope , $injector ) ;
52
51
52
+ // Migrate old settings
53
+ if ( this . panel . useDatasource ) {
54
+ this . panel . request = 'datasource' ;
55
+ delete this . panel . useDatasource ;
56
+ }
53
57
_ . defaults ( this . panel , examples [ 0 ] . config ) ;
54
58
55
59
$scope . $on ( '$destroy' , ( ) => {
@@ -58,11 +62,29 @@ class AjaxCtrl extends MetricsPanelCtrl {
58
62
}
59
63
} ) ;
60
64
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
+
61
69
this . events . on ( 'init-edit-mode' , this . onInitEditMode . bind ( this ) ) ;
62
70
this . events . on ( 'panel-initialized' , this . onPanelInitalized . bind ( this ) ) ;
63
71
this . events . on ( 'render' , this . notifyWhenRenderingCompleted . bind ( this ) ) ;
64
72
}
65
73
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
+
66
88
// This checks that all requests have completed before saying
67
89
notifyWhenRenderingCompleted ( ) {
68
90
if ( this . timer ) {
@@ -93,6 +115,10 @@ class AjaxCtrl extends MetricsPanelCtrl {
93
115
return examples ;
94
116
}
95
117
118
+ isUsingMetricQuery ( ) {
119
+ return this . panel . request . startsWith ( 'query' ) ;
120
+ }
121
+
96
122
loadExample ( example : any , evt ?: any ) {
97
123
if ( evt ) {
98
124
evt . stopPropagation ( ) ;
@@ -120,7 +146,6 @@ class AjaxCtrl extends MetricsPanelCtrl {
120
146
this . $scope . response = null ;
121
147
this . updateFN ( ) ;
122
148
this . updateTemplate ( ) ;
123
- this . datasourceChanged ( null ) ;
124
149
this . refresh ( ) ;
125
150
}
126
151
@@ -170,19 +195,23 @@ class AjaxCtrl extends MetricsPanelCtrl {
170
195
* @override
171
196
*/
172
197
updateTimeRange ( datasource ?) {
173
- // Keep the timeinfo even after updating the range
174
198
const before = this . timeInfo ;
175
- super . updateTimeRange ( ) ;
199
+ const v = super . updateTimeRange ( datasource ) ;
176
200
if ( this . panel . showTime && before ) {
177
201
this . timeInfo = before ;
178
202
}
203
+ return v ;
179
204
}
180
205
181
206
/**
182
207
* Rather than issue a datasource query, we will call our ajax request
183
208
* @override
184
209
*/
185
210
issueQueries ( datasource ) {
211
+ if ( this . isUsingMetricQuery ( ) ) {
212
+ return super . issueQueries ( datasource ) ;
213
+ }
214
+
186
215
if ( this . fn_error ) {
187
216
this . loading = false ;
188
217
this . error = this . fn_error ;
@@ -239,14 +268,26 @@ class AjaxCtrl extends MetricsPanelCtrl {
239
268
} ;
240
269
options . headers = options . headers || { } ;
241
270
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
+ } ) ;
250
291
} else if ( ! options . url ) {
251
292
this . error = 'Missing URL' ;
252
293
this . showError ( this . error , null ) ;
@@ -260,43 +301,50 @@ class AjaxCtrl extends MetricsPanelCtrl {
260
301
// Now make the call
261
302
this . requestCount ++ ;
262
303
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
+ } ) ;
279
322
}
280
323
281
324
// Return empty results
282
325
return null ; //this.$q.when( [] );
283
326
}
284
327
285
- // Overrides the default handling
328
+ // Overrides the default handling (error for null result)
286
329
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
+ }
288
337
this . render ( ) ;
289
338
}
290
339
291
340
onPanelInitalized ( ) {
292
341
this . updateFN ( ) ;
293
342
this . updateTemplate ( ) ;
294
- this . datasourceChanged ( null ) ;
295
343
$ ( window ) . on (
296
344
'resize' ,
297
345
_ . debounce ( fn => {
298
346
this . refresh ( ) ;
299
- } , 150 )
347
+ } , 250 )
300
348
) ;
301
349
}
302
350
@@ -307,62 +355,25 @@ class AjaxCtrl extends MetricsPanelCtrl {
307
355
308
356
onInitEditMode ( ) {
309
357
this . debugParams = { } ;
310
- this . editorTabs . splice ( 1 , 1 ) ; // remove the 'Metrics Tab'
311
358
this . addEditorTab (
312
359
'Request' ,
313
360
'public/plugins/' + this . pluginId + '/partials/editor.request.html' ,
314
- 1
361
+ 2
315
362
) ;
316
363
this . addEditorTab (
317
364
'Display' ,
318
365
'public/plugins/' + this . pluginId + '/partials/editor.display.html' ,
319
- 2
366
+ 3
320
367
) ;
321
368
this . addEditorTab (
322
369
'Examples' ,
323
370
'public/plugins/' + this . pluginId + '/partials/editor.examples.html' ,
324
- 4
371
+ 5
325
372
) ;
326
- this . editorTabIndex = 1 ;
373
+ this . editorTabIndex = 2 ;
327
374
this . updateFN ( ) ;
328
375
}
329
376
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
-
366
377
updateFN ( ) {
367
378
this . fn_error = null ;
368
379
this . params_fn = undefined ;
@@ -410,7 +421,11 @@ class AjaxCtrl extends MetricsPanelCtrl {
410
421
if ( ! this . panel . mode ) {
411
422
this . panel . mode = RenderMode . html ;
412
423
}
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 ) {
414
429
case RenderMode . html :
415
430
txt = '<div ng-bind-html="response"></div>' ;
416
431
break ;
0 commit comments