query_ctrl.ts 10 KB

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