query_ctrl.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 (this.queryModel.segments.length === 1 && this.queryModel.segments[0].type === 'series-ref') {
  52. return;
  53. }
  54. if (fromIndex === 0) {
  55. this.addSelectMetricSegment();
  56. return;
  57. }
  58. var path = this.queryModel.getSegmentPathUpTo(fromIndex + 1);
  59. if (path === "") {
  60. return Promise.resolve();
  61. }
  62. return this.datasource.metricFindQuery(path).then(segments => {
  63. if (segments.length === 0) {
  64. if (path !== '') {
  65. this.queryModel.segments = this.queryModel.segments.splice(0, fromIndex);
  66. this.segments = this.segments.splice(0, fromIndex);
  67. this.addSelectMetricSegment();
  68. }
  69. } else if (segments[0].expandable) {
  70. if (this.segments.length === fromIndex) {
  71. this.addSelectMetricSegment();
  72. } else {
  73. return this.checkOtherSegments(fromIndex + 1);
  74. }
  75. }
  76. }).catch(err => {
  77. appEvents.emit('alert-error', ['Error', err]);
  78. });
  79. }
  80. setSegmentFocus(segmentIndex) {
  81. _.each(this.segments, (segment, index) => {
  82. segment.focus = segmentIndex === index;
  83. });
  84. }
  85. getAltSegments(index) {
  86. var query = index === 0 ? '*' : this.queryModel.getSegmentPathUpTo(index) + '.*';
  87. var options = {range: this.panelCtrl.range, requestId: "get-alt-segments"};
  88. return this.datasource.metricFindQuery(query, options).then(segments => {
  89. var altSegments = _.map(segments, segment => {
  90. return this.uiSegmentSrv.newSegment({value: segment.text, expandable: segment.expandable});
  91. });
  92. if (altSegments.length === 0) { return altSegments; }
  93. // add query references
  94. if (index === 0) {
  95. _.eachRight(this.panelCtrl.panel.targets, target => {
  96. if (target.refId === this.queryModel.target.refId) {
  97. return;
  98. }
  99. altSegments.unshift(this.uiSegmentSrv.newSegment({
  100. type: 'series-ref',
  101. value: '#' + target.refId,
  102. expandable: false,
  103. }));
  104. });
  105. }
  106. // add template variables
  107. _.eachRight(this.templateSrv.variables, variable => {
  108. altSegments.unshift(this.uiSegmentSrv.newSegment({
  109. type: 'template',
  110. value: '$' + variable.name,
  111. expandable: true,
  112. }));
  113. });
  114. // add wildcard option
  115. altSegments.unshift(this.uiSegmentSrv.newSegment('*'));
  116. if (this.supportsTags && index === 0) {
  117. this.removeTaggedEntry(altSegments);
  118. return this.addAltTagSegments(index, altSegments);
  119. } else {
  120. return altSegments;
  121. }
  122. }).catch(err => {
  123. return [];
  124. });
  125. }
  126. addAltTagSegments(index, altSegments) {
  127. return this.getTagsAsSegments().then((tagSegments) => {
  128. tagSegments = _.map(tagSegments, (segment) => {
  129. segment.value = TAG_PREFIX + segment.value;
  130. return segment;
  131. });
  132. return altSegments.concat(...tagSegments);
  133. });
  134. }
  135. removeTaggedEntry(altSegments) {
  136. altSegments = _.remove(altSegments, (s) => s.value === '_tagged');
  137. }
  138. segmentValueChanged(segment, segmentIndex) {
  139. this.error = null;
  140. this.queryModel.updateSegmentValue(segment, segmentIndex);
  141. if (this.queryModel.functions.length > 0 && this.queryModel.functions[0].def.fake) {
  142. this.queryModel.functions = [];
  143. }
  144. if (segment.type === 'tag') {
  145. let tag = removeTagPrefix(segment.value);
  146. this.addSeriesByTagFunc(tag);
  147. return;
  148. }
  149. if (segment.expandable) {
  150. return this.checkOtherSegments(segmentIndex + 1).then(() => {
  151. this.setSegmentFocus(segmentIndex + 1);
  152. this.targetChanged();
  153. });
  154. } else {
  155. this.spliceSegments(segmentIndex + 1);
  156. }
  157. this.setSegmentFocus(segmentIndex + 1);
  158. this.targetChanged();
  159. }
  160. spliceSegments(index) {
  161. this.segments = this.segments.splice(0, index);
  162. this.queryModel.segments = this.queryModel.segments.splice(0, index);
  163. }
  164. emptySegments() {
  165. this.queryModel.segments = [];
  166. this.segments = [];
  167. }
  168. targetTextChanged() {
  169. this.updateModelTarget();
  170. this.refresh();
  171. }
  172. updateModelTarget() {
  173. this.queryModel.updateModelTarget(this.panelCtrl.panel.targets);
  174. }
  175. targetChanged() {
  176. if (this.queryModel.error) {
  177. return;
  178. }
  179. var oldTarget = this.queryModel.target.target;
  180. this.updateModelTarget();
  181. if (this.queryModel.target !== oldTarget) {
  182. this.panelCtrl.refresh();
  183. }
  184. }
  185. addFunction(funcDef) {
  186. var newFunc = gfunc.createFuncInstance(funcDef, { withDefaultParams: true });
  187. newFunc.added = true;
  188. this.queryModel.addFunction(newFunc);
  189. this.smartlyHandleNewAliasByNode(newFunc);
  190. if (this.segments.length === 1 && this.segments[0].fake) {
  191. this.emptySegments();
  192. }
  193. if (!newFunc.params.length && newFunc.added) {
  194. this.targetChanged();
  195. }
  196. if (newFunc.def.name === 'seriesByTag') {
  197. this.parseTarget();
  198. }
  199. }
  200. removeFunction(func) {
  201. this.queryModel.removeFunction(func);
  202. this.targetChanged();
  203. }
  204. addSeriesByTagFunc(tag) {
  205. let funcDef = gfunc.getFuncDef('seriesByTag');
  206. let newFunc = gfunc.createFuncInstance(funcDef, { withDefaultParams: false });
  207. let tagParam = `${tag}=select tag value`;
  208. newFunc.params = [tagParam];
  209. this.queryModel.addFunction(newFunc);
  210. newFunc.added = true;
  211. this.emptySegments();
  212. this.targetChanged();
  213. this.parseTarget();
  214. }
  215. smartlyHandleNewAliasByNode(func) {
  216. if (func.def.name !== 'aliasByNode') {
  217. return;
  218. }
  219. for (var i = 0; i < this.segments.length; i++) {
  220. if (this.segments[i].value.indexOf('*') >= 0) {
  221. func.params[0] = i;
  222. func.added = false;
  223. this.targetChanged();
  224. return;
  225. }
  226. }
  227. }
  228. getAllTags() {
  229. return this.datasource.getTags().then((values) => {
  230. let altTags = _.map(values, 'text');
  231. altTags.splice(0, 0, this.removeTagValue);
  232. return mapToDropdownOptions(altTags);
  233. });
  234. }
  235. getTags(index, tagPrefix) {
  236. let tagExpressions = this.queryModel.renderTagExpressions(index);
  237. return this.datasource.getTagsAutoComplete(tagExpressions, tagPrefix)
  238. .then((values) => {
  239. let altTags = _.map(values, 'text');
  240. altTags.splice(0, 0, this.removeTagValue);
  241. return mapToDropdownOptions(altTags);
  242. });
  243. }
  244. getTagsAsSegments() {
  245. let tagExpressions = this.queryModel.renderTagExpressions();
  246. return this.datasource.getTagsAutoComplete(tagExpressions)
  247. .then((values) => {
  248. return _.map(values, (val) => {
  249. return this.uiSegmentSrv.newSegment({value: val.text, type: 'tag', expandable: false});
  250. });
  251. });
  252. }
  253. getTagOperators() {
  254. return mapToDropdownOptions(GRAPHITE_TAG_OPERATORS);
  255. }
  256. getAllTagValues(tag) {
  257. let tagKey = tag.key;
  258. return this.datasource.getTagValues(tagKey).then((values) => {
  259. let altValues = _.map(values, 'text');
  260. return mapToDropdownOptions(altValues);
  261. });
  262. }
  263. getTagValues(tag, index, valuePrefix) {
  264. let tagExpressions = this.queryModel.renderTagExpressions(index);
  265. let tagKey = tag.key;
  266. return this.datasource.getTagValuesAutoComplete(tagExpressions, tagKey, valuePrefix).then((values) => {
  267. let altValues = _.map(values, 'text');
  268. return mapToDropdownOptions(altValues);
  269. });
  270. }
  271. tagChanged(tag, tagIndex) {
  272. this.queryModel.updateTag(tag, tagIndex);
  273. this.targetChanged();
  274. }
  275. addNewTag(segment) {
  276. let newTagKey = segment.value;
  277. let newTag = {key: newTagKey, operator: '=', value: 'select tag value'};
  278. this.queryModel.addTag(newTag);
  279. this.targetChanged();
  280. this.fixTagSegments();
  281. }
  282. removeTag(index) {
  283. this.queryModel.removeTag(index);
  284. this.targetChanged();
  285. }
  286. fixTagSegments() {
  287. // Adding tag with the same name as just removed works incorrectly if single segment is used (instead of array)
  288. this.addTagSegments = [this.uiSegmentSrv.newPlusButton()];
  289. }
  290. showDelimiter(index) {
  291. return index !== this.queryModel.tags.length - 1;
  292. }
  293. }
  294. function mapToDropdownOptions(results) {
  295. return _.map(results, (value) => {
  296. return {text: value, value: value};
  297. });
  298. }
  299. function removeTagPrefix(value: string): string {
  300. return value.replace(TAG_PREFIX, '');
  301. }