query_ctrl.ts 7.3 KB

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