| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- import './add_graphite_func';
- import './func_editor';
- import _ from 'lodash';
- import gfunc from './gfunc';
- import GraphiteQuery from './graphite_query';
- import {QueryCtrl} from 'app/plugins/sdk';
- import appEvents from 'app/core/app_events';
- const GRAPHITE_TAG_OPERATORS = ['=', '!=', '=~', '!=~'];
- export class GraphiteQueryCtrl extends QueryCtrl {
- static templateUrl = 'partials/query.editor.html';
- queryModel: GraphiteQuery;
- segments: any[];
- addTagSegments: any[];
- removeTagValue: string;
- /** @ngInject **/
- constructor($scope, $injector, private uiSegmentSrv, private templateSrv) {
- super($scope, $injector);
- if (this.target) {
- this.target.target = this.target.target || '';
- this.queryModel = new GraphiteQuery(this.target, templateSrv);
- this.buildSegments();
- }
- this.removeTagValue = '-- remove tag --';
- }
- parseTarget() {
- this.queryModel.parseTarget();
- this.buildSegments();
- }
- toggleEditorMode() {
- this.target.textEditor = !this.target.textEditor;
- this.parseTarget();
- }
- buildSegments() {
- this.segments = _.map(this.queryModel.segments, segment => {
- return this.uiSegmentSrv.newSegment(segment);
- });
- let checkOtherSegmentsIndex = this.queryModel.checkOtherSegmentsIndex || 0;
- this.checkOtherSegments(checkOtherSegmentsIndex);
- if (this.queryModel.seriesByTagUsed) {
- this.fixTagSegments();
- }
- }
- addSelectMetricSegment() {
- this.queryModel.addSelectMetricSegment();
- this.segments.push(this.uiSegmentSrv.newSelectMetric());
- }
- checkOtherSegments(fromIndex) {
- if (fromIndex === 0) {
- this.addSelectMetricSegment();
- return;
- }
- var path = this.queryModel.getSegmentPathUpTo(fromIndex + 1);
- if (path === "") {
- return Promise.resolve();
- }
- return this.datasource.metricFindQuery(path).then(segments => {
- if (segments.length === 0) {
- if (path !== '') {
- this.queryModel.segments = this.queryModel.segments.splice(0, fromIndex);
- 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);
- }
- }
- }).catch(err => {
- appEvents.emit('alert-error', ['Error', err]);
- });
- }
- setSegmentFocus(segmentIndex) {
- _.each(this.segments, (segment, index) => {
- segment.focus = segmentIndex === index;
- });
- }
- getAltSegments(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 => {
- var altSegments = _.map(segments, segment => {
- return this.uiSegmentSrv.newSegment({value: segment.text, expandable: segment.expandable});
- });
- if (altSegments.length === 0) { return altSegments; }
- // add template variables
- _.each(this.templateSrv.variables, variable => {
- altSegments.unshift(this.uiSegmentSrv.newSegment({
- type: 'template',
- value: '$' + variable.name,
- expandable: true,
- }));
- });
- // add wildcard option
- altSegments.unshift(this.uiSegmentSrv.newSegment('*'));
- return altSegments;
- }).catch(err => {
- return [];
- });
- }
- segmentValueChanged(segment, segmentIndex) {
- this.error = null;
- this.queryModel.updateSegmentValue(segment, segmentIndex);
- if (this.queryModel.functions.length > 0 && this.queryModel.functions[0].def.fake) {
- this.queryModel.functions = [];
- }
- if (segment.expandable) {
- return this.checkOtherSegments(segmentIndex + 1).then(() => {
- this.setSegmentFocus(segmentIndex + 1);
- this.targetChanged();
- });
- } else {
- this.spliceSegments(segmentIndex + 1);
- }
- this.setSegmentFocus(segmentIndex + 1);
- this.targetChanged();
- }
- spliceSegments(index) {
- this.segments = this.segments.splice(0, index);
- this.queryModel.segments = this.queryModel.segments.splice(0, index);
- }
- emptySegments() {
- this.queryModel.segments = [];
- this.segments = [];
- }
- targetTextChanged() {
- this.updateModelTarget();
- this.refresh();
- }
- updateModelTarget() {
- this.queryModel.updateModelTarget(this.panelCtrl.panel.targets);
- }
- targetChanged() {
- if (this.queryModel.error) {
- return;
- }
- var oldTarget = this.queryModel.target.target;
- this.updateModelTarget();
- if (this.queryModel.target !== oldTarget) {
- var lastSegment = this.segments.length > 0 ? this.segments[this.segments.length - 1] : {};
- if (lastSegment.value !== 'select metric') {
- this.panelCtrl.refresh();
- }
- }
- }
- addFunction(funcDef) {
- var newFunc = gfunc.createFuncInstance(funcDef, { withDefaultParams: true });
- newFunc.added = true;
- this.queryModel.addFunction(newFunc);
- this.smartlyHandleNewAliasByNode(newFunc);
- if (this.segments.length === 1 && this.segments[0].fake) {
- this.emptySegments();
- }
- if (!newFunc.params.length && newFunc.added) {
- this.targetChanged();
- }
- if (newFunc.def.name === 'seriesByTag') {
- this.parseTarget();
- }
- }
- removeFunction(func) {
- this.queryModel.removeFunction(func);
- this.targetChanged();
- }
- smartlyHandleNewAliasByNode(func) {
- if (func.def.name !== 'aliasByNode') {
- return;
- }
- for (var i = 0; i < this.segments.length; i++) {
- if (this.segments[i].value.indexOf('*') >= 0) {
- func.params[0] = i;
- func.added = false;
- this.targetChanged();
- return;
- }
- }
- }
- getTags() {
- return this.datasource.getTags().then((values) => {
- let altTags = _.map(values, 'text');
- altTags.splice(0, 0, this.removeTagValue);
- return mapToDropdownOptions(altTags);
- });
- }
- getTagsAsSegments() {
- return this.datasource.getTags().then((values) => {
- return _.map(values, (val) => {
- return this.uiSegmentSrv.newSegment(val.text);
- });
- });
- }
- getTagOperators() {
- return mapToDropdownOptions(GRAPHITE_TAG_OPERATORS);
- }
- getTagValues(tag) {
- let tagKey = tag.key;
- return this.datasource.getTagValues(tagKey).then((values) => {
- let altValues = _.map(values, 'text');
- return mapToDropdownOptions(altValues);
- });
- }
- tagChanged(tag, tagIndex) {
- this.queryModel.updateTag(tag, tagIndex);
- this.targetChanged();
- }
- addNewTag(segment) {
- let newTagKey = segment.value;
- let newTag = {key: newTagKey, operator: '=', value: 'select tag value'};
- this.queryModel.addTag(newTag);
- this.targetChanged();
- this.fixTagSegments();
- }
- removeTag(index) {
- this.queryModel.removeTag(index);
- 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()];
- }
- showDelimiter(index) {
- return index !== this.queryModel.tags.length - 1;
- }
- }
- function mapToDropdownOptions(results) {
- return _.map(results, (value) => {
- return {text: value, value: value};
- });
- }
|