query_ctrl.ts 10 KB

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