From f90fda8e6fd0c2cd0d773c5a3a12ef46b7a67d4a Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Sun, 7 Feb 2016 21:13:00 -0800 Subject: [PATCH 01/14] Tracking opentsdb version in opentsdb config --- .../datasource/opentsdb/config_ctrl.ts | 21 +++++++++++++++++++ .../plugins/datasource/opentsdb/datasource.js | 5 +++++ .../app/plugins/datasource/opentsdb/module.ts | 5 +---- .../datasource/opentsdb/partials/config.html | 13 ++++++++++++ .../opentsdb/specs/datasource-specs.ts | 2 +- 5 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 public/app/plugins/datasource/opentsdb/config_ctrl.ts diff --git a/public/app/plugins/datasource/opentsdb/config_ctrl.ts b/public/app/plugins/datasource/opentsdb/config_ctrl.ts new file mode 100644 index 00000000000..bbf7ced7ef1 --- /dev/null +++ b/public/app/plugins/datasource/opentsdb/config_ctrl.ts @@ -0,0 +1,21 @@ +/// + +import angular from 'angular'; +import _ from 'lodash'; + +export class OpenTsConfigCtrl { + static templateUrl = 'public/app/plugins/datasource/opentsdb/partials/config.html'; + current: any; + + /** @ngInject */ + constructor($scope) { + this.current.jsonData = this.current.jsonData || {}; + this.current.jsonData.tsdbVersion = this.current.jsonData.tsdbVersion || 1; + } + + tsdbVersions = [ + {name: '<=2.1', value: 1}, + {name: '>=2.2', value: 2}, + ]; + +} diff --git a/public/app/plugins/datasource/opentsdb/datasource.js b/public/app/plugins/datasource/opentsdb/datasource.js index c80d58a2ac7..a652f429a48 100644 --- a/public/app/plugins/datasource/opentsdb/datasource.js +++ b/public/app/plugins/datasource/opentsdb/datasource.js @@ -14,6 +14,11 @@ function (angular, _, dateMath) { this.name = instanceSettings.name; this.withCredentials = instanceSettings.withCredentials; this.basicAuth = instanceSettings.basicAuth; + this.tsdbVersions = [ + {name: '<=2.1', value: 1}, + {name: '>=2.2', value: 2}, + ]; + this.tsdbVersion = instanceSettings.jsonData.tsdbVersion; this.supportMetrics = true; this.tagKeys = {}; diff --git a/public/app/plugins/datasource/opentsdb/module.ts b/public/app/plugins/datasource/opentsdb/module.ts index 429cda5e5ea..e18552ac64c 100644 --- a/public/app/plugins/datasource/opentsdb/module.ts +++ b/public/app/plugins/datasource/opentsdb/module.ts @@ -1,9 +1,6 @@ import {OpenTsDatasource} from './datasource'; import {OpenTsQueryCtrl} from './query_ctrl'; - -class OpenTsConfigCtrl { - static templateUrl = 'partials/config.html'; -} +import {OpenTsConfigCtrl} from './config_ctrl'; export { OpenTsDatasource as Datasource, diff --git a/public/app/plugins/datasource/opentsdb/partials/config.html b/public/app/plugins/datasource/opentsdb/partials/config.html index 3b7f169a0a8..d212b31686d 100644 --- a/public/app/plugins/datasource/opentsdb/partials/config.html +++ b/public/app/plugins/datasource/opentsdb/partials/config.html @@ -1,2 +1,15 @@ +
+
Opentsdb settings
+
+
    +
  • + Version +
  • +
  • + +
  • +
+
+
diff --git a/public/app/plugins/datasource/opentsdb/specs/datasource-specs.ts b/public/app/plugins/datasource/opentsdb/specs/datasource-specs.ts index b786a93f14c..1da4268e433 100644 --- a/public/app/plugins/datasource/opentsdb/specs/datasource-specs.ts +++ b/public/app/plugins/datasource/opentsdb/specs/datasource-specs.ts @@ -4,7 +4,7 @@ import {OpenTsDatasource} from "../datasource"; describe('opentsdb', function() { var ctx = new helpers.ServiceTestContext(); - var instanceSettings = {url: '' }; + var instanceSettings = {url: '', jsonData: { tsdbVersion: 1 }}; beforeEach(angularMocks.module('grafana.core')); beforeEach(angularMocks.module('grafana.services')); From 908e8577bbce4b75a0f04f6953f346165aa0d670 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Sun, 7 Feb 2016 21:14:43 -0800 Subject: [PATCH 02/14] Removed unused code from datasource.js --- public/app/plugins/datasource/opentsdb/datasource.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/public/app/plugins/datasource/opentsdb/datasource.js b/public/app/plugins/datasource/opentsdb/datasource.js index a652f429a48..2cf0ecf2571 100644 --- a/public/app/plugins/datasource/opentsdb/datasource.js +++ b/public/app/plugins/datasource/opentsdb/datasource.js @@ -14,10 +14,6 @@ function (angular, _, dateMath) { this.name = instanceSettings.name; this.withCredentials = instanceSettings.withCredentials; this.basicAuth = instanceSettings.basicAuth; - this.tsdbVersions = [ - {name: '<=2.1', value: 1}, - {name: '>=2.2', value: 2}, - ]; this.tsdbVersion = instanceSettings.jsonData.tsdbVersion; this.supportMetrics = true; this.tagKeys = {}; From 26232406359e3f5d4bddfbfe26d0e9a36e3e8111 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Sun, 7 Feb 2016 22:03:01 -0800 Subject: [PATCH 03/14] Fill Policy visible only in <=2.2 --- .../plugins/datasource/opentsdb/partials/query.editor.html | 5 ++--- public/app/plugins/datasource/opentsdb/query_ctrl.ts | 3 +++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/opentsdb/partials/query.editor.html b/public/app/plugins/datasource/opentsdb/partials/query.editor.html index 0c7c7d25a57..d896381274e 100644 --- a/public/app/plugins/datasource/opentsdb/partials/query.editor.html +++ b/public/app/plugins/datasource/opentsdb/partials/query.editor.html @@ -63,12 +63,11 @@ -
  • +
  • Fill - Available since OpenTSDB 2.2
  • -
  • +
  • + + + + + add filter + + + + +
  • + +
    + +
    diff --git a/public/app/plugins/datasource/opentsdb/query_ctrl.ts b/public/app/plugins/datasource/opentsdb/query_ctrl.ts index 0ab2cd911aa..57afd75ab30 100644 --- a/public/app/plugins/datasource/opentsdb/query_ctrl.ts +++ b/public/app/plugins/datasource/opentsdb/query_ctrl.ts @@ -76,6 +76,11 @@ export class OpenTsQueryCtrl extends QueryCtrl { } addTag() { + + if (this.target.filters && this.target.filters.length > 0) { + this.errors.tags = "Please remove filters to use tags, tags and filters are mutually exclusive."; + } + if (!this.addTagMode) { this.addTagMode = true; return; @@ -109,7 +114,17 @@ export class OpenTsQueryCtrl extends QueryCtrl { this.addTag(); } + closeAddTagMode() { + this.addTagMode = false; + return; + } + addFilter() { + + if (this.target.tags && _.size(this.target.tags) > 0) { + this.errors.filters = "Please remove tags to use filters, tags and filters are mutually exclusive."; + } + if (!this.addFilterMode) { this.addFilterMode = true; return; @@ -161,6 +176,11 @@ export class OpenTsQueryCtrl extends QueryCtrl { this.addFilter(); } + closeAddFilterMode() { + this.addFilterMode = false; + return; + } + validateTarget() { var errs: any = {}; From 63dfa303e54a688ed115df008fa21104eab55153 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Wed, 10 Feb 2016 21:54:36 -0800 Subject: [PATCH 07/14] Datasource working with filters after fixing bugs --- .../app/plugins/datasource/opentsdb/datasource.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/public/app/plugins/datasource/opentsdb/datasource.js b/public/app/plugins/datasource/opentsdb/datasource.js index 802ea16076b..8e2956c6448 100644 --- a/public/app/plugins/datasource/opentsdb/datasource.js +++ b/public/app/plugins/datasource/opentsdb/datasource.js @@ -89,6 +89,7 @@ function (angular, _, dateMath) { // In case the backend is 3rd-party hosted and does not suport OPTIONS, urlencoded requests // go as POST rather than OPTIONS+POST options.headers = { 'Content-Type': 'application/x-www-form-urlencoded' }; + return backendSrv.datasourceRequest(options); }; @@ -308,15 +309,17 @@ function (angular, _, dateMath) { } } - query.tags = angular.copy(target.tags); - if(query.tags){ - for(var key in query.tags){ - query.tags[key] = templateSrv.replace(query.tags[key], options.scopedVars); + if (target.filters && target.filters.length > 0) { + query.filters = angular.copy(target.filters); + } else { + query.tags = angular.copy(target.tags); + if(query.tags){ + for(var key in query.tags){ + query.tags[key] = templateSrv.replace(query.tags[key], options.scopedVars); + } } } - query.filters = angular.copy(target.filters); - return query; } From 09e80f03902e5cc1dd927f8f07e053e31a6590f3 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Wed, 10 Feb 2016 22:49:27 -0800 Subject: [PATCH 08/14] Labels fixed in legend for Filters --- .../plugins/datasource/opentsdb/datasource.js | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/public/app/plugins/datasource/opentsdb/datasource.js b/public/app/plugins/datasource/opentsdb/datasource.js index 8e2956c6448..eab5c4a992c 100644 --- a/public/app/plugins/datasource/opentsdb/datasource.js +++ b/public/app/plugins/datasource/opentsdb/datasource.js @@ -40,9 +40,15 @@ function (angular, _, dateMath) { var groupByTags = {}; _.each(queries, function(query) { - _.each(query.tags, function(val, key) { - groupByTags[key] = true; - }); + if (query.filters && query.filters.length > 0) { + _.each(query.filters, function(val) { + groupByTags[val.tagk] = true; + }); + } else { + _.each(query.tags, function(val, key) { + groupByTags[key] = true; + }); + } }); return this.performTimeSeriesQuery(queries, start, end).then(function(response) { @@ -327,11 +333,18 @@ function (angular, _, dateMath) { var interpolatedTagValue; return _.map(metrics, function(metricData) { return _.findIndex(options.targets, function(target) { - return target.metric === metricData.metric && + if (target.filters && target.filters.length > 0) { + return target.metric === metricData.metric && + _.all(target.filters, function(filter) { + return filter.tagk === interpolatedTagValue === "*"; + }); + } else { + return target.metric === metricData.metric && _.all(target.tags, function(tagV, tagK) { - interpolatedTagValue = templateSrv.replace(tagV, options.scopedVars); - return metricData.tags[tagK] === interpolatedTagValue || interpolatedTagValue === "*"; - }); + interpolatedTagValue = templateSrv.replace(tagV, options.scopedVars); + return metricData.tags[tagK] === interpolatedTagValue || interpolatedTagValue === "*"; + }); + } }); }); } From 936dd2eaaa641e67876f93fcf82ee192e560ae06 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Wed, 10 Feb 2016 23:01:37 -0800 Subject: [PATCH 09/14] Added relevant docs and changes --- CHANGELOG.md | 1 + docs/sources/datasources/opentsdb.md | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c7763166f20..c8f4fbef587 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * **Prometheus**: Prometheus annotation support, closes[#2883](https://github.com/grafana/grafana/pull/2883) * **Cli**: New cli tool for downloading and updating plugins * **Annotations**: Annotations can now contain links that can be clicked (you can navigate on to annotation popovers), closes [#1588](https://github.com/grafana/grafana/issues/1588) +* **Opentsdb**: Opentsdb 2.2 filters support, closes[#3077](https://github.com/grafana/grafana/issues/3077) ### Breaking changes * **Plugin API**: Both datasource and panel plugin api (and plugin.json schema) have been updated, requiring an update to plugins. See [plugin api](https://github.com/grafana/grafana/blob/master/public/app/plugins/plugin_api.md) for more info. diff --git a/docs/sources/datasources/opentsdb.md b/docs/sources/datasources/opentsdb.md index 43fcda643ee..757ddcefab5 100644 --- a/docs/sources/datasources/opentsdb.md +++ b/docs/sources/datasources/opentsdb.md @@ -23,6 +23,7 @@ Name | The data source name, important that this is the same as in Grafana v1.x Default | Default data source means that it will be pre-selected for new panels. Url | The http protocol, ip and port of you opentsdb server (default port is usually 4242) Access | Proxy = access via Grafana backend, Direct = access directory from browser. +Version | Version = opentsdb version, either <=2.1 or 2.2 ## Query editor Open a graph in edit mode by click the title. From 4aa5dab62d56d26a32afbca9fb8c9325785d3575 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Wed, 17 Feb 2016 07:11:53 -0800 Subject: [PATCH 10/14] Added query ctrl tests for Opentsdb --- .../opentsdb/specs/query-ctrl-specs.ts | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 public/app/plugins/datasource/opentsdb/specs/query-ctrl-specs.ts diff --git a/public/app/plugins/datasource/opentsdb/specs/query-ctrl-specs.ts b/public/app/plugins/datasource/opentsdb/specs/query-ctrl-specs.ts new file mode 100644 index 00000000000..d59bf0c64c7 --- /dev/null +++ b/public/app/plugins/datasource/opentsdb/specs/query-ctrl-specs.ts @@ -0,0 +1,85 @@ +import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common'; +import helpers from 'test/specs/helpers'; +import {OpenTsQueryCtrl} from "../query_ctrl"; + +describe('OpenTsQueryCtrl', function() { + var ctx = new helpers.ControllerTestContext(); + + beforeEach(angularMocks.module('grafana.core')); + beforeEach(angularMocks.module('grafana.services')); + beforeEach(ctx.providePhase(['backendSrv','templateSrv'])); + + beforeEach(ctx.providePhase()); + beforeEach(angularMocks.inject(($rootScope, $controller, $q) => { + ctx.$q = $q; + ctx.scope = $rootScope.$new(); + ctx.target = {target: ''}; + ctx.panelCtrl = {panel: {}}; + ctx.panelCtrl.refresh = sinon.spy(); + ctx.datasource.getAggregators = sinon.stub().returns(ctx.$q.when([])); + + ctx.ctrl = $controller(OpenTsQueryCtrl, {$scope: ctx.scope}, { + panelCtrl: ctx.panelCtrl, + datasource: ctx.datasource, + target: ctx.target, + }); + ctx.scope.$digest(); + })); + + describe('init query_ctrl variables', function() { + + it('filter types should be initialized', function() { + expect(ctx.ctrl.filterTypes.length).to.be(7); + }); + + it('aggregators should be initialized', function() { + expect(ctx.ctrl.aggregators.length).to.be(8); + }); + + it('fill policy options should be initialized', function() { + expect(ctx.ctrl.fillPolicies.length).to.be(4); + }); + + }); + + describe('when adding filters and tags', function() { + + it('addTagMode should be false when closed', function() { + ctx.ctrl.addTagMode = true; + ctx.ctrl.closeAddTagMode(); + expect(ctx.ctrl.addTagMode).to.be(false); + }); + + it('addFilterMode should be false when closed', function() { + ctx.ctrl.addFilterMode = true; + ctx.ctrl.closeAddFilterMode(); + expect(ctx.ctrl.addFilterMode).to.be(false); + }); + + it('removing a tag from the tags list', function() { + ctx.ctrl.target.tags = {"tagk": "tag_key", "tagk2": "tag_value2"}; + ctx.ctrl.removeTag("tagk"); + expect(Object.keys(ctx.ctrl.target.tags).length).to.be(1); + }); + + it('removing a filter from the filters list', function() { + ctx.ctrl.target.filters = [{"tagk": "tag_key", "filter": "tag_value2", "type": "wildcard", "groupBy": true}]; + ctx.ctrl.removeFilter(0); + expect(ctx.ctrl.target.filters.length).to.be(0); + }); + + it('adding a filter when tags exist should generate error', function() { + ctx.ctrl.target.tags = {"tagk": "tag_key", "tagk2": "tag_value2"}; + ctx.ctrl.addFilter(); + expect(ctx.ctrl.errors.filters).to.be('Please remove tags to use filters, tags and filters are mutually exclusive.'); + }); + + it('adding a tag when filters exist should generate error', function() { + ctx.ctrl.target.filters = [{"tagk": "tag_key", "filter": "tag_value2", "type": "wildcard", "groupBy": true}]; + ctx.ctrl.addTag(); + expect(ctx.ctrl.errors.tags).to.be('Please remove filters to use tags, tags and filters are mutually exclusive.'); + }); + + }); + +}); From 8925329950d3072edf9e0270de70ec7537f33e1e Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Fri, 26 Feb 2016 10:15:41 -0800 Subject: [PATCH 11/14] Added default opentsdb version to datasource --- public/app/plugins/datasource/opentsdb/datasource.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/app/plugins/datasource/opentsdb/datasource.js b/public/app/plugins/datasource/opentsdb/datasource.js index eab5c4a992c..75a0560f5a8 100644 --- a/public/app/plugins/datasource/opentsdb/datasource.js +++ b/public/app/plugins/datasource/opentsdb/datasource.js @@ -14,7 +14,7 @@ function (angular, _, dateMath) { this.name = instanceSettings.name; this.withCredentials = instanceSettings.withCredentials; this.basicAuth = instanceSettings.basicAuth; - this.tsdbVersion = instanceSettings.jsonData.tsdbVersion; + this.tsdbVersion = instanceSettings.jsonData.tsdbVersion || 1; this.supportMetrics = true; this.tagKeys = {}; From 7fa170cee9704ff915f710962738091352e1ad4e Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Fri, 26 Feb 2016 10:27:00 -0800 Subject: [PATCH 12/14] Fixed the UI as per new UX convention --- .../datasource/opentsdb/partials/config.html | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/public/app/plugins/datasource/opentsdb/partials/config.html b/public/app/plugins/datasource/opentsdb/partials/config.html index d212b31686d..f4f0bedfc19 100644 --- a/public/app/plugins/datasource/opentsdb/partials/config.html +++ b/public/app/plugins/datasource/opentsdb/partials/config.html @@ -2,14 +2,12 @@
    Opentsdb settings
    -
    -
      -
    • - Version -
    • -
    • - -
    • -
    +
    + + Version + + + +
    From a883424d25ef9b9316830704287c0f4d28dcf161 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Fri, 26 Feb 2016 19:01:06 -0800 Subject: [PATCH 13/14] smooth upgrade from Grafana 2.6 to 3.0 --- public/app/plugins/datasource/opentsdb/datasource.js | 1 + 1 file changed, 1 insertion(+) diff --git a/public/app/plugins/datasource/opentsdb/datasource.js b/public/app/plugins/datasource/opentsdb/datasource.js index 75a0560f5a8..83e7168399f 100644 --- a/public/app/plugins/datasource/opentsdb/datasource.js +++ b/public/app/plugins/datasource/opentsdb/datasource.js @@ -14,6 +14,7 @@ function (angular, _, dateMath) { this.name = instanceSettings.name; this.withCredentials = instanceSettings.withCredentials; this.basicAuth = instanceSettings.basicAuth; + instanceSettings.jsonData = instanceSettings.jsonData || {}; this.tsdbVersion = instanceSettings.jsonData.tsdbVersion || 1; this.supportMetrics = true; this.tagKeys = {}; From 18c57ea230003f8617b6b7904d605d3fd9f51e26 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Sun, 28 Feb 2016 01:35:21 -0800 Subject: [PATCH 14/14] Made opentsdb query_ctrl robust --- .../app/plugins/datasource/opentsdb/datasource.js | 15 ++++++++++++++- .../app/plugins/datasource/opentsdb/query_ctrl.ts | 12 ++++++++++-- .../datasource/opentsdb/specs/query-ctrl-specs.ts | 1 + 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/opentsdb/datasource.js b/public/app/plugins/datasource/opentsdb/datasource.js index 83e7168399f..14605151752 100644 --- a/public/app/plugins/datasource/opentsdb/datasource.js +++ b/public/app/plugins/datasource/opentsdb/datasource.js @@ -224,7 +224,7 @@ function (angular, _, dateMath) { this.getAggregators = function() { if (aggregatorsPromise) { return aggregatorsPromise; } - aggregatorsPromise = this._get('/api/aggregators').then(function(result) { + aggregatorsPromise = this._get('/api/aggregators').then(function(result) { if (result.data && _.isArray(result.data)) { return result.data.sort(); } @@ -233,6 +233,19 @@ function (angular, _, dateMath) { return aggregatorsPromise; }; + var filterTypesPromise = null; + this.getFilterTypes = function() { + if (filterTypesPromise) { return filterTypesPromise; } + + filterTypesPromise = this._get('/api/config/filters').then(function(result) { + if (result.data) { + return Object.keys(result.data).sort(); + } + return []; + }); + return filterTypesPromise; + }; + function transformMetricData(md, groupByTags, target, options) { var metricLabel = createMetricLabel(md, target, groupByTags, options); var dps = []; diff --git a/public/app/plugins/datasource/opentsdb/query_ctrl.ts b/public/app/plugins/datasource/opentsdb/query_ctrl.ts index 57afd75ab30..60466a00ff5 100644 --- a/public/app/plugins/datasource/opentsdb/query_ctrl.ts +++ b/public/app/plugins/datasource/opentsdb/query_ctrl.ts @@ -45,7 +45,15 @@ export class OpenTsQueryCtrl extends QueryCtrl { } this.datasource.getAggregators().then((aggs) => { - this.aggregators = aggs; + if (aggs.length !== 0) { + this.aggregators = aggs; + } + }); + + this.datasource.getFilterTypes().then((filterTypes) => { + if (filterTypes.length !== 0) { + this.filterTypes = filterTypes; + } }); // needs to be defined here as it is called from typeahead @@ -135,7 +143,7 @@ export class OpenTsQueryCtrl extends QueryCtrl { } if (!this.target.currentFilterType) { - this.target.currentFilterType = 'literal_or'; + this.target.currentFilterType = 'iliteral_or'; } if (!this.target.currentFilterGroupBy) { diff --git a/public/app/plugins/datasource/opentsdb/specs/query-ctrl-specs.ts b/public/app/plugins/datasource/opentsdb/specs/query-ctrl-specs.ts index d59bf0c64c7..5924fac7a38 100644 --- a/public/app/plugins/datasource/opentsdb/specs/query-ctrl-specs.ts +++ b/public/app/plugins/datasource/opentsdb/specs/query-ctrl-specs.ts @@ -17,6 +17,7 @@ describe('OpenTsQueryCtrl', function() { ctx.panelCtrl = {panel: {}}; ctx.panelCtrl.refresh = sinon.spy(); ctx.datasource.getAggregators = sinon.stub().returns(ctx.$q.when([])); + ctx.datasource.getFilterTypes = sinon.stub().returns(ctx.$q.when([])); ctx.ctrl = $controller(OpenTsQueryCtrl, {$scope: ctx.scope}, { panelCtrl: ctx.panelCtrl,