query_ctrl.ts 10 KB

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