prom_query.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import {
  2. QueryPartDef,
  3. QueryPart,
  4. functionRenderer,
  5. identityRenderer,
  6. quotedIdentityRenderer,
  7. } from 'app/core/components/query_part/query_part';
  8. import _ from 'lodash';
  9. var index = [];
  10. var categories = {
  11. Functions: [],
  12. GroupBy: [],
  13. };
  14. export class PromQuery {
  15. target: any;
  16. metric: string;
  17. range: string;
  18. filters: any[];
  19. functions: any[];
  20. templateSrv: any;
  21. scopedVars: any;
  22. constructor(target, templateSrv?, scopedVars?) {
  23. this.target = target;
  24. this.target.expr = this.target.expr || '';
  25. this.target.intervalFactor = this.target.intervalFactor || 2;
  26. this.target.functions = this.target.functions || [];
  27. this.target.editorMode = this.target.editorMode || true;
  28. this.templateSrv = templateSrv;
  29. this.scopedVars = scopedVars;
  30. this.updateProjection();
  31. }
  32. updateProjection() {
  33. this.functions = _.map(this.target.functions, function(func: any) {
  34. return createPart(func);
  35. });
  36. }
  37. render() {
  38. var query = this.target.metric;
  39. if (this.target.range) {
  40. query += '[' + this.target.range + ']';
  41. }
  42. for (let func of this.functions) {
  43. query = func.render(query);
  44. }
  45. return query;
  46. }
  47. addQueryPart(category, item) {
  48. var partModel = createPart({type: item.text});
  49. partModel.def.addStrategy(this, partModel);
  50. }
  51. }
  52. export function createPart(part): any {
  53. var def = index[part.type];
  54. if (!def) {
  55. throw {message: 'Could not find query part ' + part.type};
  56. }
  57. return new QueryPart(part, def);
  58. }
  59. function register(options: any) {
  60. index[options.type] = new QueryPartDef(options);
  61. options.category.push(index[options.type]);
  62. }
  63. function addFunctionStrategy(model, partModel) {
  64. model.functions.push(partModel);
  65. model.target.functions.push(partModel.part);
  66. }
  67. function groupByLabelRenderer(part, innerExpr) {
  68. return innerExpr + ' by(' + part.params.join(',') + ')';
  69. }
  70. register({
  71. type: 'rate',
  72. addStrategy: addFunctionStrategy,
  73. category: categories.Functions,
  74. params: [],
  75. defaultParams: [],
  76. renderer: functionRenderer,
  77. });
  78. register({
  79. type: 'sum',
  80. addStrategy: addFunctionStrategy,
  81. category: categories.Functions,
  82. params: [],
  83. defaultParams: [],
  84. renderer: functionRenderer,
  85. });
  86. register({
  87. type: 'by',
  88. addStrategy: addFunctionStrategy,
  89. category: categories.Functions,
  90. params: [
  91. {name: "label", type: "string", dynamicLookup: true}
  92. ],
  93. defaultParams: [],
  94. renderer: groupByLabelRenderer,
  95. });
  96. export function getQueryPartCategories() {
  97. return categories;
  98. }