-
Notifications
You must be signed in to change notification settings - Fork 11
/
datasource.js
290 lines (235 loc) · 7.9 KB
/
datasource.js
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
define([
'angular',
'lodash',
'app/core/utils/datemath',
'./influx_series',
'./query_builder',
'./query_ctrl',
'./func_editor',
],
function (angular, _, dateMath, InfluxSeries, InfluxQueryBuilder) {
'use strict';
var self;
function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv) {
this.urls = _.map(instanceSettings.url.split(','), function(url) {
return url.trim();
});
this.username = instanceSettings.username;
this.password = instanceSettings.password;
this.name = instanceSettings.name;
this.basicAuth = instanceSettings.basicAuth;
this.q = $q;
this.backendSrv = backendSrv;
this.templateSrv = templateSrv;
self = this;
}
InfluxDatasource.prototype.query = function(options) {
var timeFilter = getTimeFilter(options);
var promises = _.map(options.targets, function(target) {
if (target.hide || !((target.series && target.column) || target.query)) {
return [];
}
// build query
var queryBuilder = new InfluxQueryBuilder(target);
var query = queryBuilder.build();
// replace grafana variables
query = query.replace('$timeFilter', timeFilter);
query = query.replace(/\$interval/g, (target.interval || options.interval));
// replace templated variables
query = self.templateSrv.replace(query, options.scopedVars);
var alias = target.alias ? self.templateSrv.replace(target.alias, options.scopedVars) : '';
var handleResponse = _.partial(handleInfluxQueryResponse, alias, queryBuilder.groupByField);
return this._seriesQuery(query).then(handleResponse);
}, this);
return this.q.all(promises).then(function(results) {
return { data: _.flatten(results) };
});
};
InfluxDatasource.prototype.annotationQuery = function(options) {
var timeFilter = getTimeFilter({rangeRaw: options.rangeRaw});
var query = options.annotation.query.replace('$timeFilter', timeFilter);
query = self.templateSrv.replace(query);
return this._seriesQuery(query).then(function(results) {
return new InfluxSeries({seriesList: results, annotation: options.annotation}).getAnnotations();
});
};
InfluxDatasource.prototype.listColumns = function(seriesName) {
seriesName = self.templateSrv.replace(seriesName);
if(!seriesName.match('^/.*/') && !seriesName.match(/^merge\(.*\)/)) {
seriesName = '"' + seriesName+ '"';
}
return this._seriesQuery('select * from ' + seriesName + ' limit 1').then(function(data) {
if (!data) {
return [];
}
return data[0].columns.map(function(item) {
return /^\w+$/.test(item) ? item : ('"' + item + '"');
});
});
};
InfluxDatasource.prototype.listSeries = function(query) {
// wrap in regex
if (query && query.length > 0 && query[0] !== '/') {
query = '/' + query + '/';
}
return this._seriesQuery('list series ' + query).then(function(data) {
if (!data || data.length === 0) {
return [];
}
return _.map(data[0].points, function(point) {
return point[1];
});
});
};
InfluxDatasource.prototype.testDatasource = function() {
return this.metricFindQuery('list series').then(function () {
return { status: "success", message: "Data source is working", title: "Success" };
});
};
InfluxDatasource.prototype.metricFindQuery = function (query) {
var interpolated;
try {
interpolated = this.templateSrv.replace(query);
}
catch (err) {
return this.q.reject(err);
}
return this._seriesQuery(interpolated)
.then(function (results) {
if (!results || results.length === 0) { return []; }
return _.map(results[0].points, function (metric) {
return {
text: metric[1],
expandable: false
};
});
});
};
function retry(deferred, callback, delay) {
return callback().then(undefined, function(reason) {
if (reason.status !== 0 || reason.status >= 300) {
reason.message = 'InfluxDB Error: <br/>' + reason.data;
deferred.reject(reason);
}
else {
setTimeout(function() {
return retry(deferred, callback, Math.min(delay * 2, 30000));
}, delay);
}
});
}
InfluxDatasource.prototype._seriesQuery = function(query) {
return this._influxRequest('GET', '/series', {
q: query,
});
};
InfluxDatasource.prototype._influxRequest = function(method, url, data) {
var _this = this;
var deferred = this.q.defer();
retry(deferred, function() {
var currentUrl = _this.urls.shift();
_this.urls.push(currentUrl);
var params = {
u: _this.username,
p: _this.password,
};
if (method === 'GET') {
_.extend(params, data);
data = null;
}
var options = {
method: method,
url: currentUrl + url,
params: params,
data: data,
inspect: { type: 'influxdb' },
};
options.headers = options.headers || {};
if (_this.basicAuth) {
options.headers.Authorization = 'Basic ' + _this.basicAuth;
}
return _this.backendSrv.datasourceRequest(options).then(function(response) {
deferred.resolve(response.data);
});
}, 10);
return deferred.promise;
};
InfluxDatasource.prototype._getDashboardInternal = function(id) {
var queryString = 'select dashboard from "grafana.dashboard_' + btoa(id) + '"';
return this._seriesQuery(queryString).then(function(results) {
if (!results || !results.length) {
return null;
}
var dashCol = _.indexOf(results[0].columns, 'dashboard');
var dashJson = results[0].points[0][dashCol];
return angular.fromJson(dashJson);
}, function() {
return null;
});
};
InfluxDatasource.prototype.getDashboard = function(id) {
return this._getDashboardInternal(id).then(function(dashboard) {
if (dashboard !== null) {
return dashboard;
}
throw "Dashboard not found";
}, function(err) {
throw "Could not load dashboard, " + err.data;
});
};
InfluxDatasource.prototype.searchDashboards = function() {
var influxQuery = 'select * from /grafana.dashboard_.*/ ';
return this._seriesQuery(influxQuery).then(function(results) {
var hits = { dashboards: [], tags: [], tagsOnly: false };
if (!results || !results.length) {
return hits;
}
for (var i = 0; i < results.length; i++) {
var dashCol = _.indexOf(results[i].columns, 'title');
var idCol = _.indexOf(results[i].columns, 'id');
var hit = {
id: results[i].points[0][dashCol],
title: results[i].points[0][dashCol],
};
if (idCol !== -1) {
hit.id = results[i].points[0][idCol];
}
hits.dashboards.push(hit);
}
return hits;
});
};
function handleInfluxQueryResponse(alias, groupByField, seriesList) {
var influxSeries = new InfluxSeries({
seriesList: seriesList,
alias: alias,
groupByField: groupByField
});
return influxSeries.getTimeSeries();
}
function getTimeFilter(options) {
var from = getInfluxTime(options.rangeRaw.from, false);
var until = getInfluxTime(options.rangeRaw.to, true);
var fromIsAbsolute = from[from.length-1] === 's';
if (until === 'now()' && !fromIsAbsolute) {
return 'time > ' + from;
}
return 'time > ' + from + ' and time < ' + until;
}
function getInfluxTime(date, roundUp) {
if (_.isString(date)) {
if (date === 'now') {
return 'now()';
}
var parts = /^now-(\d+)([d|h|m|s])$/.exec(date);
if (parts) {
var amount = parseInt(parts[1]);
var unit = parts[2];
return 'now()-' + amount + unit;
}
date = dateMath.parse(date, roundUp);
}
return (date.valueOf() / 1000).toFixed(0) + 's';
}
return InfluxDatasource;
});