graphite_query.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import _ from 'lodash';
  2. import gfunc from './gfunc';
  3. import {Parser} from './parser';
  4. export default class GraphiteQuery {
  5. target: any;
  6. functions: any[];
  7. segments: any[];
  8. tags: any[];
  9. error: any;
  10. seriesByTagUsed: boolean;
  11. checkOtherSegmentsIndex: number;
  12. removeTagValue: string;
  13. templateSrv: any;
  14. scopedVars: any;
  15. /** @ngInject */
  16. constructor(target, templateSrv?, scopedVars?) {
  17. this.target = target;
  18. this.parseTarget();
  19. this.removeTagValue = '-- remove tag --';
  20. }
  21. parseTarget() {
  22. this.functions = [];
  23. this.segments = [];
  24. this.tags = [];
  25. this.error = null;
  26. if (this.target.textEditor) {
  27. return;
  28. }
  29. var parser = new Parser(this.target.target);
  30. var astNode = parser.getAst();
  31. if (astNode === null) {
  32. this.checkOtherSegmentsIndex = 0;
  33. return;
  34. }
  35. if (astNode.type === 'error') {
  36. this.error = astNode.message + " at position: " + astNode.pos;
  37. this.target.textEditor = true;
  38. return;
  39. }
  40. try {
  41. this.parseTargetRecursive(astNode, null, 0);
  42. } catch (err) {
  43. console.log('error parsing target:', err.message);
  44. this.error = err.message;
  45. this.target.textEditor = true;
  46. }
  47. this.checkOtherSegmentsIndex = this.segments.length - 1;
  48. this.checkForSeriesByTag();
  49. }
  50. checkForSeriesByTag() {
  51. let seriesByTagFunc = _.find(this.functions, (func) => func.def.name === 'seriesByTag');
  52. if (seriesByTagFunc) {
  53. this.seriesByTagUsed = true;
  54. seriesByTagFunc.hidden = true;
  55. let tags = this.splitSeriesByTagParams(seriesByTagFunc);
  56. this.tags = tags;
  57. }
  58. }
  59. getSegmentPathUpTo(index) {
  60. var arr = this.segments.slice(0, index);
  61. return _.reduce(arr, function(result, segment) {
  62. return result ? (result + "." + segment.value) : segment.value;
  63. }, "");
  64. }
  65. parseTargetRecursive(astNode, func, index) {
  66. if (astNode === null) {
  67. return null;
  68. }
  69. switch (astNode.type) {
  70. case 'function':
  71. var innerFunc = gfunc.createFuncInstance(astNode.name, { withDefaultParams: false });
  72. _.each(astNode.params, (param, index) => {
  73. this.parseTargetRecursive(param, innerFunc, index);
  74. });
  75. innerFunc.updateText();
  76. this.functions.push(innerFunc);
  77. break;
  78. case 'series-ref':
  79. this.addFunctionParameter(func, astNode.value, index, this.segments.length > 0);
  80. break;
  81. case 'bool':
  82. case 'string':
  83. case 'number':
  84. if ((index-1) >= func.def.params.length) {
  85. throw { message: 'invalid number of parameters to method ' + func.def.name };
  86. }
  87. var shiftBack = this.isShiftParamsBack(func);
  88. this.addFunctionParameter(func, astNode.value, index, shiftBack);
  89. break;
  90. case 'metric':
  91. if (this.segments.length > 0) {
  92. if (astNode.segments.length !== 1) {
  93. throw { message: 'Multiple metric params not supported, use text editor.' };
  94. }
  95. this.addFunctionParameter(func, astNode.segments[0].value, index, true);
  96. break;
  97. }
  98. this.segments = astNode.segments;
  99. }
  100. }
  101. isShiftParamsBack(func) {
  102. return func.def.name !== 'seriesByTag';
  103. }
  104. updateSegmentValue(segment, index) {
  105. this.segments[index].value = segment.value;
  106. }
  107. addSelectMetricSegment() {
  108. this.segments.push({value: "select metric"});
  109. }
  110. addFunction(newFunc) {
  111. this.functions.push(newFunc);
  112. this.moveAliasFuncLast();
  113. }
  114. moveAliasFuncLast() {
  115. var aliasFunc = _.find(this.functions, function(func) {
  116. return func.def.name === 'alias' ||
  117. func.def.name === 'aliasByNode' ||
  118. func.def.name === 'aliasByMetric';
  119. });
  120. if (aliasFunc) {
  121. this.functions = _.without(this.functions, aliasFunc);
  122. this.functions.push(aliasFunc);
  123. }
  124. }
  125. addFunctionParameter(func, value, index, shiftBack) {
  126. if (shiftBack) {
  127. index = Math.max(index - 1, 0);
  128. }
  129. func.params[index] = value;
  130. }
  131. removeFunction(func) {
  132. this.functions = _.without(this.functions, func);
  133. }
  134. updateModelTarget(targets) {
  135. // render query
  136. if (!this.target.textEditor) {
  137. var metricPath = this.getSegmentPathUpTo(this.segments.length);
  138. this.target.target = _.reduce(this.functions, wrapFunction, metricPath);
  139. }
  140. this.updateRenderedTarget(this.target, targets);
  141. // loop through other queries and update targetFull as needed
  142. for (const target of targets || []) {
  143. if (target.refId !== this.target.refId) {
  144. this.updateRenderedTarget(target, targets);
  145. }
  146. }
  147. }
  148. updateRenderedTarget(target, targets) {
  149. // render nested query
  150. var targetsByRefId = _.keyBy(targets, 'refId');
  151. // no references to self
  152. delete targetsByRefId[target.refId];
  153. var nestedSeriesRefRegex = /\#([A-Z])/g;
  154. var targetWithNestedQueries = target.target;
  155. // Keep interpolating until there are no query references
  156. // The reason for the loop is that the referenced query might contain another reference to another query
  157. while (targetWithNestedQueries.match(nestedSeriesRefRegex)) {
  158. var updated = targetWithNestedQueries.replace(nestedSeriesRefRegex, (match, g1) => {
  159. var t = targetsByRefId[g1];
  160. if (!t) {
  161. return match;
  162. }
  163. // no circular references
  164. delete targetsByRefId[g1];
  165. return t.target;
  166. });
  167. if (updated === targetWithNestedQueries) {
  168. break;
  169. }
  170. targetWithNestedQueries = updated;
  171. }
  172. delete target.targetFull;
  173. if (target.target !== targetWithNestedQueries) {
  174. target.targetFull = targetWithNestedQueries;
  175. }
  176. }
  177. splitSeriesByTagParams(func) {
  178. const tagPattern = /([^\!=~]+)([\!=~]+)([^\!=~]+)/;
  179. return _.flatten(_.map(func.params, (param: string) => {
  180. let matches = tagPattern.exec(param);
  181. if (matches) {
  182. let tag = matches.slice(1);
  183. if (tag.length === 3) {
  184. return {
  185. key: tag[0],
  186. operator: tag[1],
  187. value: tag[2]
  188. };
  189. }
  190. }
  191. return [];
  192. }));
  193. }
  194. getSeriesByTagFuncIndex() {
  195. return _.findIndex(this.functions, (func) => func.def.name === 'seriesByTag');
  196. }
  197. getSeriesByTagFunc() {
  198. let seriesByTagFuncIndex = this.getSeriesByTagFuncIndex();
  199. if (seriesByTagFuncIndex >= 0) {
  200. return this.functions[seriesByTagFuncIndex];
  201. } else {
  202. return undefined;
  203. }
  204. }
  205. addTag(tag) {
  206. let newTagParam = renderTagString(tag);
  207. this.getSeriesByTagFunc().params.push(newTagParam);
  208. this.tags.push(tag);
  209. }
  210. removeTag(index) {
  211. this.getSeriesByTagFunc().params.splice(index, 1);
  212. this.tags.splice(index, 1);
  213. }
  214. updateTag(tag, tagIndex) {
  215. this.error = null;
  216. if (tag.key === this.removeTagValue) {
  217. this.removeTag(tagIndex);
  218. return;
  219. }
  220. let newTagParam = renderTagString(tag);
  221. this.getSeriesByTagFunc().params[tagIndex] = newTagParam;
  222. this.tags[tagIndex] = tag;
  223. }
  224. renderTagExpressions(excludeIndex = -1) {
  225. return _.compact(_.map(this.tags, (tagExpr, index) => {
  226. // Don't render tag that we want to lookup
  227. if (index !== excludeIndex) {
  228. return tagExpr.key + tagExpr.operator + tagExpr.value;
  229. }
  230. }));
  231. }
  232. }
  233. function wrapFunction(target, func) {
  234. return func.render(target);
  235. }
  236. function renderTagString(tag) {
  237. return tag.key + tag.operator + tag.value;
  238. }