2022-04-22 08:33:13 -05:00
|
|
|
import { defaultAddOperationHandler } from './shared/operationUtils';
|
2022-03-14 11:36:25 -05:00
|
|
|
import { QueryBuilderOperation, QueryBuilderOperationDef, QueryBuilderOperationParamDef } from './shared/types';
|
2022-03-04 08:04:09 -06:00
|
|
|
import { PromOperationId, PromVisualQueryOperationCategory } from './types';
|
|
|
|
|
|
|
|
export const binaryScalarDefs = [
|
|
|
|
{
|
|
|
|
id: PromOperationId.Addition,
|
|
|
|
name: 'Add scalar',
|
|
|
|
sign: '+',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: PromOperationId.Subtraction,
|
|
|
|
name: 'Subtract scalar',
|
|
|
|
sign: '-',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: PromOperationId.MultiplyBy,
|
|
|
|
name: 'Multiply by scalar',
|
|
|
|
sign: '*',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: PromOperationId.DivideBy,
|
|
|
|
name: 'Divide by scalar',
|
|
|
|
sign: '/',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: PromOperationId.Modulo,
|
|
|
|
name: 'Modulo by scalar',
|
|
|
|
sign: '%',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: PromOperationId.Exponent,
|
|
|
|
name: 'Exponent',
|
|
|
|
sign: '^',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: PromOperationId.EqualTo,
|
|
|
|
name: 'Equal to',
|
|
|
|
sign: '==',
|
2022-03-14 11:36:25 -05:00
|
|
|
comparison: true,
|
2022-03-04 08:04:09 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: PromOperationId.NotEqualTo,
|
|
|
|
name: 'Not equal to',
|
|
|
|
sign: '!=',
|
2022-03-14 11:36:25 -05:00
|
|
|
comparison: true,
|
2022-03-04 08:04:09 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: PromOperationId.GreaterThan,
|
|
|
|
name: 'Greater than',
|
|
|
|
sign: '>',
|
2022-03-14 11:36:25 -05:00
|
|
|
comparison: true,
|
2022-03-04 08:04:09 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: PromOperationId.LessThan,
|
|
|
|
name: 'Less than',
|
|
|
|
sign: '<',
|
2022-03-14 11:36:25 -05:00
|
|
|
comparison: true,
|
2022-03-04 08:04:09 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: PromOperationId.GreaterOrEqual,
|
|
|
|
name: 'Greater or equal to',
|
|
|
|
sign: '>=',
|
2022-03-14 11:36:25 -05:00
|
|
|
comparison: true,
|
2022-03-04 08:04:09 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: PromOperationId.LessOrEqual,
|
|
|
|
name: 'Less or equal to',
|
|
|
|
sign: '<=',
|
2022-03-14 11:36:25 -05:00
|
|
|
comparison: true,
|
2022-03-04 08:04:09 -06:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-04-22 08:33:13 -05:00
|
|
|
export const binaryScalarOperatorToOperatorName = binaryScalarDefs.reduce((acc, def) => {
|
|
|
|
acc[def.sign] = {
|
|
|
|
id: def.id,
|
|
|
|
comparison: def.comparison,
|
|
|
|
};
|
|
|
|
return acc;
|
|
|
|
}, {} as Record<string, { id: string; comparison?: boolean }>);
|
|
|
|
|
2022-03-04 08:04:09 -06:00
|
|
|
// Not sure about this one. It could also be a more generic 'Simple math operation' where user specifies
|
|
|
|
// both the operator and the operand in a single input
|
2022-03-14 11:36:25 -05:00
|
|
|
export const binaryScalarOperations: QueryBuilderOperationDef[] = binaryScalarDefs.map((opDef) => {
|
|
|
|
const params: QueryBuilderOperationParamDef[] = [{ name: 'Value', type: 'number' }];
|
|
|
|
const defaultParams: any[] = [2];
|
|
|
|
if (opDef.comparison) {
|
2022-04-01 10:51:50 -05:00
|
|
|
params.push({
|
2022-03-17 04:51:42 -05:00
|
|
|
name: 'Bool',
|
|
|
|
type: 'boolean',
|
|
|
|
description: 'If checked comparison will return 0 or 1 for the value rather than filtering.',
|
|
|
|
});
|
2022-04-01 10:51:50 -05:00
|
|
|
defaultParams.push(false);
|
2022-03-14 11:36:25 -05:00
|
|
|
}
|
|
|
|
|
2022-03-04 08:04:09 -06:00
|
|
|
return {
|
|
|
|
id: opDef.id,
|
|
|
|
name: opDef.name,
|
2022-03-14 11:36:25 -05:00
|
|
|
params,
|
|
|
|
defaultParams,
|
2022-03-04 08:04:09 -06:00
|
|
|
alternativesKey: 'binary scalar operations',
|
|
|
|
category: PromVisualQueryOperationCategory.BinaryOps,
|
|
|
|
renderer: getSimpleBinaryRenderer(opDef.sign),
|
|
|
|
addOperationHandler: defaultAddOperationHandler,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
function getSimpleBinaryRenderer(operator: string) {
|
|
|
|
return function binaryRenderer(model: QueryBuilderOperation, def: QueryBuilderOperationDef, innerExpr: string) {
|
2022-03-14 11:36:25 -05:00
|
|
|
let param = model.params[0];
|
|
|
|
let bool = '';
|
|
|
|
if (model.params.length === 2) {
|
2022-04-01 10:51:50 -05:00
|
|
|
bool = model.params[1] ? ' bool' : '';
|
2022-03-14 11:36:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return `${innerExpr} ${operator}${bool} ${param}`;
|
2022-03-04 08:04:09 -06:00
|
|
|
};
|
|
|
|
}
|