aggregations.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { addOperationWithRangeVector } from './operations';
  2. import {
  3. createAggregationOperation,
  4. createAggregationOperationWithParam,
  5. getPromAndLokiOperationDisplayName,
  6. getRangeVectorParamDef,
  7. } from './shared/operationUtils';
  8. import { QueryBuilderOperation, QueryBuilderOperationDef } from './shared/types';
  9. import { PromVisualQueryOperationCategory, PromOperationId } from './types';
  10. export function getAggregationOperations(): QueryBuilderOperationDef[] {
  11. return [
  12. ...createAggregationOperation(PromOperationId.Sum),
  13. ...createAggregationOperation(PromOperationId.Avg),
  14. ...createAggregationOperation(PromOperationId.Min),
  15. ...createAggregationOperation(PromOperationId.Max),
  16. ...createAggregationOperation(PromOperationId.Count),
  17. ...createAggregationOperationWithParam(PromOperationId.TopK, {
  18. params: [{ name: 'K-value', type: 'number' }],
  19. defaultParams: [5],
  20. }),
  21. ...createAggregationOperationWithParam(PromOperationId.BottomK, {
  22. params: [{ name: 'K-value', type: 'number' }],
  23. defaultParams: [5],
  24. }),
  25. ...createAggregationOperationWithParam(PromOperationId.CountValues, {
  26. params: [{ name: 'Identifier', type: 'string' }],
  27. defaultParams: ['count'],
  28. }),
  29. createAggregationOverTime(PromOperationId.SumOverTime),
  30. createAggregationOverTime(PromOperationId.AvgOverTime),
  31. createAggregationOverTime(PromOperationId.MinOverTime),
  32. createAggregationOverTime(PromOperationId.MaxOverTime),
  33. createAggregationOverTime(PromOperationId.CountOverTime),
  34. createAggregationOverTime(PromOperationId.LastOverTime),
  35. createAggregationOverTime(PromOperationId.PresentOverTime),
  36. createAggregationOverTime(PromOperationId.AbsentOverTime),
  37. createAggregationOverTime(PromOperationId.StddevOverTime),
  38. ];
  39. }
  40. function createAggregationOverTime(name: string): QueryBuilderOperationDef {
  41. return {
  42. id: name,
  43. name: getPromAndLokiOperationDisplayName(name),
  44. params: [getRangeVectorParamDef()],
  45. defaultParams: ['$__interval'],
  46. alternativesKey: 'overtime function',
  47. category: PromVisualQueryOperationCategory.RangeFunctions,
  48. renderer: operationWithRangeVectorRenderer,
  49. addOperationHandler: addOperationWithRangeVector,
  50. };
  51. }
  52. function operationWithRangeVectorRenderer(
  53. model: QueryBuilderOperation,
  54. def: QueryBuilderOperationDef,
  55. innerExpr: string
  56. ) {
  57. let rangeVector = (model.params ?? [])[0] ?? '$__interval';
  58. return `${def.id}(${innerExpr}[${rangeVector}])`;
  59. }