graphite_query.ts 7.3 KB

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