query_ctrl.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import './add_graphite_func';
  3. import './func_editor';
  4. import angular from 'angular';
  5. import _ from 'lodash';
  6. import moment from 'moment';
  7. import gfunc from './gfunc';
  8. import {Parser} from './parser';
  9. import {QueryCtrl} from 'app/plugins/sdk';
  10. import appEvents from 'app/core/app_events';
  11. export class GraphiteQueryCtrl extends QueryCtrl {
  12. static templateUrl = 'partials/query.editor.html';
  13. functions: any[];
  14. segments: any[];
  15. /** @ngInject **/
  16. constructor($scope, $injector, private uiSegmentSrv, private templateSrv) {
  17. super($scope, $injector);
  18. if (this.target) {
  19. this.target.target = this.target.target || '';
  20. this.parseTarget();
  21. }
  22. }
  23. toggleEditorMode() {
  24. this.target.textEditor = !this.target.textEditor;
  25. this.parseTarget();
  26. }
  27. parseTarget() {
  28. this.functions = [];
  29. this.segments = [];
  30. this.error = null;
  31. if (this.target.textEditor) {
  32. return;
  33. }
  34. var parser = new Parser(this.target.target);
  35. var astNode = parser.getAst();
  36. if (astNode === null) {
  37. this.checkOtherSegments(0);
  38. return;
  39. }
  40. if (astNode.type === 'error') {
  41. this.error = astNode.message + " at position: " + astNode.pos;
  42. this.target.textEditor = true;
  43. return;
  44. }
  45. try {
  46. this.parseTargeRecursive(astNode, null, 0);
  47. } catch (err) {
  48. console.log('error parsing target:', err.message);
  49. this.error = err.message;
  50. this.target.textEditor = true;
  51. }
  52. this.checkOtherSegments(this.segments.length - 1);
  53. }
  54. addFunctionParameter(func, value, index, shiftBack) {
  55. if (shiftBack) {
  56. index = Math.max(index - 1, 0);
  57. }
  58. func.params[index] = value;
  59. }
  60. parseTargeRecursive(astNode, func, index) {
  61. if (astNode === null) {
  62. return null;
  63. }
  64. switch (astNode.type) {
  65. case 'function':
  66. var innerFunc = gfunc.createFuncInstance(astNode.name, { withDefaultParams: false });
  67. _.each(astNode.params, (param, index) => {
  68. this.parseTargeRecursive(param, innerFunc, index);
  69. });
  70. innerFunc.updateText();
  71. this.functions.push(innerFunc);
  72. break;
  73. case 'series-ref':
  74. this.addFunctionParameter(func, astNode.value, index, this.segments.length > 0);
  75. break;
  76. case 'bool':
  77. case 'string':
  78. case 'number':
  79. if ((index-1) >= func.def.params.length) {
  80. throw { message: 'invalid number of parameters to method ' + func.def.name };
  81. }
  82. this.addFunctionParameter(func, astNode.value, index, true);
  83. break;
  84. case 'metric':
  85. if (this.segments.length > 0) {
  86. if (astNode.segments.length !== 1) {
  87. throw { message: 'Multiple metric params not supported, use text editor.' };
  88. }
  89. this.addFunctionParameter(func, astNode.segments[0].value, index, true);
  90. break;
  91. }
  92. this.segments = _.map(astNode.segments, segment => {
  93. return this.uiSegmentSrv.newSegment(segment);
  94. });
  95. }
  96. }
  97. getSegmentPathUpTo(index) {
  98. var arr = this.segments.slice(0, index);
  99. return _.reduce(arr, function(result, segment) {
  100. return result ? (result + "." + segment.value) : segment.value;
  101. }, "");
  102. }
  103. checkOtherSegments(fromIndex) {
  104. if (fromIndex === 0) {
  105. this.segments.push(this.uiSegmentSrv.newSelectMetric());
  106. return;
  107. }
  108. var path = this.getSegmentPathUpTo(fromIndex + 1);
  109. return this.datasource.metricFindQuery(path).then(segments => {
  110. if (segments.length === 0) {
  111. if (path !== '') {
  112. this.segments = this.segments.splice(0, fromIndex);
  113. this.segments.push(this.uiSegmentSrv.newSelectMetric());
  114. }
  115. } else if (segments[0].expandable) {
  116. if (this.segments.length === fromIndex) {
  117. this.segments.push(this.uiSegmentSrv.newSelectMetric());
  118. } else {
  119. return this.checkOtherSegments(fromIndex + 1);
  120. }
  121. }
  122. }).catch(err => {
  123. appEvents.emit('alert-error', ['Error', err]);
  124. });
  125. }
  126. setSegmentFocus(segmentIndex) {
  127. _.each(this.segments, (segment, index) => {
  128. segment.focus = segmentIndex === index;
  129. });
  130. }
  131. wrapFunction(target, func) {
  132. return func.render(target);
  133. }
  134. getAltSegments(index) {
  135. var query = index === 0 ? '*' : this.getSegmentPathUpTo(index) + '.*';
  136. return this.datasource.metricFindQuery(query).then(segments => {
  137. var altSegments = _.map(segments, segment => {
  138. return this.uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
  139. });
  140. if (altSegments.length === 0) { return altSegments; }
  141. // add template variables
  142. _.each(this.templateSrv.variables, variable => {
  143. altSegments.unshift(this.uiSegmentSrv.newSegment({
  144. type: 'template',
  145. value: '$' + variable.name,
  146. expandable: true,
  147. }));
  148. });
  149. // add wildcard option
  150. altSegments.unshift(this.uiSegmentSrv.newSegment('*'));
  151. return altSegments;
  152. }).catch(err => {
  153. appEvents.emit('alert-error', ['Error', err]);
  154. return [];
  155. });
  156. }
  157. segmentValueChanged(segment, segmentIndex) {
  158. this.error = null;
  159. if (this.functions.length > 0 && this.functions[0].def.fake) {
  160. this.functions = [];
  161. }
  162. if (segment.expandable) {
  163. return this.checkOtherSegments(segmentIndex + 1).then(() => {
  164. this.setSegmentFocus(segmentIndex + 1);
  165. this.targetChanged();
  166. });
  167. } else {
  168. this.segments = this.segments.splice(0, segmentIndex + 1);
  169. }
  170. this.setSegmentFocus(segmentIndex + 1);
  171. this.targetChanged();
  172. }
  173. targetTextChanged() {
  174. this.parseTarget();
  175. this.panelCtrl.refresh();
  176. }
  177. targetChanged() {
  178. if (this.error) {
  179. return;
  180. }
  181. var oldTarget = this.target.target;
  182. var target = this.getSegmentPathUpTo(this.segments.length);
  183. this.target.target = _.reduce(this.functions, this.wrapFunction, target);
  184. if (this.target.target !== oldTarget) {
  185. if (this.segments[this.segments.length - 1].value !== 'select metric') {
  186. this.panelCtrl.refresh();
  187. }
  188. }
  189. }
  190. removeFunction(func) {
  191. this.functions = _.without(this.functions, func);
  192. this.targetChanged();
  193. }
  194. addFunction(funcDef) {
  195. var newFunc = gfunc.createFuncInstance(funcDef, { withDefaultParams: true });
  196. newFunc.added = true;
  197. this.functions.push(newFunc);
  198. this.moveAliasFuncLast();
  199. this.smartlyHandleNewAliasByNode(newFunc);
  200. if (this.segments.length === 1 && this.segments[0].fake) {
  201. this.segments = [];
  202. }
  203. if (!newFunc.params.length && newFunc.added) {
  204. this.targetChanged();
  205. }
  206. }
  207. moveAliasFuncLast() {
  208. var aliasFunc = _.find(this.functions, function(func) {
  209. return func.def.name === 'alias' ||
  210. func.def.name === 'aliasByNode' ||
  211. func.def.name === 'aliasByMetric';
  212. });
  213. if (aliasFunc) {
  214. this.functions = _.without(this.functions, aliasFunc);
  215. this.functions.push(aliasFunc);
  216. }
  217. }
  218. smartlyHandleNewAliasByNode(func) {
  219. if (func.def.name !== 'aliasByNode') {
  220. return;
  221. }
  222. for (var i = 0; i < this.segments.length; i++) {
  223. if (this.segments[i].value.indexOf('*') >= 0) {
  224. func.params[0] = i;
  225. func.added = false;
  226. this.targetChanged();
  227. return;
  228. }
  229. }
  230. }
  231. }