2017-12-20 12:33:33 +01:00
|
|
|
import './add_graphite_func';
|
|
|
|
|
import './func_editor';
|
2016-02-02 12:52:43 +01:00
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
import GraphiteQuery from './graphite_query';
|
|
|
|
|
import { QueryCtrl } from 'app/plugins/sdk';
|
|
|
|
|
import appEvents from 'app/core/app_events';
|
2019-07-05 16:46:46 +02:00
|
|
|
import { auto } from 'angular';
|
|
|
|
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
2019-10-14 09:27:47 +01:00
|
|
|
import { AppEvents } from '@grafana/data';
|
2016-02-02 12:52:43 +01:00
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
const GRAPHITE_TAG_OPERATORS = ['=', '!=', '=~', '!=~'];
|
|
|
|
|
const TAG_PREFIX = 'tag: ';
|
2017-10-05 14:39:09 +03:00
|
|
|
|
2016-02-02 12:52:43 +01:00
|
|
|
export class GraphiteQueryCtrl extends QueryCtrl {
|
2017-12-20 12:33:33 +01:00
|
|
|
static templateUrl = 'partials/query.editor.html';
|
2016-02-02 12:52:43 +01:00
|
|
|
|
2017-10-13 12:05:38 +03:00
|
|
|
queryModel: GraphiteQuery;
|
2016-02-02 12:52:43 +01:00
|
|
|
segments: any[];
|
2017-10-05 14:39:09 +03:00
|
|
|
addTagSegments: any[];
|
|
|
|
|
removeTagValue: string;
|
2017-10-20 11:23:14 +03:00
|
|
|
supportsTags: boolean;
|
2017-12-27 12:31:44 -05:00
|
|
|
paused: boolean;
|
2016-02-02 12:52:43 +01:00
|
|
|
|
2018-08-31 16:40:43 +02:00
|
|
|
/** @ngInject */
|
2019-07-05 16:46:46 +02:00
|
|
|
constructor(
|
|
|
|
|
$scope: any,
|
|
|
|
|
$injector: auto.IInjectorService,
|
|
|
|
|
private uiSegmentSrv: any,
|
|
|
|
|
private templateSrv: TemplateSrv,
|
|
|
|
|
$timeout: any
|
|
|
|
|
) {
|
2016-02-02 12:52:43 +01:00
|
|
|
super($scope, $injector);
|
2017-10-20 11:23:14 +03:00
|
|
|
this.supportsTags = this.datasource.supportsTags;
|
2017-12-27 12:31:44 -05:00
|
|
|
this.paused = false;
|
2018-01-12 12:31:27 +01:00
|
|
|
this.target.target = this.target.target || '';
|
2016-02-02 12:52:43 +01:00
|
|
|
|
2018-01-12 12:31:27 +01:00
|
|
|
this.datasource.waitForFuncDefsLoaded().then(() => {
|
2017-12-08 18:48:39 -05:00
|
|
|
this.queryModel = new GraphiteQuery(this.datasource, this.target, templateSrv);
|
2020-01-20 10:18:41 +01:00
|
|
|
this.buildSegments(false);
|
2018-01-12 12:31:27 +01:00
|
|
|
});
|
2017-10-04 19:46:18 +03:00
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
this.removeTagValue = '-- remove tag --';
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parseTarget() {
|
2017-10-13 12:05:38 +03:00
|
|
|
this.queryModel.parseTarget();
|
|
|
|
|
this.buildSegments();
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
2017-10-13 12:05:38 +03:00
|
|
|
toggleEditorMode() {
|
|
|
|
|
this.target.textEditor = !this.target.textEditor;
|
|
|
|
|
this.parseTarget();
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-20 10:18:41 +01:00
|
|
|
buildSegments(modifyLastSegment = true) {
|
2017-10-13 12:05:38 +03:00
|
|
|
this.segments = _.map(this.queryModel.segments, segment => {
|
|
|
|
|
return this.uiSegmentSrv.newSegment(segment);
|
|
|
|
|
});
|
2017-11-16 14:16:37 +01:00
|
|
|
|
2018-08-26 17:14:40 +02:00
|
|
|
const checkOtherSegmentsIndex = this.queryModel.checkOtherSegmentsIndex || 0;
|
2020-01-20 10:18:41 +01:00
|
|
|
this.checkOtherSegments(checkOtherSegmentsIndex, modifyLastSegment);
|
2016-02-02 12:52:43 +01:00
|
|
|
|
2017-10-13 12:05:38 +03:00
|
|
|
if (this.queryModel.seriesByTagUsed) {
|
|
|
|
|
this.fixTagSegments();
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-13 12:05:38 +03:00
|
|
|
addSelectMetricSegment() {
|
|
|
|
|
this.queryModel.addSelectMetricSegment();
|
|
|
|
|
this.segments.push(this.uiSegmentSrv.newSelectMetric());
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-20 10:18:41 +01:00
|
|
|
checkOtherSegments(fromIndex: number, modifyLastSegment = true) {
|
2017-12-21 08:39:31 +01:00
|
|
|
if (this.queryModel.segments.length === 1 && this.queryModel.segments[0].type === 'series-ref') {
|
2017-12-09 16:27:05 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-02 12:52:43 +01:00
|
|
|
if (fromIndex === 0) {
|
2017-10-13 12:05:38 +03:00
|
|
|
this.addSelectMetricSegment();
|
2016-02-02 12:52:43 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 14:26:50 +02:00
|
|
|
const path = this.queryModel.getSegmentPathUpTo(fromIndex + 1);
|
2017-12-20 12:33:33 +01:00
|
|
|
if (path === '') {
|
2017-03-09 07:31:00 +01:00
|
|
|
return Promise.resolve();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-19 16:06:54 +01:00
|
|
|
return this.datasource
|
|
|
|
|
.metricFindQuery(path)
|
2019-07-05 16:46:46 +02:00
|
|
|
.then((segments: any) => {
|
2017-12-19 16:06:54 +01:00
|
|
|
if (segments.length === 0) {
|
2020-01-20 10:18:41 +01:00
|
|
|
if (path !== '' && modifyLastSegment) {
|
2017-12-21 08:39:31 +01:00
|
|
|
this.queryModel.segments = this.queryModel.segments.splice(0, fromIndex);
|
2017-12-19 16:06:54 +01:00
|
|
|
this.segments = this.segments.splice(0, fromIndex);
|
|
|
|
|
this.addSelectMetricSegment();
|
|
|
|
|
}
|
|
|
|
|
} else if (segments[0].expandable) {
|
|
|
|
|
if (this.segments.length === fromIndex) {
|
|
|
|
|
this.addSelectMetricSegment();
|
|
|
|
|
} else {
|
|
|
|
|
return this.checkOtherSegments(fromIndex + 1);
|
|
|
|
|
}
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
2017-12-19 16:06:54 +01:00
|
|
|
})
|
2019-07-05 16:46:46 +02:00
|
|
|
.catch((err: any) => {
|
2019-10-14 09:27:47 +01:00
|
|
|
appEvents.emit(AppEvents.alertError, ['Error', err]);
|
2017-12-19 16:06:54 +01:00
|
|
|
});
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
setSegmentFocus(segmentIndex: any) {
|
2016-02-02 12:52:43 +01:00
|
|
|
_.each(this.segments, (segment, index) => {
|
|
|
|
|
segment.focus = segmentIndex === index;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
getAltSegments(index: number, prefix: string) {
|
2018-08-30 09:03:11 +02:00
|
|
|
let query = prefix && prefix.length > 0 ? '*' + prefix + '*' : '*';
|
2017-12-27 13:53:12 -05:00
|
|
|
if (index > 0) {
|
|
|
|
|
query = this.queryModel.getSegmentPathUpTo(index) + '.' + query;
|
|
|
|
|
}
|
2018-08-29 14:26:50 +02:00
|
|
|
const options = {
|
2017-12-19 16:06:54 +01:00
|
|
|
range: this.panelCtrl.range,
|
2017-12-20 12:33:33 +01:00
|
|
|
requestId: 'get-alt-segments',
|
2017-12-19 16:06:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return this.datasource
|
|
|
|
|
.metricFindQuery(query, options)
|
2019-07-05 16:46:46 +02:00
|
|
|
.then((segments: any[]) => {
|
2018-08-29 14:26:50 +02:00
|
|
|
const altSegments = _.map(segments, segment => {
|
2017-12-19 16:06:54 +01:00
|
|
|
return this.uiSegmentSrv.newSegment({
|
|
|
|
|
value: segment.text,
|
2017-12-20 12:33:33 +01:00
|
|
|
expandable: segment.expandable,
|
2017-12-19 16:06:54 +01:00
|
|
|
});
|
|
|
|
|
});
|
2016-02-02 12:52:43 +01:00
|
|
|
|
2017-12-27 13:48:55 -05:00
|
|
|
if (index > 0 && altSegments.length === 0) {
|
2017-12-19 16:06:54 +01:00
|
|
|
return altSegments;
|
|
|
|
|
}
|
2016-02-02 12:52:43 +01:00
|
|
|
|
2017-12-19 16:06:54 +01:00
|
|
|
// add query references
|
|
|
|
|
if (index === 0) {
|
|
|
|
|
_.eachRight(this.panelCtrl.panel.targets, target => {
|
|
|
|
|
if (target.refId === this.queryModel.target.refId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
altSegments.unshift(
|
|
|
|
|
this.uiSegmentSrv.newSegment({
|
2017-12-20 12:33:33 +01:00
|
|
|
type: 'series-ref',
|
|
|
|
|
value: '#' + target.refId,
|
|
|
|
|
expandable: false,
|
2017-12-19 16:06:54 +01:00
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-12-09 16:27:05 -05:00
|
|
|
|
2017-12-19 16:06:54 +01:00
|
|
|
// add template variables
|
|
|
|
|
_.eachRight(this.templateSrv.variables, variable => {
|
|
|
|
|
altSegments.unshift(
|
|
|
|
|
this.uiSegmentSrv.newSegment({
|
2017-12-20 12:33:33 +01:00
|
|
|
type: 'template',
|
|
|
|
|
value: '$' + variable.name,
|
|
|
|
|
expandable: true,
|
2017-12-19 16:06:54 +01:00
|
|
|
})
|
|
|
|
|
);
|
2017-12-09 16:27:05 -05:00
|
|
|
});
|
2016-02-02 12:52:43 +01:00
|
|
|
|
2017-12-19 16:06:54 +01:00
|
|
|
// add wildcard option
|
2017-12-20 12:33:33 +01:00
|
|
|
altSegments.unshift(this.uiSegmentSrv.newSegment('*'));
|
2017-10-20 11:23:14 +03:00
|
|
|
|
2017-12-19 16:06:54 +01:00
|
|
|
if (this.supportsTags && index === 0) {
|
|
|
|
|
this.removeTaggedEntry(altSegments);
|
2017-12-27 13:48:55 -05:00
|
|
|
return this.addAltTagSegments(prefix, altSegments);
|
2017-12-19 16:06:54 +01:00
|
|
|
} else {
|
|
|
|
|
return altSegments;
|
|
|
|
|
}
|
|
|
|
|
})
|
2019-11-19 13:59:39 +00:00
|
|
|
.catch((err: any): any[] => {
|
|
|
|
|
return [];
|
|
|
|
|
});
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
addAltTagSegments(prefix: string, altSegments: any[]) {
|
|
|
|
|
return this.getTagsAsSegments(prefix).then((tagSegments: any[]) => {
|
2017-12-19 16:06:54 +01:00
|
|
|
tagSegments = _.map(tagSegments, segment => {
|
2017-10-20 11:23:14 +03:00
|
|
|
segment.value = TAG_PREFIX + segment.value;
|
|
|
|
|
return segment;
|
|
|
|
|
});
|
|
|
|
|
return altSegments.concat(...tagSegments);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-15 12:11:52 +02:00
|
|
|
removeTaggedEntry(altSegments: any[]) {
|
2017-12-20 12:33:33 +01:00
|
|
|
altSegments = _.remove(altSegments, s => s.value === '_tagged');
|
2017-10-20 11:23:14 +03:00
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
segmentValueChanged(segment: { type: string; value: string; expandable: any }, segmentIndex: number) {
|
2016-02-02 22:58:37 +01:00
|
|
|
this.error = null;
|
2017-10-13 12:05:38 +03:00
|
|
|
this.queryModel.updateSegmentValue(segment, segmentIndex);
|
2016-02-02 12:52:43 +01:00
|
|
|
|
2017-12-21 08:39:31 +01:00
|
|
|
if (this.queryModel.functions.length > 0 && this.queryModel.functions[0].def.fake) {
|
2017-10-13 12:05:38 +03:00
|
|
|
this.queryModel.functions = [];
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
if (segment.type === 'tag') {
|
2018-08-26 17:14:40 +02:00
|
|
|
const tag = removeTagPrefix(segment.value);
|
2017-12-27 12:31:44 -05:00
|
|
|
this.pause();
|
2017-10-20 11:23:14 +03:00
|
|
|
this.addSeriesByTagFunc(tag);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-02 12:52:43 +01:00
|
|
|
if (segment.expandable) {
|
|
|
|
|
return this.checkOtherSegments(segmentIndex + 1).then(() => {
|
|
|
|
|
this.setSegmentFocus(segmentIndex + 1);
|
|
|
|
|
this.targetChanged();
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2017-10-13 12:05:38 +03:00
|
|
|
this.spliceSegments(segmentIndex + 1);
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setSegmentFocus(segmentIndex + 1);
|
|
|
|
|
this.targetChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
spliceSegments(index: any) {
|
2017-10-13 12:05:38 +03:00
|
|
|
this.segments = this.segments.splice(0, index);
|
|
|
|
|
this.queryModel.segments = this.queryModel.segments.splice(0, index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emptySegments() {
|
|
|
|
|
this.queryModel.segments = [];
|
|
|
|
|
this.segments = [];
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-02 12:52:43 +01:00
|
|
|
targetTextChanged() {
|
2017-04-14 17:41:22 -04:00
|
|
|
this.updateModelTarget();
|
|
|
|
|
this.refresh();
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
2016-10-11 12:49:51 +02:00
|
|
|
updateModelTarget() {
|
2017-10-13 12:05:38 +03:00
|
|
|
this.queryModel.updateModelTarget(this.panelCtrl.panel.targets);
|
2016-10-11 12:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-02 12:52:43 +01:00
|
|
|
targetChanged() {
|
2017-10-13 12:05:38 +03:00
|
|
|
if (this.queryModel.error) {
|
2016-02-02 12:52:43 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 14:26:50 +02:00
|
|
|
const oldTarget = this.queryModel.target.target;
|
2016-10-11 12:49:51 +02:00
|
|
|
this.updateModelTarget();
|
2016-02-02 12:52:43 +01:00
|
|
|
|
2017-12-27 12:31:44 -05:00
|
|
|
if (this.queryModel.target !== oldTarget && !this.paused) {
|
2017-12-10 10:46:31 -05:00
|
|
|
this.panelCtrl.refresh();
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
addFunction(funcDef: any) {
|
2018-08-29 14:26:50 +02:00
|
|
|
const newFunc = this.datasource.createFuncInstance(funcDef, {
|
2017-12-20 12:33:33 +01:00
|
|
|
withDefaultParams: true,
|
2017-12-19 16:06:54 +01:00
|
|
|
});
|
2016-02-02 12:52:43 +01:00
|
|
|
newFunc.added = true;
|
2017-10-13 12:05:38 +03:00
|
|
|
this.queryModel.addFunction(newFunc);
|
2016-02-02 12:52:43 +01:00
|
|
|
this.smartlyHandleNewAliasByNode(newFunc);
|
|
|
|
|
|
|
|
|
|
if (this.segments.length === 1 && this.segments[0].fake) {
|
2017-10-13 12:05:38 +03:00
|
|
|
this.emptySegments();
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!newFunc.params.length && newFunc.added) {
|
|
|
|
|
this.targetChanged();
|
|
|
|
|
}
|
2017-10-04 19:46:18 +03:00
|
|
|
|
2017-12-20 12:33:33 +01:00
|
|
|
if (newFunc.def.name === 'seriesByTag') {
|
2017-10-04 19:46:18 +03:00
|
|
|
this.parseTarget();
|
|
|
|
|
}
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
removeFunction(func: any) {
|
2017-10-13 12:05:38 +03:00
|
|
|
this.queryModel.removeFunction(func);
|
|
|
|
|
this.targetChanged();
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
moveFunction(func: any, offset: any) {
|
2019-02-18 17:55:38 +01:00
|
|
|
this.queryModel.moveFunction(func, offset);
|
|
|
|
|
this.targetChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
addSeriesByTagFunc(tag: string) {
|
2018-08-26 17:14:40 +02:00
|
|
|
const newFunc = this.datasource.createFuncInstance('seriesByTag', {
|
2017-12-20 12:33:33 +01:00
|
|
|
withDefaultParams: false,
|
2017-12-19 16:06:54 +01:00
|
|
|
});
|
2018-08-26 17:14:40 +02:00
|
|
|
const tagParam = `${tag}=`;
|
2017-10-20 11:23:14 +03:00
|
|
|
newFunc.params = [tagParam];
|
|
|
|
|
this.queryModel.addFunction(newFunc);
|
|
|
|
|
newFunc.added = true;
|
|
|
|
|
|
|
|
|
|
this.emptySegments();
|
|
|
|
|
this.targetChanged();
|
|
|
|
|
this.parseTarget();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
smartlyHandleNewAliasByNode(func: { def: { name: string }; params: number[]; added: boolean }) {
|
2017-12-20 12:33:33 +01:00
|
|
|
if (func.def.name !== 'aliasByNode') {
|
2016-02-02 12:52:43 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 09:03:11 +02:00
|
|
|
for (let i = 0; i < this.segments.length; i++) {
|
2017-12-20 12:33:33 +01:00
|
|
|
if (this.segments[i].value.indexOf('*') >= 0) {
|
2016-02-02 12:52:43 +01:00
|
|
|
func.params[0] = i;
|
|
|
|
|
func.added = false;
|
|
|
|
|
this.targetChanged();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-04 19:46:18 +03:00
|
|
|
|
2017-10-27 13:15:54 +03:00
|
|
|
getAllTags() {
|
2019-07-05 16:46:46 +02:00
|
|
|
return this.datasource.getTags().then((values: any[]) => {
|
2018-08-26 17:14:40 +02:00
|
|
|
const altTags = _.map(values, 'text');
|
2017-10-05 14:39:09 +03:00
|
|
|
altTags.splice(0, 0, this.removeTagValue);
|
|
|
|
|
return mapToDropdownOptions(altTags);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
getTags(index: number, tagPrefix: any) {
|
2018-08-26 17:14:40 +02:00
|
|
|
const tagExpressions = this.queryModel.renderTagExpressions(index);
|
2019-07-05 16:46:46 +02:00
|
|
|
return this.datasource.getTagsAutoComplete(tagExpressions, tagPrefix).then((values: any) => {
|
2018-08-26 17:14:40 +02:00
|
|
|
const altTags = _.map(values, 'text');
|
2017-12-21 08:39:31 +01:00
|
|
|
altTags.splice(0, 0, this.removeTagValue);
|
|
|
|
|
return mapToDropdownOptions(altTags);
|
|
|
|
|
});
|
2017-10-27 13:15:54 +03:00
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
getTagsAsSegments(tagPrefix: string) {
|
2018-08-26 17:14:40 +02:00
|
|
|
const tagExpressions = this.queryModel.renderTagExpressions();
|
2019-07-05 16:46:46 +02:00
|
|
|
return this.datasource.getTagsAutoComplete(tagExpressions, tagPrefix).then((values: any) => {
|
2017-12-19 16:06:54 +01:00
|
|
|
return _.map(values, val => {
|
|
|
|
|
return this.uiSegmentSrv.newSegment({
|
|
|
|
|
value: val.text,
|
2017-12-20 12:33:33 +01:00
|
|
|
type: 'tag',
|
|
|
|
|
expandable: false,
|
2017-12-19 16:06:54 +01:00
|
|
|
});
|
2017-10-05 14:39:09 +03:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getTagOperators() {
|
|
|
|
|
return mapToDropdownOptions(GRAPHITE_TAG_OPERATORS);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
getAllTagValues(tag: { key: any }) {
|
2018-08-26 17:14:40 +02:00
|
|
|
const tagKey = tag.key;
|
2019-07-05 16:46:46 +02:00
|
|
|
return this.datasource.getTagValues(tagKey).then((values: any[]) => {
|
2018-08-26 17:14:40 +02:00
|
|
|
const altValues = _.map(values, 'text');
|
2017-10-27 13:15:54 +03:00
|
|
|
return mapToDropdownOptions(altValues);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
getTagValues(tag: { key: any }, index: number, valuePrefix: any) {
|
2018-08-26 17:14:40 +02:00
|
|
|
const tagExpressions = this.queryModel.renderTagExpressions(index);
|
|
|
|
|
const tagKey = tag.key;
|
2019-07-05 16:46:46 +02:00
|
|
|
return this.datasource.getTagValuesAutoComplete(tagExpressions, tagKey, valuePrefix).then((values: any[]) => {
|
2018-08-26 17:14:40 +02:00
|
|
|
const altValues = _.map(values, 'text');
|
2018-02-15 11:49:26 -06:00
|
|
|
// Add template variables as additional values
|
|
|
|
|
_.eachRight(this.templateSrv.variables, variable => {
|
|
|
|
|
altValues.push('${' + variable.name + ':regex}');
|
|
|
|
|
});
|
2017-12-21 08:39:31 +01:00
|
|
|
return mapToDropdownOptions(altValues);
|
|
|
|
|
});
|
2017-10-05 14:39:09 +03:00
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
tagChanged(tag: any, tagIndex: any) {
|
2017-10-13 12:05:38 +03:00
|
|
|
this.queryModel.updateTag(tag, tagIndex);
|
2017-10-05 14:39:09 +03:00
|
|
|
this.targetChanged();
|
2017-10-04 19:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
addNewTag(segment: { value: any }) {
|
2018-08-26 17:14:40 +02:00
|
|
|
const newTagKey = segment.value;
|
|
|
|
|
const newTag = { key: newTagKey, operator: '=', value: '' };
|
2017-10-13 12:05:38 +03:00
|
|
|
this.queryModel.addTag(newTag);
|
2017-10-05 14:39:09 +03:00
|
|
|
this.targetChanged();
|
2017-10-05 16:30:20 +03:00
|
|
|
this.fixTagSegments();
|
2017-10-05 14:39:09 +03:00
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
removeTag(index: any) {
|
2017-10-13 12:05:38 +03:00
|
|
|
this.queryModel.removeTag(index);
|
2017-10-05 14:39:09 +03:00
|
|
|
this.targetChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fixTagSegments() {
|
|
|
|
|
// Adding tag with the same name as just removed works incorrectly if single segment is used (instead of array)
|
|
|
|
|
this.addTagSegments = [this.uiSegmentSrv.newPlusButton()];
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
showDelimiter(index: number) {
|
2017-10-13 12:05:38 +03:00
|
|
|
return index !== this.queryModel.tags.length - 1;
|
2017-10-05 14:39:09 +03:00
|
|
|
}
|
2017-12-27 12:31:44 -05:00
|
|
|
|
|
|
|
|
pause() {
|
|
|
|
|
this.paused = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unpause() {
|
|
|
|
|
this.paused = false;
|
|
|
|
|
this.panelCtrl.refresh();
|
|
|
|
|
}
|
2019-01-17 13:15:25 +01:00
|
|
|
|
|
|
|
|
getCollapsedText() {
|
|
|
|
|
return this.target.target;
|
|
|
|
|
}
|
2017-10-04 19:46:18 +03:00
|
|
|
}
|
|
|
|
|
|
2019-07-05 16:46:46 +02:00
|
|
|
function mapToDropdownOptions(results: any[]) {
|
2017-12-19 16:06:54 +01:00
|
|
|
return _.map(results, value => {
|
|
|
|
|
return { text: value, value: value };
|
2017-10-05 14:39:09 +03:00
|
|
|
});
|
2016-02-02 12:52:43 +01:00
|
|
|
}
|
2017-10-20 11:23:14 +03:00
|
|
|
|
|
|
|
|
function removeTagPrefix(value: string): string {
|
2017-12-20 12:33:33 +01:00
|
|
|
return value.replace(TAG_PREFIX, '');
|
2017-10-20 11:23:14 +03:00
|
|
|
}
|