graphite_query.ts 7.2 KB

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