Commit 528f5415 by Torkel Ödegaard

More work on experimental InfluxDB 0.9 support

parent ae7f18f9
......@@ -25,10 +25,6 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) {
this.name = datasource.name;
this.database = datasource.database;
this.basicAuth = datasource.basicAuth;
this.grafanaDB = datasource.grafanaDB;
this.saveTemp = _.isUndefined(datasource.save_temp) ? true : datasource.save_temp;
this.saveTempTTL = _.isUndefined(datasource.save_temp_ttl) ? '30d' : datasource.save_temp_ttl;
this.supportAnnotations = true;
this.supportMetrics = true;
......@@ -196,177 +192,6 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) {
return deferred.promise;
};
InfluxDatasource.prototype.saveDashboard = function(dashboard) {
var tags = dashboard.tags.join(',');
var title = dashboard.title;
var temp = dashboard.temp;
var id = kbn.slugifyForUrl(title);
if (temp) { delete dashboard.temp; }
var data = [{
name: 'grafana.dashboard_' + btoa(id),
columns: ['time', 'sequence_number', 'title', 'tags', 'dashboard', 'id'],
points: [[1000000000000, 1, title, tags, angular.toJson(dashboard), id]]
}];
if (temp) {
return this._saveDashboardTemp(data, title, id);
}
else {
var self = this;
return this._influxRequest('POST', '/series', data).then(function() {
self._removeUnslugifiedDashboard(id, title, false);
return { title: title, url: '/dashboard/db/' + id };
}, function(err) {
throw 'Failed to save dashboard to InfluxDB: ' + err.data;
});
}
};
InfluxDatasource.prototype._removeUnslugifiedDashboard = function(id, title, isTemp) {
if (id === title) { return; }
var self = this;
self._getDashboardInternal(title, isTemp).then(function(dashboard) {
if (dashboard !== null) {
self.deleteDashboard(title);
}
});
};
InfluxDatasource.prototype._saveDashboardTemp = function(data, title, id) {
data[0].name = 'grafana.temp_dashboard_' + btoa(id);
data[0].columns.push('expires');
data[0].points[0].push(this._getTempDashboardExpiresDate());
return this._influxRequest('POST', '/series', data).then(function() {
var baseUrl = window.location.href.replace(window.location.hash,'');
var url = baseUrl + "#dashboard/temp/" + id;
return { title: title, url: url };
}, function(err) {
throw 'Failed to save shared dashboard to InfluxDB: ' + err.data;
});
};
InfluxDatasource.prototype._getTempDashboardExpiresDate = function() {
var ttlLength = this.saveTempTTL.substring(0, this.saveTempTTL.length - 1);
var ttlTerm = this.saveTempTTL.substring(this.saveTempTTL.length - 1, this.saveTempTTL.length).toLowerCase();
var expires = Date.now();
switch(ttlTerm) {
case "m":
expires += ttlLength * 60000;
break;
case "d":
expires += ttlLength * 86400000;
break;
case "w":
expires += ttlLength * 604800000;
break;
default:
throw "Unknown ttl duration format";
}
return expires;
};
InfluxDatasource.prototype._getDashboardInternal = function(id, isTemp) {
var queryString = 'select dashboard from "grafana.dashboard_' + btoa(id) + '"';
if (isTemp) {
queryString = 'select dashboard from "grafana.temp_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, isTemp) {
var self = this;
return this._getDashboardInternal(id, isTemp).then(function(dashboard) {
if (dashboard !== null) {
return dashboard;
}
// backward compatible load for unslugified ids
var slug = kbn.slugifyForUrl(id);
if (slug !== id) {
return self.getDashboard(slug, isTemp);
}
throw "Dashboard not found";
}, function(err) {
throw "Could not load dashboard, " + err.data;
});
};
InfluxDatasource.prototype.deleteDashboard = function(id) {
return this._seriesQuery('drop series "grafana.dashboard_' + btoa(id) + '"').then(function(results) {
if (!results) {
throw "Could not delete dashboard";
}
return id;
}, function(err) {
throw "Could not delete dashboard, " + err.data;
});
};
InfluxDatasource.prototype.searchDashboards = function(queryString) {
var influxQuery = 'select * from /grafana.dashboard_.*/ where ';
var tagsOnly = queryString.indexOf('tags!:') === 0;
if (tagsOnly) {
var tagsQuery = queryString.substring(6, queryString.length);
influxQuery = influxQuery + 'tags =~ /.*' + tagsQuery + '.*/i';
}
else {
var titleOnly = queryString.indexOf('title:') === 0;
if (titleOnly) {
var titleQuery = queryString.substring(6, queryString.length);
influxQuery = influxQuery + ' title =~ /.*' + titleQuery + '.*/i';
}
else {
influxQuery = influxQuery + '(tags =~ /.*' + queryString + '.*/i or title =~ /.*' + queryString + '.*/i)';
}
}
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 tagsCol = _.indexOf(results[i].columns, 'tags');
var idCol = _.indexOf(results[i].columns, 'id');
var hit = {
id: results[i].points[0][dashCol],
title: results[i].points[0][dashCol],
tags: results[i].points[0][tagsCol].split(",")
};
if (idCol !== -1) {
hit.id = results[i].points[0][idCol];
}
hit.tags = hit.tags[0] ? hit.tags : [];
hits.dashboards.push(hit);
}
return hits;
});
};
function handleInfluxQueryResponse(alias, seriesList) {
var influxSeries = new InfluxSeries({ seriesList: seriesList, alias: alias });
return influxSeries.getTimeSeries();
......@@ -386,7 +211,7 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) {
function getInfluxTime(date) {
if (_.isString(date)) {
return date.replace('now', 'now()');
return date.replace('now', 'now()').replace('-', ' - ');
}
return to_utc_epoch_seconds(date);
......
......@@ -28,7 +28,17 @@ function (_) {
for (var i = 0; i < series.values.length; i++) {
datapoints[i] = [series.values[i][1], new Date(series.values[i][0]).getTime()];
}
output.push({ target: series.name, datapoints: datapoints });
var seriesName = series.name;
var tags = _.map(series.tags, function(value, key) {
return key + ': ' + value;
});
if (tags.length > 0) {
seriesName = seriesName + ' {' + tags.join(', ') + '}';
}
output.push({ target: seriesName, datapoints: datapoints });
});
return output;
......
......@@ -30,8 +30,8 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) {
this.supportAnnotations = true;
this.supportMetrics = true;
this.editorSrc = 'app/features/influxdb/partials/query.editor.html';
this.annotationEditorSrc = 'app/features/influxdb/partials/annotations.editor.html';
this.editorSrc = 'app/features/influxdb_08/partials/query.editor.html';
this.annotationEditorSrc = 'app/features/influxdb_08/partials/annotations.editor.html';
}
InfluxDatasource.prototype.query = function(options) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment