From 968ce15e7f351f50acec2d4df6bf3e9936847d8e Mon Sep 17 00:00:00 2001 From: Alexander Zobnin Date: Fri, 20 Oct 2017 11:23:14 +0300 Subject: [PATCH] graphite: add tags to dropdown and switch to tag editor if selected --- public/app/core/directives/metric_segment.js | 1 + .../plugins/datasource/graphite/datasource.ts | 5 ++ .../plugins/datasource/graphite/query_ctrl.ts | 52 +++++++++++++++++-- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/public/app/core/directives/metric_segment.js b/public/app/core/directives/metric_segment.js index 37352556819..60dc61ef7f2 100644 --- a/public/app/core/directives/metric_segment.js +++ b/public/app/core/directives/metric_segment.js @@ -46,6 +46,7 @@ function (_, $, coreModule) { segment.html = selected.html || selected.value; segment.fake = false; segment.expandable = selected.expandable; + segment.type = selected.type; } else if (segment.custom !== 'false') { segment.value = value; diff --git a/public/app/plugins/datasource/graphite/datasource.ts b/public/app/plugins/datasource/graphite/datasource.ts index f2af15ffe9d..a2ea5e0768f 100644 --- a/public/app/plugins/datasource/graphite/datasource.ts +++ b/public/app/plugins/datasource/graphite/datasource.ts @@ -9,6 +9,7 @@ export function GraphiteDatasource(instanceSettings, $q, backendSrv, templateSrv this.url = instanceSettings.url; this.name = instanceSettings.name; this.graphiteVersion = instanceSettings.jsonData.graphiteVersion || '0.9'; + this.supportsTags = supportsTags(this.graphiteVersion); this.cacheTimeout = instanceSettings.cacheTimeout; this.withCredentials = instanceSettings.withCredentials; this.render_method = instanceSettings.render_method || 'POST'; @@ -357,3 +358,7 @@ export function GraphiteDatasource(instanceSettings, $q, backendSrv, templateSrv return clean_options; }; } + +function supportsTags(version: string): boolean { + return version >= '1.1'; +} diff --git a/public/app/plugins/datasource/graphite/query_ctrl.ts b/public/app/plugins/datasource/graphite/query_ctrl.ts index 9f661e924bd..1dd30fbfc52 100644 --- a/public/app/plugins/datasource/graphite/query_ctrl.ts +++ b/public/app/plugins/datasource/graphite/query_ctrl.ts @@ -8,6 +8,7 @@ import {QueryCtrl} from 'app/plugins/sdk'; import appEvents from 'app/core/app_events'; const GRAPHITE_TAG_OPERATORS = ['=', '!=', '=~', '!=~']; +const TAG_PREFIX = 'tag: '; export class GraphiteQueryCtrl extends QueryCtrl { static templateUrl = 'partials/query.editor.html'; @@ -16,10 +17,12 @@ export class GraphiteQueryCtrl extends QueryCtrl { segments: any[]; addTagSegments: any[]; removeTagValue: string; + supportsTags: boolean; /** @ngInject **/ constructor($scope, $injector, private uiSegmentSrv, private templateSrv) { super($scope, $injector); + this.supportsTags = this.datasource.supportsTags; if (this.target) { this.target.target = this.target.target || ''; @@ -94,7 +97,7 @@ export class GraphiteQueryCtrl extends QueryCtrl { } getAltSegments(index) { - var query = index === 0 ? '*' : this.queryModel.getSegmentPathUpTo(index) + '.*'; + var query = index === 0 ? '*' : this.queryModel.getSegmentPathUpTo(index) + '.*'; var options = {range: this.panelCtrl.range, requestId: "get-alt-segments"}; return this.datasource.metricFindQuery(query, options).then(segments => { @@ -115,12 +118,32 @@ export class GraphiteQueryCtrl extends QueryCtrl { // add wildcard option altSegments.unshift(this.uiSegmentSrv.newSegment('*')); - return altSegments; + + if (this.supportsTags && index === 0) { + this.removeTaggedEntry(altSegments); + return this.addAltTagSegments(index, altSegments); + } else { + return altSegments; + } }).catch(err => { return []; }); } + addAltTagSegments(index, altSegments) { + return this.getTagsAsSegments().then((tagSegments) => { + tagSegments = _.map(tagSegments, (segment) => { + segment.value = TAG_PREFIX + segment.value; + return segment; + }); + return altSegments.concat(...tagSegments); + }); + } + + removeTaggedEntry(altSegments) { + altSegments = _.remove(altSegments, (s) => s.value === '_tagged'); + } + segmentValueChanged(segment, segmentIndex) { this.error = null; this.queryModel.updateSegmentValue(segment, segmentIndex); @@ -129,6 +152,12 @@ export class GraphiteQueryCtrl extends QueryCtrl { this.queryModel.functions = []; } + if (segment.type === 'tag') { + let tag = removeTagPrefix(segment.value); + this.addSeriesByTagFunc(tag); + return; + } + if (segment.expandable) { return this.checkOtherSegments(segmentIndex + 1).then(() => { this.setSegmentFocus(segmentIndex + 1); @@ -201,6 +230,19 @@ export class GraphiteQueryCtrl extends QueryCtrl { this.targetChanged(); } + addSeriesByTagFunc(tag) { + let funcDef = gfunc.getFuncDef('seriesByTag'); + let newFunc = gfunc.createFuncInstance(funcDef, { withDefaultParams: false }); + let tagParam = `${tag}=select tag value`; + newFunc.params = [tagParam]; + this.queryModel.addFunction(newFunc); + newFunc.added = true; + + this.emptySegments(); + this.targetChanged(); + this.parseTarget(); + } + smartlyHandleNewAliasByNode(func) { if (func.def.name !== 'aliasByNode') { return; @@ -227,7 +269,7 @@ export class GraphiteQueryCtrl extends QueryCtrl { getTagsAsSegments() { return this.datasource.getTags().then((values) => { return _.map(values, (val) => { - return this.uiSegmentSrv.newSegment(val.text); + return this.uiSegmentSrv.newSegment({value: val.text, type: 'tag', expandable: false}); }); }); } @@ -277,3 +319,7 @@ function mapToDropdownOptions(results) { return {text: value, value: value}; }); } + +function removeTagPrefix(value: string): string { + return value.replace(TAG_PREFIX, ''); +}