2019-12-05 03:04:03 -06:00
|
|
|
import angular, { auto } from 'angular';
|
2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
|
|
|
import { InfluxQueryBuilder } from './query_builder';
|
2019-06-10 07:39:53 -05:00
|
|
|
import InfluxQueryModel from './influx_query_model';
|
2017-12-20 05:33:33 -06:00
|
|
|
import queryPart from './query_part';
|
|
|
|
import { QueryCtrl } from 'app/plugins/sdk';
|
2020-10-01 12:51:23 -05:00
|
|
|
import { TemplateSrv } from '@grafana/runtime';
|
2020-06-22 15:03:34 -05:00
|
|
|
import { InfluxQuery } from './types';
|
|
|
|
import InfluxDatasource from './datasource';
|
2016-02-02 15:58:37 -06:00
|
|
|
|
|
|
|
export class InfluxQueryCtrl extends QueryCtrl {
|
2017-12-20 05:33:33 -06:00
|
|
|
static templateUrl = 'partials/query.editor.html';
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2020-06-22 15:03:34 -05:00
|
|
|
datasource: InfluxDatasource;
|
2019-06-10 07:39:53 -05:00
|
|
|
queryModel: InfluxQueryModel;
|
2016-02-02 15:58:37 -06:00
|
|
|
queryBuilder: any;
|
|
|
|
groupBySegment: any;
|
|
|
|
resultFormats: any[];
|
2017-03-05 16:13:46 -06:00
|
|
|
orderByTime: any[];
|
2016-02-02 15:58:37 -06:00
|
|
|
policySegment: any;
|
|
|
|
tagSegments: any[];
|
|
|
|
selectMenu: any;
|
|
|
|
measurementSegment: any;
|
|
|
|
removeTagFilterSegment: any;
|
|
|
|
|
2018-08-31 09:40:43 -05:00
|
|
|
/** @ngInject */
|
2019-07-05 00:52:23 -05:00
|
|
|
constructor(
|
|
|
|
$scope: any,
|
|
|
|
$injector: auto.IInjectorService,
|
|
|
|
private templateSrv: TemplateSrv,
|
|
|
|
private uiSegmentSrv: any
|
|
|
|
) {
|
2016-02-02 15:58:37 -06:00
|
|
|
super($scope, $injector);
|
|
|
|
this.target = this.target;
|
2019-06-10 07:39:53 -05:00
|
|
|
this.queryModel = new InfluxQueryModel(this.target, templateSrv, this.panel.scopedVars);
|
2017-12-21 01:39:31 -06:00
|
|
|
this.queryBuilder = new InfluxQueryBuilder(this.target, this.datasource.database);
|
2016-02-02 15:58:37 -06:00
|
|
|
this.groupBySegment = this.uiSegmentSrv.newPlusButton();
|
2019-11-19 07:59:39 -06:00
|
|
|
this.resultFormats = [
|
|
|
|
{ text: 'Time series', value: 'time_series' },
|
|
|
|
{ text: 'Table', value: 'table' },
|
2020-07-09 09:14:55 -05:00
|
|
|
{ text: 'Logs', value: 'logs' },
|
2019-11-19 07:59:39 -06:00
|
|
|
];
|
2020-06-10 14:26:24 -05:00
|
|
|
|
2016-02-02 15:58:37 -06:00
|
|
|
this.policySegment = uiSegmentSrv.newSegment(this.target.policy);
|
|
|
|
|
|
|
|
if (!this.target.measurement) {
|
|
|
|
this.measurementSegment = uiSegmentSrv.newSelectMeasurement();
|
|
|
|
} else {
|
2017-12-21 01:39:31 -06:00
|
|
|
this.measurementSegment = uiSegmentSrv.newSegment(this.target.measurement);
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
this.tagSegments = [];
|
2018-08-26 10:14:40 -05:00
|
|
|
for (const tag of this.target.tags) {
|
2016-02-02 15:58:37 -06:00
|
|
|
if (!tag.operator) {
|
|
|
|
if (/^\/.*\/$/.test(tag.value)) {
|
2017-12-20 05:33:33 -06:00
|
|
|
tag.operator = '=~';
|
2016-02-02 15:58:37 -06:00
|
|
|
} else {
|
2017-12-20 05:33:33 -06:00
|
|
|
tag.operator = '=';
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag.condition) {
|
|
|
|
this.tagSegments.push(uiSegmentSrv.newCondition(tag.condition));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.tagSegments.push(uiSegmentSrv.newKey(tag.key));
|
|
|
|
this.tagSegments.push(uiSegmentSrv.newOperator(tag.operator));
|
|
|
|
this.tagSegments.push(uiSegmentSrv.newKeyValue(tag.value));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.fixTagSegments();
|
|
|
|
this.buildSelectMenu();
|
2017-12-19 09:06:54 -06:00
|
|
|
this.removeTagFilterSegment = uiSegmentSrv.newSegment({
|
|
|
|
fake: true,
|
2017-12-20 05:33:33 -06:00
|
|
|
value: '-- remove tag filter --',
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
2020-06-22 18:36:58 -05:00
|
|
|
/**
|
|
|
|
* Only called for flux
|
|
|
|
*/
|
2020-06-22 15:03:34 -05:00
|
|
|
onChange = (target: InfluxQuery) => {
|
|
|
|
this.target.query = target.query;
|
|
|
|
};
|
|
|
|
|
2020-06-22 18:36:58 -05:00
|
|
|
onRunQuery = () => {
|
|
|
|
this.panelCtrl.refresh();
|
|
|
|
};
|
|
|
|
|
2017-05-16 09:28:58 -05:00
|
|
|
removeOrderByTime() {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.target.orderByTime = 'ASC';
|
2017-05-16 09:28:58 -05:00
|
|
|
}
|
|
|
|
|
2016-02-02 15:58:37 -06:00
|
|
|
buildSelectMenu() {
|
2018-08-29 07:27:29 -05:00
|
|
|
const categories = queryPart.getCategories();
|
2017-12-19 09:06:54 -06:00
|
|
|
this.selectMenu = _.reduce(
|
|
|
|
categories,
|
2018-09-04 07:27:03 -05:00
|
|
|
(memo, cat, key) => {
|
2018-08-29 07:27:29 -05:00
|
|
|
const menu = {
|
2017-12-19 09:06:54 -06:00
|
|
|
text: key,
|
2019-07-05 00:52:23 -05:00
|
|
|
submenu: cat.map((item: any) => {
|
2017-12-19 09:06:54 -06:00
|
|
|
return { text: item.type, value: item.type };
|
2017-12-20 05:33:33 -06:00
|
|
|
}),
|
2017-12-19 09:06:54 -06:00
|
|
|
};
|
|
|
|
memo.push(menu);
|
|
|
|
return memo;
|
|
|
|
},
|
2020-07-08 04:05:20 -05:00
|
|
|
[] as any
|
2017-12-19 09:06:54 -06:00
|
|
|
);
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
getGroupByOptions() {
|
2018-08-29 07:27:29 -05:00
|
|
|
const query = this.queryBuilder.buildExploreQuery('TAG_KEYS');
|
2017-12-19 09:06:54 -06:00
|
|
|
|
|
|
|
return this.datasource
|
|
|
|
.metricFindQuery(query)
|
2019-07-05 00:52:23 -05:00
|
|
|
.then((tags: any) => {
|
2018-08-29 07:27:29 -05:00
|
|
|
const options = [];
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!this.queryModel.hasFill()) {
|
2017-12-20 05:33:33 -06:00
|
|
|
options.push(this.uiSegmentSrv.newSegment({ value: 'fill(null)' }));
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
|
|
|
if (!this.target.limit) {
|
2017-12-20 05:33:33 -06:00
|
|
|
options.push(this.uiSegmentSrv.newSegment({ value: 'LIMIT' }));
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
|
|
|
if (!this.target.slimit) {
|
2017-12-20 05:33:33 -06:00
|
|
|
options.push(this.uiSegmentSrv.newSegment({ value: 'SLIMIT' }));
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
2018-06-12 11:32:59 -05:00
|
|
|
if (!this.target.tz) {
|
|
|
|
options.push(this.uiSegmentSrv.newSegment({ value: 'tz' }));
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
if (this.target.orderByTime === 'ASC') {
|
2017-12-21 01:39:31 -06:00
|
|
|
options.push(this.uiSegmentSrv.newSegment({ value: 'ORDER BY time DESC' }));
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
|
|
|
if (!this.queryModel.hasGroupByTime()) {
|
2017-12-21 01:39:31 -06:00
|
|
|
options.push(this.uiSegmentSrv.newSegment({ value: 'time($interval)' }));
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
2018-08-26 10:14:40 -05:00
|
|
|
for (const tag of tags) {
|
2017-12-21 01:39:31 -06:00
|
|
|
options.push(this.uiSegmentSrv.newSegment({ value: 'tag(' + tag.text + ')' }));
|
2017-12-19 09:06:54 -06:00
|
|
|
}
|
|
|
|
return options;
|
|
|
|
})
|
|
|
|
.catch(this.handleQueryError.bind(this));
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
groupByAction() {
|
2017-05-16 09:28:58 -05:00
|
|
|
switch (this.groupBySegment.value) {
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'LIMIT': {
|
2017-05-16 09:28:58 -05:00
|
|
|
this.target.limit = 10;
|
|
|
|
break;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'SLIMIT': {
|
2017-05-16 09:28:58 -05:00
|
|
|
this.target.slimit = 10;
|
|
|
|
break;
|
|
|
|
}
|
2018-06-12 11:32:59 -05:00
|
|
|
case 'tz': {
|
|
|
|
this.target.tz = 'UTC';
|
|
|
|
break;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'ORDER BY time DESC': {
|
|
|
|
this.target.orderByTime = 'DESC';
|
2017-05-16 09:28:58 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
this.queryModel.addGroupBy(this.groupBySegment.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 07:27:29 -05:00
|
|
|
const plusButton = this.uiSegmentSrv.newPlusButton();
|
2017-12-19 09:06:54 -06:00
|
|
|
this.groupBySegment.value = plusButton.value;
|
|
|
|
this.groupBySegment.html = plusButton.html;
|
2020-07-03 07:58:29 -05:00
|
|
|
this.groupBySegment.fake = true;
|
2016-02-02 15:58:37 -06:00
|
|
|
this.panelCtrl.refresh();
|
|
|
|
}
|
|
|
|
|
2019-07-05 00:52:23 -05:00
|
|
|
addSelectPart(selectParts: any, cat: any, subitem: { value: any }) {
|
2016-02-02 15:58:37 -06:00
|
|
|
this.queryModel.addSelectPart(selectParts, subitem.value);
|
|
|
|
this.panelCtrl.refresh();
|
|
|
|
}
|
|
|
|
|
2019-07-05 00:52:23 -05:00
|
|
|
handleSelectPartEvent(selectParts: any, part: any, evt: { name: any }) {
|
2016-08-15 06:51:55 -05:00
|
|
|
switch (evt.name) {
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'get-param-options': {
|
2018-08-29 07:27:29 -05:00
|
|
|
const fieldsQuery = this.queryBuilder.buildExploreQuery('FIELDS');
|
2017-12-19 09:06:54 -06:00
|
|
|
return this.datasource
|
|
|
|
.metricFindQuery(fieldsQuery)
|
|
|
|
.then(this.transformToSegments(true))
|
|
|
|
.catch(this.handleQueryError.bind(this));
|
2016-08-15 06:51:55 -05:00
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'part-param-changed': {
|
2016-08-15 10:20:45 -05:00
|
|
|
this.panelCtrl.refresh();
|
|
|
|
break;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'action': {
|
2016-08-15 06:51:55 -05:00
|
|
|
this.queryModel.removeSelectPart(selectParts, part);
|
|
|
|
this.panelCtrl.refresh();
|
2016-08-15 10:20:45 -05:00
|
|
|
break;
|
2016-08-15 06:51:55 -05:00
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'get-part-actions': {
|
2019-12-05 03:04:03 -06:00
|
|
|
return Promise.resolve([{ text: 'Remove', value: 'remove-part' }]);
|
2016-08-15 06:51:55 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-22 15:03:34 -05:00
|
|
|
return Promise.resolve();
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
2019-07-05 00:52:23 -05:00
|
|
|
handleGroupByPartEvent(part: any, index: any, evt: { name: any }) {
|
2016-08-15 10:20:45 -05:00
|
|
|
switch (evt.name) {
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'get-param-options': {
|
2018-08-29 07:27:29 -05:00
|
|
|
const tagsQuery = this.queryBuilder.buildExploreQuery('TAG_KEYS');
|
2017-12-19 09:06:54 -06:00
|
|
|
return this.datasource
|
|
|
|
.metricFindQuery(tagsQuery)
|
|
|
|
.then(this.transformToSegments(true))
|
|
|
|
.catch(this.handleQueryError.bind(this));
|
2016-08-15 10:20:45 -05:00
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'part-param-changed': {
|
2016-08-15 10:20:45 -05:00
|
|
|
this.panelCtrl.refresh();
|
|
|
|
break;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'action': {
|
2016-08-15 10:20:45 -05:00
|
|
|
this.queryModel.removeGroupByPart(part, index);
|
|
|
|
this.panelCtrl.refresh();
|
|
|
|
break;
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
case 'get-part-actions': {
|
2019-12-05 03:04:03 -06:00
|
|
|
return Promise.resolve([{ text: 'Remove', value: 'remove-part' }]);
|
2016-08-15 10:20:45 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-22 15:03:34 -05:00
|
|
|
return Promise.resolve();
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fixTagSegments() {
|
2018-08-29 07:27:29 -05:00
|
|
|
const count = this.tagSegments.length;
|
|
|
|
const lastSegment = this.tagSegments[Math.max(count - 1, 0)];
|
2016-02-02 15:58:37 -06:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
if (!lastSegment || lastSegment.type !== 'plus-button') {
|
2016-02-02 15:58:37 -06:00
|
|
|
this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
measurementChanged() {
|
|
|
|
this.target.measurement = this.measurementSegment.value;
|
|
|
|
this.panelCtrl.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
getPolicySegments() {
|
2018-08-29 07:27:29 -05:00
|
|
|
const policiesQuery = this.queryBuilder.buildExploreQuery('RETENTION POLICIES');
|
2017-12-19 09:06:54 -06:00
|
|
|
return this.datasource
|
|
|
|
.metricFindQuery(policiesQuery)
|
|
|
|
.then(this.transformToSegments(false))
|
|
|
|
.catch(this.handleQueryError.bind(this));
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
policyChanged() {
|
|
|
|
this.target.policy = this.policySegment.value;
|
|
|
|
this.panelCtrl.refresh();
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:03:34 -05:00
|
|
|
// Only valid for InfluxQL queries
|
2016-02-04 15:19:46 -06:00
|
|
|
toggleEditorMode() {
|
2020-09-03 16:11:39 -05:00
|
|
|
if (this.datasource.isFlux) {
|
2020-06-22 18:36:58 -05:00
|
|
|
return; // nothing
|
|
|
|
}
|
|
|
|
|
2016-04-27 03:16:04 -05:00
|
|
|
try {
|
|
|
|
this.target.query = this.queryModel.render(false);
|
|
|
|
} catch (err) {
|
2020-07-10 09:07:04 -05:00
|
|
|
console.error('query render error');
|
2016-04-27 03:16:04 -05:00
|
|
|
}
|
2020-06-22 15:03:34 -05:00
|
|
|
this.target.rawQuery = !this.target.rawQuery;
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
2019-07-05 00:52:23 -05:00
|
|
|
getMeasurements(measurementFilter: any) {
|
2018-08-29 07:27:29 -05:00
|
|
|
const query = this.queryBuilder.buildExploreQuery('MEASUREMENTS', undefined, measurementFilter);
|
2017-12-19 09:06:54 -06:00
|
|
|
return this.datasource
|
|
|
|
.metricFindQuery(query)
|
2016-02-02 15:58:37 -06:00
|
|
|
.then(this.transformToSegments(true))
|
|
|
|
.catch(this.handleQueryError.bind(this));
|
|
|
|
}
|
|
|
|
|
2019-07-05 00:52:23 -05:00
|
|
|
handleQueryError(err: any): any[] {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.error = err.message || 'Failed to issue metric query';
|
2016-02-02 15:58:37 -06:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-07-05 00:52:23 -05:00
|
|
|
transformToSegments(addTemplateVars: any) {
|
|
|
|
return (results: any) => {
|
2021-01-20 00:59:48 -06:00
|
|
|
const segments = _.map(results, (segment) => {
|
2017-12-19 09:06:54 -06:00
|
|
|
return this.uiSegmentSrv.newSegment({
|
|
|
|
value: segment.text,
|
2017-12-20 05:33:33 -06:00
|
|
|
expandable: segment.expandable,
|
2017-12-19 09:06:54 -06:00
|
|
|
});
|
2016-02-02 15:58:37 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
if (addTemplateVars) {
|
2020-03-24 10:03:53 -05:00
|
|
|
for (const variable of this.templateSrv.getVariables()) {
|
2017-12-19 09:06:54 -06:00
|
|
|
segments.unshift(
|
|
|
|
this.uiSegmentSrv.newSegment({
|
2018-01-29 10:04:11 -06:00
|
|
|
type: 'value',
|
2017-12-20 05:33:33 -06:00
|
|
|
value: '/^$' + variable.name + '$/',
|
|
|
|
expandable: true,
|
2017-12-19 09:06:54 -06:00
|
|
|
})
|
|
|
|
);
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return segments;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-05 00:52:23 -05:00
|
|
|
getTagsOrValues(segment: { type: string }, index: number) {
|
2017-12-20 05:33:33 -06:00
|
|
|
if (segment.type === 'condition') {
|
2019-12-05 03:04:03 -06:00
|
|
|
return Promise.resolve([this.uiSegmentSrv.newSegment('AND'), this.uiSegmentSrv.newSegment('OR')]);
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
2020-07-03 07:58:29 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
if (segment.type === 'operator') {
|
2018-08-29 07:27:29 -05:00
|
|
|
const nextValue = this.tagSegments[index + 1].value;
|
2016-02-02 15:58:37 -06:00
|
|
|
if (/^\/.*\/$/.test(nextValue)) {
|
2019-12-05 03:04:03 -06:00
|
|
|
return Promise.resolve(this.uiSegmentSrv.newOperators(['=~', '!~']));
|
2016-02-02 15:58:37 -06:00
|
|
|
} else {
|
2019-12-05 03:04:03 -06:00
|
|
|
return Promise.resolve(this.uiSegmentSrv.newOperators(['=', '!=', '<>', '<', '>']));
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 02:03:11 -05:00
|
|
|
let query, addTemplateVars;
|
2017-12-20 05:33:33 -06:00
|
|
|
if (segment.type === 'key' || segment.type === 'plus-button') {
|
|
|
|
query = this.queryBuilder.buildExploreQuery('TAG_KEYS');
|
2016-02-02 15:58:37 -06:00
|
|
|
addTemplateVars = false;
|
2017-12-20 05:33:33 -06:00
|
|
|
} else if (segment.type === 'value') {
|
2017-12-21 01:39:31 -06:00
|
|
|
query = this.queryBuilder.buildExploreQuery('TAG_VALUES', this.tagSegments[index - 2].value);
|
2016-02-02 15:58:37 -06:00
|
|
|
addTemplateVars = true;
|
|
|
|
}
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
return this.datasource
|
|
|
|
.metricFindQuery(query)
|
|
|
|
.then(this.transformToSegments(addTemplateVars))
|
2019-07-05 00:52:23 -05:00
|
|
|
.then((results: any) => {
|
2017-12-20 05:33:33 -06:00
|
|
|
if (segment.type === 'key') {
|
2017-12-19 09:06:54 -06:00
|
|
|
results.splice(0, 0, angular.copy(this.removeTagFilterSegment));
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
})
|
|
|
|
.catch(this.handleQueryError.bind(this));
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
getFieldSegments() {
|
2018-08-29 07:27:29 -05:00
|
|
|
const fieldsQuery = this.queryBuilder.buildExploreQuery('FIELDS');
|
2017-12-19 09:06:54 -06:00
|
|
|
return this.datasource
|
|
|
|
.metricFindQuery(fieldsQuery)
|
|
|
|
.then(this.transformToSegments(false))
|
|
|
|
.catch(this.handleQueryError);
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
2019-07-05 00:52:23 -05:00
|
|
|
tagSegmentUpdated(segment: { value: any; type: string; cssClass: string }, index: number) {
|
2016-02-02 15:58:37 -06:00
|
|
|
this.tagSegments[index] = segment;
|
|
|
|
|
|
|
|
// handle remove tag condition
|
|
|
|
if (segment.value === this.removeTagFilterSegment.value) {
|
|
|
|
this.tagSegments.splice(index, 3);
|
|
|
|
if (this.tagSegments.length === 0) {
|
|
|
|
this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
|
|
|
|
} else if (this.tagSegments.length > 2) {
|
2017-12-19 09:06:54 -06:00
|
|
|
this.tagSegments.splice(Math.max(index - 1, 0), 1);
|
2017-12-21 01:39:31 -06:00
|
|
|
if (this.tagSegments[this.tagSegments.length - 1].type !== 'plus-button') {
|
2016-02-02 15:58:37 -06:00
|
|
|
this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-20 05:33:33 -06:00
|
|
|
if (segment.type === 'plus-button') {
|
2016-02-02 15:58:37 -06:00
|
|
|
if (index > 2) {
|
2017-12-21 01:39:31 -06:00
|
|
|
this.tagSegments.splice(index, 0, this.uiSegmentSrv.newCondition('AND'));
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
this.tagSegments.push(this.uiSegmentSrv.newOperator('='));
|
2017-12-21 01:39:31 -06:00
|
|
|
this.tagSegments.push(this.uiSegmentSrv.newFake('select tag value', 'value', 'query-segment-value'));
|
2017-12-20 05:33:33 -06:00
|
|
|
segment.type = 'key';
|
|
|
|
segment.cssClass = 'query-segment-key';
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
if (index + 1 === this.tagSegments.length) {
|
2016-02-02 15:58:37 -06:00
|
|
|
this.tagSegments.push(this.uiSegmentSrv.newPlusButton());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.rebuildTargetTagConditions();
|
|
|
|
}
|
|
|
|
|
|
|
|
rebuildTargetTagConditions() {
|
2019-07-05 00:52:23 -05:00
|
|
|
const tags: any[] = [];
|
2018-08-30 02:03:11 -05:00
|
|
|
let tagIndex = 0;
|
2020-07-08 04:05:20 -05:00
|
|
|
let tagOperator: string | null = '';
|
2016-02-02 15:58:37 -06:00
|
|
|
|
|
|
|
_.each(this.tagSegments, (segment2, index) => {
|
2017-12-20 05:33:33 -06:00
|
|
|
if (segment2.type === 'key') {
|
2016-02-02 15:58:37 -06:00
|
|
|
if (tags.length === 0) {
|
|
|
|
tags.push({});
|
|
|
|
}
|
|
|
|
tags[tagIndex].key = segment2.value;
|
2017-12-20 05:33:33 -06:00
|
|
|
} else if (segment2.type === 'value') {
|
2017-12-21 01:39:31 -06:00
|
|
|
tagOperator = this.getTagValueOperator(segment2.value, tags[tagIndex].operator);
|
2016-02-02 15:58:37 -06:00
|
|
|
if (tagOperator) {
|
2017-12-21 01:39:31 -06:00
|
|
|
this.tagSegments[index - 1] = this.uiSegmentSrv.newOperator(tagOperator);
|
2016-02-02 15:58:37 -06:00
|
|
|
tags[tagIndex].operator = tagOperator;
|
|
|
|
}
|
|
|
|
tags[tagIndex].value = segment2.value;
|
2017-12-20 05:33:33 -06:00
|
|
|
} else if (segment2.type === 'condition') {
|
2016-02-02 15:58:37 -06:00
|
|
|
tags.push({ condition: segment2.value });
|
|
|
|
tagIndex += 1;
|
2017-12-20 05:33:33 -06:00
|
|
|
} else if (segment2.type === 'operator') {
|
2016-02-02 15:58:37 -06:00
|
|
|
tags[tagIndex].operator = segment2.value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.target.tags = tags;
|
|
|
|
this.panelCtrl.refresh();
|
|
|
|
}
|
|
|
|
|
2020-07-08 04:05:20 -05:00
|
|
|
getTagValueOperator(tagValue: string, tagOperator: string): string | null {
|
2017-12-21 01:39:31 -06:00
|
|
|
if (tagOperator !== '=~' && tagOperator !== '!~' && /^\/.*\/$/.test(tagValue)) {
|
2017-12-20 05:33:33 -06:00
|
|
|
return '=~';
|
2017-12-21 01:39:31 -06:00
|
|
|
} else if ((tagOperator === '=~' || tagOperator === '!~') && /^(?!\/.*\/$)/.test(tagValue)) {
|
2017-12-20 05:33:33 -06:00
|
|
|
return '=';
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
2017-09-26 04:24:58 -05:00
|
|
|
return null;
|
2016-02-02 15:58:37 -06:00
|
|
|
}
|
|
|
|
}
|