graphite_query.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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);
  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) {
  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 => {
  73. this.parseTargetRecursive(param, innerFunc);
  74. });
  75. innerFunc.updateText();
  76. this.functions.push(innerFunc);
  77. break;
  78. case 'series-ref':
  79. if (this.segments.length > 0) {
  80. this.addFunctionParameter(func, astNode.value);
  81. } else {
  82. this.segments.push(astNode);
  83. }
  84. break;
  85. case 'bool':
  86. case 'string':
  87. case 'number':
  88. this.addFunctionParameter(func, astNode.value);
  89. break;
  90. case 'metric':
  91. if (this.segments.length > 0) {
  92. this.addFunctionParameter(func, _.join(_.map(astNode.segments, 'value'), '.'));
  93. } else {
  94. this.segments = astNode.segments;
  95. }
  96. break;
  97. }
  98. }
  99. updateSegmentValue(segment, index) {
  100. this.segments[index].value = segment.value;
  101. }
  102. addSelectMetricSegment() {
  103. this.segments.push({value: "select metric"});
  104. }
  105. addFunction(newFunc) {
  106. this.functions.push(newFunc);
  107. this.moveAliasFuncLast();
  108. }
  109. moveAliasFuncLast() {
  110. var aliasFunc = _.find(this.functions, function(func) {
  111. return func.def.name === 'alias' ||
  112. func.def.name === 'aliasByNode' ||
  113. func.def.name === 'aliasByMetric';
  114. });
  115. if (aliasFunc) {
  116. this.functions = _.without(this.functions, aliasFunc);
  117. this.functions.push(aliasFunc);
  118. }
  119. }
  120. addFunctionParameter(func, value) {
  121. if (func.params.length >= func.def.params.length) {
  122. throw { message: 'too many parameters for function ' + func.def.name };
  123. }
  124. func.params.push(value);
  125. }
  126. removeFunction(func) {
  127. this.functions = _.without(this.functions, func);
  128. }
  129. updateModelTarget(targets) {
  130. // render query
  131. if (!this.target.textEditor) {
  132. var metricPath = this.getSegmentPathUpTo(this.segments.length).replace(/\.select metric$/, '');
  133. this.target.target = _.reduce(this.functions, wrapFunction, metricPath);
  134. }
  135. this.updateRenderedTarget(this.target, targets);
  136. // loop through other queries and update targetFull as needed
  137. for (const target of targets || []) {
  138. if (target.refId !== this.target.refId) {
  139. this.updateRenderedTarget(target, targets);
  140. }
  141. }
  142. }
  143. updateRenderedTarget(target, targets) {
  144. // render nested query
  145. var targetsByRefId = _.keyBy(targets, 'refId');
  146. // no references to self
  147. delete targetsByRefId[target.refId];
  148. var nestedSeriesRefRegex = /\#([A-Z])/g;
  149. var targetWithNestedQueries = target.target;
  150. // Keep interpolating until there are no query references
  151. // The reason for the loop is that the referenced query might contain another reference to another query
  152. while (targetWithNestedQueries.match(nestedSeriesRefRegex)) {
  153. var updated = targetWithNestedQueries.replace(nestedSeriesRefRegex, (match, g1) => {
  154. var t = targetsByRefId[g1];
  155. if (!t) {
  156. return match;
  157. }
  158. // no circular references
  159. delete targetsByRefId[g1];
  160. return t.target;
  161. });
  162. if (updated === targetWithNestedQueries) {
  163. break;
  164. }
  165. targetWithNestedQueries = updated;
  166. }
  167. delete target.targetFull;
  168. if (target.target !== targetWithNestedQueries) {
  169. target.targetFull = targetWithNestedQueries;
  170. }
  171. }
  172. splitSeriesByTagParams(func) {
  173. const tagPattern = /([^\!=~]+)([\!=~]+)([^\!=~]+)/;
  174. return _.flatten(_.map(func.params, (param: string) => {
  175. let matches = tagPattern.exec(param);
  176. if (matches) {
  177. let tag = matches.slice(1);
  178. if (tag.length === 3) {
  179. return {
  180. key: tag[0],
  181. operator: tag[1],
  182. value: tag[2]
  183. };
  184. }
  185. }
  186. return [];
  187. }));
  188. }
  189. getSeriesByTagFuncIndex() {
  190. return _.findIndex(this.functions, (func) => func.def.name === 'seriesByTag');
  191. }
  192. getSeriesByTagFunc() {
  193. let seriesByTagFuncIndex = this.getSeriesByTagFuncIndex();
  194. if (seriesByTagFuncIndex >= 0) {
  195. return this.functions[seriesByTagFuncIndex];
  196. } else {
  197. return undefined;
  198. }
  199. }
  200. addTag(tag) {
  201. let newTagParam = renderTagString(tag);
  202. this.getSeriesByTagFunc().params.push(newTagParam);
  203. this.tags.push(tag);
  204. }
  205. removeTag(index) {
  206. this.getSeriesByTagFunc().params.splice(index, 1);
  207. this.tags.splice(index, 1);
  208. }
  209. updateTag(tag, tagIndex) {
  210. this.error = null;
  211. if (tag.key === this.removeTagValue) {
  212. this.removeTag(tagIndex);
  213. return;
  214. }
  215. let newTagParam = renderTagString(tag);
  216. this.getSeriesByTagFunc().params[tagIndex] = newTagParam;
  217. this.tags[tagIndex] = tag;
  218. }
  219. renderTagExpressions(excludeIndex = -1) {
  220. return _.compact(_.map(this.tags, (tagExpr, index) => {
  221. // Don't render tag that we want to lookup
  222. if (index !== excludeIndex) {
  223. return tagExpr.key + tagExpr.operator + tagExpr.value;
  224. }
  225. }));
  226. }
  227. }
  228. function wrapFunction(target, func) {
  229. return func.render(target);
  230. }
  231. function renderTagString(tag) {
  232. return tag.key + tag.operator + tag.value;
  233. }