binaryScalarOperations.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { defaultAddOperationHandler } from '../../prometheus/querybuilder/shared/operationUtils';
  2. import {
  3. QueryBuilderOperation,
  4. QueryBuilderOperationDef,
  5. QueryBuilderOperationParamDef,
  6. } from '../../prometheus/querybuilder/shared/types';
  7. import { LokiOperationId, LokiVisualQueryOperationCategory } from './types';
  8. export const binaryScalarDefs = [
  9. {
  10. id: LokiOperationId.Addition,
  11. name: 'Add scalar',
  12. sign: '+',
  13. },
  14. {
  15. id: LokiOperationId.Subtraction,
  16. name: 'Subtract scalar',
  17. sign: '-',
  18. },
  19. {
  20. id: LokiOperationId.MultiplyBy,
  21. name: 'Multiply by scalar',
  22. sign: '*',
  23. },
  24. {
  25. id: LokiOperationId.DivideBy,
  26. name: 'Divide by scalar',
  27. sign: '/',
  28. },
  29. {
  30. id: LokiOperationId.Modulo,
  31. name: 'Modulo by scalar',
  32. sign: '%',
  33. },
  34. {
  35. id: LokiOperationId.Exponent,
  36. name: 'Exponent',
  37. sign: '^',
  38. },
  39. {
  40. id: LokiOperationId.EqualTo,
  41. name: 'Equal to',
  42. sign: '==',
  43. comparison: true,
  44. },
  45. {
  46. id: LokiOperationId.NotEqualTo,
  47. name: 'Not equal to',
  48. sign: '!=',
  49. comparison: true,
  50. },
  51. {
  52. id: LokiOperationId.GreaterThan,
  53. name: 'Greater than',
  54. sign: '>',
  55. comparison: true,
  56. },
  57. {
  58. id: LokiOperationId.LessThan,
  59. name: 'Less than',
  60. sign: '<',
  61. comparison: true,
  62. },
  63. {
  64. id: LokiOperationId.GreaterOrEqual,
  65. name: 'Greater or equal to',
  66. sign: '>=',
  67. comparison: true,
  68. },
  69. {
  70. id: LokiOperationId.LessOrEqual,
  71. name: 'Less or equal to',
  72. sign: '<=',
  73. comparison: true,
  74. },
  75. ];
  76. // Not sure about this one. It could also be a more generic 'Simple math operation' where user specifies
  77. // both the operator and the operand in a single input
  78. export const binaryScalarOperations: QueryBuilderOperationDef[] = binaryScalarDefs.map((opDef) => {
  79. const params: QueryBuilderOperationParamDef[] = [{ name: 'Value', type: 'number' }];
  80. const defaultParams: any[] = [2];
  81. if (opDef.comparison) {
  82. params.unshift({
  83. name: 'Bool',
  84. type: 'boolean',
  85. description: 'If checked comparison will return 0 or 1 for the value rather than filtering.',
  86. });
  87. defaultParams.unshift(false);
  88. }
  89. return {
  90. id: opDef.id,
  91. name: opDef.name,
  92. params,
  93. defaultParams,
  94. alternativesKey: 'binary scalar operations',
  95. category: LokiVisualQueryOperationCategory.BinaryOps,
  96. renderer: getSimpleBinaryRenderer(opDef.sign),
  97. addOperationHandler: defaultAddOperationHandler,
  98. };
  99. });
  100. function getSimpleBinaryRenderer(operator: string) {
  101. return function binaryRenderer(model: QueryBuilderOperation, def: QueryBuilderOperationDef, innerExpr: string) {
  102. let param = model.params[0];
  103. let bool = '';
  104. if (model.params.length === 2) {
  105. param = model.params[1];
  106. bool = model.params[0] ? ' bool' : '';
  107. }
  108. return `${innerExpr} ${operator}${bool} ${param}`;
  109. };
  110. }