query_ctrl.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import './add_graphite_func';
  2. import './func_editor';
  3. import _ from 'lodash';
  4. import gfunc from './gfunc';
  5. import GraphiteQuery from './graphite_query';
  6. import {QueryCtrl} from 'app/plugins/sdk';
  7. import appEvents from 'app/core/app_events';
  8. const GRAPHITE_TAG_OPERATORS = ['=', '!=', '=~', '!=~'];
  9. const TAG_PREFIX = 'tag: ';
  10. export class GraphiteQueryCtrl extends QueryCtrl {
  11. static templateUrl = 'partials/query.editor.html';
  12. queryModel: GraphiteQuery;
  13. segments: any[];
  14. addTagSegments: any[];
  15. removeTagValue: string;
  16. supportsTags: boolean;
  17. /** @ngInject **/
  18. constructor($scope, $injector, private uiSegmentSrv, private templateSrv) {
  19. super($scope, $injector);
  20. this.supportsTags = this.datasource.supportsTags;
  21. if (this.target) {
  22. this.target.target = this.target.target || '';
  23. this.queryModel = new GraphiteQuery(this.target, templateSrv);
  24. this.buildSegments();
  25. }
  26. this.removeTagValue = '-- remove tag --';
  27. }
  28. parseTarget() {
  29. this.queryModel.parseTarget();
  30. this.buildSegments();
  31. }
  32. toggleEditorMode() {
  33. this.target.textEditor = !this.target.textEditor;
  34. this.parseTarget();
  35. }
  36. buildSegments() {
  37. this.segments = _.map(this.queryModel.segments, segment => {
  38. return this.uiSegmentSrv.newSegment(segment);
  39. });
  40. let checkOtherSegmentsIndex = this.queryModel.checkOtherSegmentsIndex || 0;
  41. this.checkOtherSegments(checkOtherSegmentsIndex);
  42. if (this.queryModel.seriesByTagUsed) {
  43. this.fixTagSegments();
  44. }
  45. }
  46. addSelectMetricSegment() {
  47. this.queryModel.addSelectMetricSegment();
  48. this.segments.push(this.uiSegmentSrv.newSelectMetric());
  49. }
  50. checkOtherSegments(fromIndex) {
  51. if (fromIndex === 0) {
  52. this.addSelectMetricSegment();
  53. return;
  54. }
  55. var path = this.queryModel.getSegmentPathUpTo(fromIndex + 1);
  56. if (path === "") {
  57. return Promise.resolve();
  58. }
  59. return this.datasource.metricFindQuery(path).then(segments => {
  60. if (segments.length === 0) {
  61. if (path !== '') {
  62. this.queryModel.segments = this.queryModel.segments.splice(0, fromIndex);
  63. this.segments = this.segments.splice(0, fromIndex);
  64. this.addSelectMetricSegment();
  65. }
  66. } else if (segments[0].expandable) {
  67. if (this.segments.length === fromIndex) {
  68. this.addSelectMetricSegment();
  69. } else {
  70. return this.checkOtherSegments(fromIndex + 1);
  71. }
  72. }
  73. }).catch(err => {
  74. appEvents.emit('alert-error', ['Error', err]);
  75. });
  76. }
  77. setSegmentFocus(segmentIndex) {
  78. _.each(this.segments, (segment, index) => {
  79. segment.focus = segmentIndex === index;
  80. });
  81. }
  82. getAltSegments(index) {
  83. var query = index === 0 ? '*' : this.queryModel.getSegmentPathUpTo(index) + '.*';
  84. var options = {range: this.panelCtrl.range, requestId: "get-alt-segments"};
  85. return this.datasource.metricFindQuery(query, options).then(segments => {
  86. var altSegments = _.map(segments, segment => {
  87. return this.uiSegmentSrv.newSegment({value: segment.text, expandable: segment.expandable});
  88. });
  89. if (altSegments.length === 0) { return altSegments; }
  90. // add template variables
  91. _.each(this.templateSrv.variables, variable => {
  92. altSegments.unshift(this.uiSegmentSrv.newSegment({
  93. type: 'template',
  94. value: '$' + variable.name,
  95. expandable: true,
  96. }));
  97. });
  98. // add wildcard option
  99. altSegments.unshift(this.uiSegmentSrv.newSegment('*'));
  100. if (this.supportsTags && index === 0) {
  101. this.removeTaggedEntry(altSegments);
  102. return this.addAltTagSegments(index, altSegments);
  103. } else {
  104. return altSegments;
  105. }
  106. }).catch(err => {
  107. return [];
  108. });
  109. }
  110. addAltTagSegments(index, altSegments) {
  111. return this.getTagsAsSegments().then((tagSegments) => {
  112. tagSegments = _.map(tagSegments, (segment) => {
  113. segment.value = TAG_PREFIX + segment.value;
  114. return segment;
  115. });
  116. return altSegments.concat(...tagSegments);
  117. });
  118. }
  119. removeTaggedEntry(altSegments) {
  120. altSegments = _.remove(altSegments, (s) => s.value === '_tagged');
  121. }
  122. segmentValueChanged(segment, segmentIndex) {
  123. this.error = null;
  124. this.queryModel.updateSegmentValue(segment, segmentIndex);
  125. if (this.queryModel.functions.length > 0 && this.queryModel.functions[0].def.fake) {
  126. this.queryModel.functions = [];
  127. }
  128. if (segment.type === 'tag') {
  129. let tag = removeTagPrefix(segment.value);
  130. this.addSeriesByTagFunc(tag);
  131. return;
  132. }
  133. if (segment.expandable) {
  134. return this.checkOtherSegments(segmentIndex + 1).then(() => {
  135. this.setSegmentFocus(segmentIndex + 1);
  136. this.targetChanged();
  137. });
  138. } else {
  139. this.spliceSegments(segmentIndex + 1);
  140. }
  141. this.setSegmentFocus(segmentIndex + 1);
  142. this.targetChanged();
  143. }
  144. spliceSegments(index) {
  145. this.segments = this.segments.splice(0, index);
  146. this.queryModel.segments = this.queryModel.segments.splice(0, index);
  147. }
  148. emptySegments() {
  149. this.queryModel.segments = [];
  150. this.segments = [];
  151. }
  152. targetTextChanged() {
  153. this.updateModelTarget();
  154. this.refresh();
  155. }
  156. updateModelTarget() {
  157. this.queryModel.updateModelTarget(this.panelCtrl.panel.targets);
  158. }
  159. targetChanged() {
  160. if (this.queryModel.error) {
  161. return;
  162. }
  163. var oldTarget = this.queryModel.target.target;
  164. this.updateModelTarget();
  165. if (this.queryModel.target !== oldTarget) {
  166. var lastSegment = this.segments.length > 0 ? this.segments[this.segments.length - 1] : {};
  167. if (lastSegment.value !== 'select metric') {
  168. this.panelCtrl.refresh();
  169. }
  170. }
  171. }
  172. addFunction(funcDef) {
  173. var newFunc = gfunc.createFuncInstance(funcDef, { withDefaultParams: true });
  174. newFunc.added = true;
  175. this.queryModel.addFunction(newFunc);
  176. this.smartlyHandleNewAliasByNode(newFunc);
  177. if (this.segments.length === 1 && this.segments[0].fake) {
  178. this.emptySegments();
  179. }
  180. if (!newFunc.params.length && newFunc.added) {
  181. this.targetChanged();
  182. }
  183. if (newFunc.def.name === 'seriesByTag') {
  184. this.parseTarget();
  185. }
  186. }
  187. removeFunction(func) {
  188. this.queryModel.removeFunction(func);
  189. this.targetChanged();
  190. }
  191. addSeriesByTagFunc(tag) {
  192. let funcDef = gfunc.getFuncDef('seriesByTag');
  193. let newFunc = gfunc.createFuncInstance(funcDef, { withDefaultParams: false });
  194. let tagParam = `${tag}=select tag value`;
  195. newFunc.params = [tagParam];
  196. this.queryModel.addFunction(newFunc);
  197. newFunc.added = true;
  198. this.emptySegments();
  199. this.targetChanged();
  200. this.parseTarget();
  201. }
  202. smartlyHandleNewAliasByNode(func) {
  203. if (func.def.name !== 'aliasByNode') {
  204. return;
  205. }
  206. for (var i = 0; i < this.segments.length; i++) {
  207. if (this.segments[i].value.indexOf('*') >= 0) {
  208. func.params[0] = i;
  209. func.added = false;
  210. this.targetChanged();
  211. return;
  212. }
  213. }
  214. }
  215. getAllTags() {
  216. return this.datasource.getTags().then((values) => {
  217. let altTags = _.map(values, 'text');
  218. altTags.splice(0, 0, this.removeTagValue);
  219. return mapToDropdownOptions(altTags);
  220. });
  221. }
  222. getTags(index, tagPrefix) {
  223. let tagExpressions = this.queryModel.renderTagExpressions(index);
  224. return this.datasource.getTagsAutoComplete(tagExpressions, tagPrefix)
  225. .then((values) => {
  226. let altTags = _.map(values, 'text');
  227. altTags.splice(0, 0, this.removeTagValue);
  228. return mapToDropdownOptions(altTags);
  229. });
  230. }
  231. getTagsAsSegments() {
  232. let tagExpressions = this.queryModel.renderTagExpressions();
  233. return this.datasource.getTagsAutoComplete(tagExpressions)
  234. .then((values) => {
  235. return _.map(values, (val) => {
  236. return this.uiSegmentSrv.newSegment({value: val.text, type: 'tag', expandable: false});
  237. });
  238. });
  239. }
  240. getTagOperators() {
  241. return mapToDropdownOptions(GRAPHITE_TAG_OPERATORS);
  242. }
  243. getAllTagValues(tag) {
  244. let tagKey = tag.key;
  245. return this.datasource.getTagValues(tagKey).then((values) => {
  246. let altValues = _.map(values, 'text');
  247. return mapToDropdownOptions(altValues);
  248. });
  249. }
  250. getTagValues(tag, index, valuePrefix) {
  251. let tagExpressions = this.queryModel.renderTagExpressions(index);
  252. let tagKey = tag.key;
  253. return this.datasource.getTagValuesAutoComplete(tagExpressions, tagKey, valuePrefix).then((values) => {
  254. let altValues = _.map(values, 'text');
  255. return mapToDropdownOptions(altValues);
  256. });
  257. }
  258. tagChanged(tag, tagIndex) {
  259. this.queryModel.updateTag(tag, tagIndex);
  260. this.targetChanged();
  261. }
  262. addNewTag(segment) {
  263. let newTagKey = segment.value;
  264. let newTag = {key: newTagKey, operator: '=', value: 'select tag value'};
  265. this.queryModel.addTag(newTag);
  266. this.targetChanged();
  267. this.fixTagSegments();
  268. }
  269. removeTag(index) {
  270. this.queryModel.removeTag(index);
  271. this.targetChanged();
  272. }
  273. fixTagSegments() {
  274. // Adding tag with the same name as just removed works incorrectly if single segment is used (instead of array)
  275. this.addTagSegments = [this.uiSegmentSrv.newPlusButton()];
  276. }
  277. showDelimiter(index) {
  278. return index !== this.queryModel.tags.length - 1;
  279. }
  280. }
  281. function mapToDropdownOptions(results) {
  282. return _.map(results, (value) => {
  283. return {text: value, value: value};
  284. });
  285. }
  286. function removeTagPrefix(value: string): string {
  287. return value.replace(TAG_PREFIX, '');
  288. }