From 50fd5512d6b069ac9b0c981d43568d85eb0dd589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sun, 2 Mar 2014 10:41:16 +0100 Subject: [PATCH] more work on influxdb datasource, query editor, and refactoring --- src/app/components/settings.js | 7 +- src/app/panels/graphite/module.html | 10 -- src/app/panels/graphite/module.js | 11 +- src/app/partials/graphite/editor.html | 5 +- src/app/partials/influxdb/editor.html | 41 +++--- src/app/partials/metrics.html | 17 +++ .../services/graphite/graphiteDatasource.js | 1 + .../services/influxdb/influxdbDatasource.js | 118 ++++++++++-------- 8 files changed, 121 insertions(+), 89 deletions(-) create mode 100644 src/app/partials/metrics.html diff --git a/src/app/components/settings.js b/src/app/components/settings.js index b32b7d65771..18fbf5e48a6 100644 --- a/src/app/components/settings.js +++ b/src/app/components/settings.js @@ -52,14 +52,17 @@ function (_, crypto) { if (options.graphiteUrl) { settings.datasources = { graphite: { - name: 'default', + type: 'graphite', url: options.graphiteUrl, default: true } }; } - _.map(settings.datasources, parseBasicAuth); + _.each(settings.datasources, function(datasource, key) { + datasource.name = key; + parseBasicAuth(datasource); + }); var elasticParsed = parseBasicAuth({ url: settings.elasticsearch }); settings.elasticsearchBasicAuth = elasticParsed.basicAuth; diff --git a/src/app/panels/graphite/module.html b/src/app/panels/graphite/module.html index 4c56568b993..caedc6b7122 100644 --- a/src/app/panels/graphite/module.html +++ b/src/app/panels/graphite/module.html @@ -27,16 +27,6 @@
- -
-
-
Datasource options
-
- - -
-
-
\ No newline at end of file diff --git a/src/app/panels/graphite/module.js b/src/app/panels/graphite/module.js index b678801739b..8fd982bddf6 100644 --- a/src/app/panels/graphite/module.js +++ b/src/app/panels/graphite/module.js @@ -46,6 +46,7 @@ function (angular, app, $, _, kbn, moment, timeSeries) { }, { title: 'Metrics', + src:'app/partials/metrics.html' }, { title:'Axes & Grid', @@ -206,12 +207,12 @@ function (angular, app, $, _, kbn, moment, timeSeries) { $scope.hiddenSeries = {}; $scope.datasources = datasourceSrv.listOptions(); - $scope.datasourceChanged(); + $scope.setDatasource($scope.panel.datasource); }; - $scope.datasourceChanged = function() { - $scope.datasource = datasourceSrv.get($scope.panel.datasource); - $scope.panelMeta.fullEditorTabs[1].src = $scope.datasource.editorSrc; + $scope.setDatasource = function(datasource) { + $scope.panel.datasource = datasource; + $scope.datasource = datasourceSrv.get(datasource); $scope.get_data(); }; @@ -299,6 +300,8 @@ function (angular, app, $, _, kbn, moment, timeSeries) { yaxis: yaxis }; + $scope.legend.push(seriesInfo); + var series = new timeSeries.ZeroFilled({ datapoints: datapoints, info: seriesInfo, diff --git a/src/app/partials/graphite/editor.html b/src/app/partials/graphite/editor.html index 95725085c00..c1304bce136 100644 --- a/src/app/partials/graphite/editor.html +++ b/src/app/partials/graphite/editor.html @@ -1,5 +1,5 @@ -
+
-
- -
\ No newline at end of file diff --git a/src/app/partials/influxdb/editor.html b/src/app/partials/influxdb/editor.html index eea3066e3c7..eacbdd5c9f4 100644 --- a/src/app/partials/influxdb/editor.html +++ b/src/app/partials/influxdb/editor.html @@ -1,6 +1,5 @@ -
InfluxDB queries
-
+
-
-
- -
\ No newline at end of file diff --git a/src/app/partials/metrics.html b/src/app/partials/metrics.html new file mode 100644 index 00000000000..cc5446e12d0 --- /dev/null +++ b/src/app/partials/metrics.html @@ -0,0 +1,17 @@ +
+ + +
+ + +
+ + + +
+ +
diff --git a/src/app/services/graphite/graphiteDatasource.js b/src/app/services/graphite/graphiteDatasource.js index 423930d04ed..2d3ca39c84e 100644 --- a/src/app/services/graphite/graphiteDatasource.js +++ b/src/app/services/graphite/graphiteDatasource.js @@ -18,6 +18,7 @@ function (angular, _, $, config, kbn, moment) { this.basicAuth = datasource.basicAuth; this.url = datasource.url; this.editorSrc = 'app/partials/graphite/editor.html'; + this.name = datasource.name; } GraphiteDatasource.prototype.query = function(options) { diff --git a/src/app/services/influxdb/influxdbDatasource.js b/src/app/services/influxdb/influxdbDatasource.js index 7b017c2ce1e..74fe54cf300 100644 --- a/src/app/services/influxdb/influxdbDatasource.js +++ b/src/app/services/influxdb/influxdbDatasource.js @@ -1,8 +1,9 @@ define([ 'angular', 'underscore', + 'kbn' ], -function (angular, _) { +function (angular, _, kbn) { 'use strict'; var module = angular.module('kibana.services'); @@ -15,6 +16,7 @@ function (angular, _) { this.url = datasource.url; this.username = datasource.username; this.password = datasource.password; + this.name = datasource.name; this.templateSettings = { interpolate : /\[\[([\s\S]+?)\]\]/g, @@ -22,49 +24,32 @@ function (angular, _) { } InfluxDatasource.prototype.query = function(options) { - var target = options.targets[0]; - var template = "select [[func]]([[column]]) from [[series]] where [[timeFilter]] group by time([[interval]])"; - var templateData = { - series: target.series, - column: target.column, - func: target.function, - timeFilter: getTimeFilter(options), - interval: options.interval - }; + var promises = _.map(options.targets, function(target) { + if (!target.series || !target.column || target.hide) { + return []; + } - var query = _.template(template, templateData, this.templateSettings); - console.log(query); + var template = "select [[func]]([[column]]) from [[series]] where [[timeFilter]] group by time([[interval]]) order asc"; - var output = { data: [] }; + var templateData = { + series: target.series, + column: target.column, + func: target.function, + timeFilter: getTimeFilter(options), + interval: target.interval || options.interval + }; - return this.doInfluxRequest(query).then(function(results) { + var query = _.template(template, templateData, this.templateSettings); + console.log(query); - _.each(results.data, function(series) { - var timeCol = series.columns.indexOf('time'); + return this.doInfluxRequest(query).then(handleInfluxQueryResponse); - _.each(series.columns, function(column, index) { - if (column === "time" || column === "sequence_number") { - return; - } + }, this); - console.log("series:"+series.name + ": "+series.points.length + " points"); + return $q.all(promises).then(function(results) { - var target = series.name + "." + column; - var datapoints = []; - - var i, y; - for(i = series.points.length - 1, y = 0; i >= 0; i--, y++) { - var t = Math.floor(series.points[i][timeCol] / 1000); - var v = series.points[i][index]; - datapoints[y] = [v,t]; - } - - output.data.push({ target:target, datapoints:datapoints }); - }); - }); - - return output; + return { data: _.flatten(results) }; }); }; @@ -85,24 +70,59 @@ function (angular, _) { return $http(options); }; + function handleInfluxQueryResponse(results) { + var output = []; + + _.each(results.data, function(series) { + var timeCol = series.columns.indexOf('time'); + + _.each(series.columns, function(column, index) { + if (column === "time" || column === "sequence_number") { + return; + } + + console.log("series:"+series.name + ": "+series.points.length + " points"); + + var target = series.name + "." + column; + var datapoints = []; + + for(var i = 0; i < series.points.length; i++) { + var t = Math.floor(series.points[i][timeCol] / 1000); + var v = series.points[i][index]; + datapoints[i] = [v,t]; + } + + output.push({ target:target, datapoints:datapoints }); + }); + }); + + return output; + } + function getTimeFilter(options) { - var from = options.range.from; - var until = options.range.to; + var from = getInfluxTime(options.range.from); + var until = getInfluxTime(options.range.to); - if (_.isString(from)) { - return 'time > now() - ' + from.substring(4); - } - else { - from = to_utc_epoch_seconds(from); + if (until === 'now()') { + return 'time > now() - ' + from; } - if (until === 'now') { - return 'time > ' + from; - } - else { - until = to_utc_epoch_seconds(until); - return 'time > ' + from + ' and time < ' + until; + return 'time > ' + from + ' and time < ' + until; + } + + function getInfluxTime(date) { + if (_.isString(date)) { + if (date === 'now') { + return 'now()'; + } + else if (date.indexOf('now') >= 0) { + return date.substring(4); + } + + date = kbn.parseDate(date); } + + return to_utc_epoch_seconds(date); } function to_utc_epoch_seconds(date) {