prom_query.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import {
  2. QueryPartDef,
  3. QueryPart,
  4. functionRenderer,
  5. suffixRenderer,
  6. identityRenderer,
  7. quotedIdentityRenderer,
  8. } from 'app/core/components/query_part/query_part';
  9. import _ from 'lodash';
  10. var index = [];
  11. var categories = {
  12. Functions: [],
  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.templateSrv = templateSrv;
  28. this.scopedVars = scopedVars;
  29. this.updateProjection();
  30. }
  31. updateProjection() {
  32. this.functions = _.map(this.target.functions, function(func: any) {
  33. return createPart(func);
  34. });
  35. }
  36. render() {
  37. var query = this.target.metric;
  38. if (this.target.range) {
  39. query += '[' + this.target.range + ']';
  40. }
  41. for (let func of this.functions) {
  42. query = func.render(query);
  43. }
  44. return query;
  45. }
  46. addQueryPart(category, item) {
  47. var partModel = createPart({type: item.text});
  48. partModel.def.addStrategy(this, partModel);
  49. }
  50. }
  51. export function createPart(part): any {
  52. var def = index[part.type];
  53. if (!def) {
  54. throw {message: 'Could not find query part ' + part.type};
  55. }
  56. return new QueryPart(part, def);
  57. }
  58. function register(options: any) {
  59. index[options.type] = new QueryPartDef(options);
  60. options.category.push(index[options.type]);
  61. }
  62. function addFunctionStrategy(model, partModel) {
  63. model.functions.push(partModel);
  64. model.target.functions.push(partModel.part);
  65. }
  66. register({
  67. type: 'rate',
  68. addStrategy: addFunctionStrategy,
  69. category: categories.Functions,
  70. params: [],
  71. defaultParams: [],
  72. renderer: functionRenderer,
  73. });
  74. export function getQueryPartCategories() {
  75. return categories;
  76. }