From cf2ef0955debf30c8000118c00e7b71b6a7a4a69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 17 Jul 2014 10:24:30 +0200 Subject: [PATCH] influxdb annotations working, need to figure out how to know which columns to use for title, tags, and data --- src/app/services/annotationsSrv.js | 15 +++---- .../services/graphite/graphiteDatasource.js | 2 +- src/app/services/influxdb/influxSeries.js | 42 +++++++++++++++++- .../services/influxdb/influxdbDatasource.js | 43 ++----------------- src/test/specs/influxSeries-specs.js | 34 +++++++++++++++ 5 files changed, 85 insertions(+), 51 deletions(-) diff --git a/src/app/services/annotationsSrv.js b/src/app/services/annotationsSrv.js index 1efa8c3ac20..bd27ad4b56a 100644 --- a/src/app/services/annotationsSrv.js +++ b/src/app/services/annotationsSrv.js @@ -49,16 +49,8 @@ define([ }; this.receiveAnnotationResults = function(results) { - console.log('Annotation result!', results); for (var i = 0; i < results.length; i++) { - var data = results[i]; - addAnnotation({ - annotation: data.annotation, - time: data.time, - description: data.description, - tags: data.tags, - data: data.text - }); + addAnnotation(results[i]); } }; @@ -143,14 +135,17 @@ define([ } function addAnnotation(options) { - var tooltip = "" + options.description + "
"; + var tooltip = "" + options.title + "
"; if (options.tags) { tooltip += (options.tags || '') + '
'; } + tooltip += '' + moment(options.time).format('YYYY-MM-DD HH:mm:ss') + '
'; + if (options.data) { tooltip += options.data.replace(/\n/g, '
'); } + tooltip += "
"; list.push({ diff --git a/src/app/services/graphite/graphiteDatasource.js b/src/app/services/graphite/graphiteDatasource.js index 1b0c5a2ac0a..4a8ac8f53f5 100644 --- a/src/app/services/graphite/graphiteDatasource.js +++ b/src/app/services/graphite/graphiteDatasource.js @@ -79,7 +79,7 @@ function (angular, _, $, config, kbn, moment) { list.push({ annotation: annotation, time: datapoint[1] * 1000, - description: target.target + title: target.target }); } } diff --git a/src/app/services/influxdb/influxSeries.js b/src/app/services/influxdb/influxSeries.js index bbbe603225c..17c7bb10085 100644 --- a/src/app/services/influxdb/influxSeries.js +++ b/src/app/services/influxdb/influxSeries.js @@ -8,6 +8,7 @@ function (_) { this.seriesList = options.seriesList; this.alias = options.alias; this.groupByField = options.groupByField; + this.annotation = options.annotation; } var p = InfluxSeries.prototype; @@ -65,6 +66,45 @@ function (_) { return output; }; + p.getAnnotations = function () { + var list = []; + var self = this; + + _.each(this.seriesList, function (series) { + var titleCol = 0; + var tagsCol = 0; + + _.each(series.columns, function(column, index) { + if (column === 'time' || column === 'sequence_number') { + return; + } + + if (!titleCol && column !== 'tags') { + titleCol = index; + } + else { + tagsCol = index; + } + }); + + _.each(series.points, function (point) { + var data = { + annotation: self.annotation, + time: point[0] * 1000, + title: point[titleCol] + }; + + if (tagsCol) { + data.tags = point[tagsCol]; + } + + list.push(data); + }); + }); + + return list; + }; + p.createNameForSeries = function(seriesName, groupByColValue) { var name = this.alias .replace('$s', seriesName); @@ -84,4 +124,4 @@ function (_) { }; return InfluxSeries; -}); \ No newline at end of file +}); diff --git a/src/app/services/influxdb/influxdbDatasource.js b/src/app/services/influxdb/influxdbDatasource.js index 31b92ef3d4f..ecc45f99d3a 100644 --- a/src/app/services/influxdb/influxdbDatasource.js +++ b/src/app/services/influxdb/influxdbDatasource.js @@ -121,46 +121,11 @@ function (angular, _, kbn, InfluxSeries) { InfluxDatasource.prototype.annotationQuery = function(annotation, filterSrv, rangeUnparsed) { var timeFilter = getTimeFilter({ range: rangeUnparsed }); - var query = _.template(annotation.query, { - timeFilter: timeFilter - }, this.templateSettings); + var query = _.template(annotation.query, { timeFilter: timeFilter }, this.templateSettings); - return this.doInfluxRequest(query) - .then(function (results) { - var list = []; - _.each(results, function (series) { - var descriptionCol = 0; - var tagsCol = 0; - _.each(series.columns, function(column, index) { - if (column === 'time' || column === 'sequence_number') { - return; - } - - if (!descriptionCol) { - descriptionCol = index; - } - else { - tagsCol = index; - } - }); - - _.each(series.points, function (point) { - var data = { - annotation: annotation, - time: point[0] * 1000, - description: point[descriptionCol] - }; - - if (tagsCol) { - data.tags = point[tagsCol]; - } - - list.push(data); - }); - }); - - return list; - }); + return this.doInfluxRequest(query).then(function(results) { + return new InfluxSeries({ seriesList: results, annotation: annotation }).getAnnotations(); + }); }; InfluxDatasource.prototype.listColumns = function(seriesName) { diff --git a/src/test/specs/influxSeries-specs.js b/src/test/specs/influxSeries-specs.js index e24ddb46a02..0787a834e37 100644 --- a/src/test/specs/influxSeries-specs.js +++ b/src/test/specs/influxSeries-specs.js @@ -139,4 +139,38 @@ define([ }); + describe("when creating annotations from influxdb response", function() { + describe('given two series', function() { + var series = new InfluxSeries({ + seriesList: [ + { + columns: ['time', 'text', 'sequence_number'], + name: 'events1', + points: [[1402596000, 'some text', 1], [1402596001, 'asd', 2]] + }, + { + columns: ['time', 'tags', 'text'], + name: 'events2', + points: [[1402596000, 'tag1, tag2', 'mu']] + } + ], + annotation: {query: 'select'} + }); + + var result = series.getAnnotations(); + + it(' should generate 4 annnotations ', function() { + expect(result.length).to.be(3); + expect(result[0].annotation.query).to.be('select'); + expect(result[0].title).to.be('some text'); + expect(result[0].time).to.be(1402596000000); + expect(result[1].title).to.be('asd'); + //expect(result[2].tags).to.be('tag1, tag2'); + //expect(result[2].title).to.be('mu'); + }); + + }); + + }); + });