Skip to content

Commit 0c9a19e

Browse files
committed
Merge branch 'ds-template-var'
2 parents c4440ea + 8b4c7c9 commit 0c9a19e

File tree

11 files changed

+239
-95
lines changed

11 files changed

+239
-95
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Enhancements
44
* **Singlestat**: Support for gauges in singlestat panel. closes [#3688](https://github.com/grafana/grafana/pull/3688)
5+
* **Templating**: Support for data source as variable, closes [#816](https://github.com/grafana/grafana/pull/816)
56

67
### Bug fixes
78
* **InfluxDB 0.12**: Fixed issue templating and `show tag values` query only returning tags for first measurement, fixes [#4726](https://github.com/grafana/grafana/issues/4726)

public/app/core/components/info_popover.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export function infoPopover() {
1111
template: '<i class="fa fa-info-circle"></i>',
1212
transclude: true,
1313
link: function(scope, elem, attrs, ctrl, transclude) {
14-
1514
var offset = attrs.offset || '0 -10px';
1615
var position = attrs.position || 'right middle';
1716
var classes = 'drop-help drop-hide-out-of-bounds';
@@ -39,6 +38,7 @@ export function infoPopover() {
3938
position: position,
4039
classes: classes,
4140
openOn: openOn,
41+
hoverOpenDelay: 400,
4242
tetherOptions: {
4343
offset: offset
4444
}

public/app/core/services/datasource_srv.js

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,20 @@ define([
77
function (angular, _, coreModule, config) {
88
'use strict';
99

10-
coreModule.default.service('datasourceSrv', function($q, $injector, $rootScope) {
10+
coreModule.default.service('datasourceSrv', function($q, $injector, $rootScope, templateSrv) {
1111
var self = this;
1212

1313
this.init = function() {
1414
this.datasources = {};
15-
this.metricSources = [];
16-
this.annotationSources = [];
17-
18-
_.each(config.datasources, function(value, key) {
19-
if (value.meta && value.meta.metrics) {
20-
self.metricSources.push({
21-
value: key === config.defaultDatasource ? null : key,
22-
name: key,
23-
meta: value.meta,
24-
});
25-
}
26-
if (value.meta && value.meta.annotations) {
27-
self.annotationSources.push(value);
28-
}
29-
});
30-
31-
this.metricSources.sort(function(a, b) {
32-
if (a.meta.builtIn || a.name > b.name) {
33-
return 1;
34-
}
35-
if (a.name < b.name) {
36-
return -1;
37-
}
38-
return 0;
39-
});
4015
};
4116

4217
this.get = function(name) {
4318
if (!name) {
4419
return this.get(config.defaultDatasource);
4520
}
4621

22+
name = templateSrv.replace(name);
23+
4724
if (this.datasources[name]) {
4825
return $q.when(this.datasources[name]);
4926
}
@@ -89,11 +66,61 @@ function (angular, _, coreModule, config) {
8966
};
9067

9168
this.getAnnotationSources = function() {
92-
return this.annotationSources;
69+
return _.reduce(config.datasources, function(memo, key, value) {
70+
71+
if (value.meta && value.meta.annotations) {
72+
memo.push(value);
73+
}
74+
75+
return memo;
76+
}, []);
9377
};
9478

95-
this.getMetricSources = function() {
96-
return this.metricSources;
79+
this.getMetricSources = function(options) {
80+
var metricSources = [];
81+
82+
_.each(config.datasources, function(value, key) {
83+
if (value.meta && value.meta.metrics) {
84+
metricSources.push({
85+
value: key === config.defaultDatasource ? null : key,
86+
name: key,
87+
meta: value.meta,
88+
});
89+
}
90+
});
91+
92+
if (!options || !options.skipVariables) {
93+
// look for data source variables
94+
for (var i = 0; i < templateSrv.variables.length; i++) {
95+
var variable = templateSrv.variables[i];
96+
if (variable.type !== 'datasource') {
97+
continue;
98+
}
99+
100+
var first = variable.current.value;
101+
var ds = config.datasources[first];
102+
103+
if (ds) {
104+
metricSources.push({
105+
name: '$' + variable.name,
106+
value: '$' + variable.name,
107+
meta: ds.meta,
108+
});
109+
}
110+
}
111+
}
112+
113+
metricSources.sort(function(a, b) {
114+
if (a.meta.builtIn || a.name > b.name) {
115+
return 1;
116+
}
117+
if (a.name < b.name) {
118+
return -1;
119+
}
120+
return 0;
121+
});
122+
123+
return metricSources;
97124
};
98125

99126
this.init();

public/app/core/services/segment_srv.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function (angular, _, coreModule) {
1919

2020
if (_.isString(options)) {
2121
this.value = options;
22-
this.html = $sce.trustAsHtml(this.value);
22+
this.html = $sce.trustAsHtml(templateSrv.highlightVariablesAsHtml(this.value));
2323
return;
2424
}
2525

public/app/features/panel/metrics_ds_selector.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export class MetricsDsSelectorCtrl {
6363
}
6464
}
6565

66+
if (!this.current) {
67+
this.current = {name: dsValue + ' not found', value: null};
68+
}
69+
6670
this.dsSegment = uiSegmentSrv.newSegment(this.current.name);
6771
}
6872

public/app/features/panel/metrics_panel_ctrl.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class MetricsPanelCtrl extends PanelCtrl {
1515
error: boolean;
1616
loading: boolean;
1717
datasource: any;
18+
datasourceName: any;
1819
$q: any;
1920
$timeout: any;
2021
datasourceSrv: any;
@@ -244,6 +245,7 @@ class MetricsPanelCtrl extends PanelCtrl {
244245
}
245246

246247
this.panel.datasource = datasource.value;
248+
this.datasourceName = datasource.name;
247249
this.datasource = null;
248250
this.refresh();
249251
}

public/app/features/templating/editorCtrl.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ function (angular, _) {
2020
multi: false,
2121
};
2222

23+
$scope.variableTypes = [
24+
{value: "query", text: "Query"},
25+
{value: "interval", text: "Interval"},
26+
{value: "datasource", text: "Datasource"},
27+
{value: "custom", text: "Custom"},
28+
];
29+
2330
$scope.refreshOptions = [
2431
{value: 0, text: "Never"},
2532
{value: 1, text: "On Dashboard Load"},
@@ -35,10 +42,16 @@ function (angular, _) {
3542
$scope.init = function() {
3643
$scope.mode = 'list';
3744

45+
$scope.datasourceTypes = {};
3846
$scope.datasources = _.filter(datasourceSrv.getMetricSources(), function(ds) {
47+
$scope.datasourceTypes[ds.meta.id] = {text: ds.meta.name, value: ds.meta.id};
3948
return !ds.meta.builtIn;
4049
});
4150

51+
$scope.datasourceTypes = _.map($scope.datasourceTypes, function(value) {
52+
return value;
53+
});
54+
4255
$scope.variables = templateSrv.variables;
4356
$scope.reset();
4457

@@ -132,9 +145,16 @@ function (angular, _) {
132145
if ($scope.current.type === 'interval') {
133146
$scope.current.query = '1m,10m,30m,1h,6h,12h,1d,7d,14d,30d';
134147
}
148+
135149
if ($scope.current.type === 'query') {
136150
$scope.current.query = '';
137151
}
152+
153+
if ($scope.current.type === 'datasource') {
154+
$scope.current.query = $scope.datasourceTypes[0].value;
155+
$scope.current.regex = '';
156+
$scope.current.refresh = 1;
157+
}
138158
};
139159

140160
$scope.removeVariable = function(variable) {

0 commit comments

Comments
 (0)