query_ctrl.ts 7.2 KB

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