-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bal 2254 rule engine v2 #2470
bal 2254 rule engine v2 #2470
Conversation
|
WalkthroughThe changes enhance the rule engine within the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (13)
- services/workflows-service/src/app.module.ts (2 hunks)
- services/workflows-service/src/rule-engine/core/errors.ts (1 hunks)
- services/workflows-service/src/rule-engine/core/operators/constants.ts (1 hunks)
- services/workflows-service/src/rule-engine/core/operators/enums.ts (1 hunks)
- services/workflows-service/src/rule-engine/core/operators/helpers.ts (1 hunks)
- services/workflows-service/src/rule-engine/core/operators/schemas.ts (1 hunks)
- services/workflows-service/src/rule-engine/core/operators/types.ts (1 hunks)
- services/workflows-service/src/rule-engine/core/rule-engine.ts (1 hunks)
- services/workflows-service/src/rule-engine/core/rule-engine.unit.test.ts (1 hunks)
- services/workflows-service/src/rule-engine/core/types.ts (1 hunks)
- services/workflows-service/src/rule-engine/rule-engine.module.ts (1 hunks)
- services/workflows-service/src/rule-engine/rule-engine.service.ts (1 hunks)
- services/workflows-service/src/rule-engine/rule-store.service.ts (1 hunks)
Files skipped from review due to trivial changes (2)
- services/workflows-service/src/rule-engine/core/operators/enums.ts
- services/workflows-service/src/rule-engine/rule-engine.module.ts
Additional comments not posted (12)
services/workflows-service/src/rule-engine/core/operators/types.ts (1)
1-16
: Well-defined types for rule engine operations.The type definitions in this file are crucial for ensuring type safety and clarity across the rule engine functionality. The use of generic types in
ConditionFn
is particularly commendable as it provides flexibility while maintaining type safety.services/workflows-service/src/rule-engine/core/operators/constants.ts (1)
1-13
: Effective mapping of operations to helper functions.This mapping ensures that each operation can be easily associated with its corresponding logic, which is fundamental for the maintainability and scalability of the rule engine. The use of
as const
ensures that the object is treated as a constant, preventing unintended modifications.services/workflows-service/src/rule-engine/rule-engine.service.ts (1)
1-20
: Well-structured method for running rule engine operations.The
run
method is well-implemented, with clear steps for retrieving rules, creating a rule engine instance, and executing it. This structure enhances readability and maintainability.services/workflows-service/src/rule-engine/core/types.ts (1)
1-36
: Comprehensive type definitions enhancing the rule engine's robustness.The extended type definitions for operations, operators, and results are crucial for ensuring the rule engine's functionality is robust and error-resistant. The clear distinction between
PassedRuleResult
andFailedRuleResult
is particularly beneficial for handling different outcomes explicitly.services/workflows-service/src/rule-engine/core/errors.ts (4)
3-8
: Ensure consistent error naming convention.The
OperationNotFoundError
class explicitly sets its name usingthis.name = OperationNotFoundError.name;
. This is a good practice as it ensures the error's name property matches the class name, which can be helpful for debugging and logging.
10-15
: Consistent error handling approach.The
DataValueNotFoundError
follows the same pattern asOperationNotFoundError
, setting its name explicitly. This consistency in error handling across different classes is good for maintainability.
24-41
: Detailed error information inValidationFailedError
.The constructor of
ValidationFailedError
optionally accepts aZodError
, and if present, it formats and sets detailed error information. This is a robust design choice as it leverages the structured error information from Zod to provide more detailed feedback.
43-48
: Type alias for error handling.
EngineErrors
type alias is used to define a union of the custom errors and the genericError
. This is useful for type-checking and ensuring that error handling in the application can accommodate both specific and unexpected errors.services/workflows-service/src/rule-engine/core/rule-engine.ts (1)
13-38
: Robust rule validation logic invalidateRule
.The
validateRule
function comprehensively checks for various error conditions (missing key, invalid data value, unknown operation) and throws the appropriate custom errors. This ensures that rules are thoroughly validated before execution, contributing to the reliability of the rule engine.services/workflows-service/src/rule-engine/core/rule-engine.unit.test.ts (1)
12-150
: Comprehensive test coverage inrule-engine.unit.test.ts
.The unit tests cover a variety of scenarios including simple rule validation, handling missing keys, unknown operations, and incorrect values. The use of descriptive test names and the structured arrangement of tests enhance readability and maintainability.
services/workflows-service/src/app.module.ts (1)
124-124
: Proper integration ofRuleEngineModule
.The
RuleEngineModule
has been correctly added to the imports array inAppModule
. This ensures that the rule engine's functionality is available application-wide, aligning with the modular architecture of NestJS.services/workflows-service/src/rule-engine/core/operators/helpers.ts (1)
7-197
: Well-structured operator classes inhelpers.ts
.The operator classes (
Equals
,GreaterThan
,LessThan
, etc.) are well-defined and extend theBaseOperator
abstract class, which handles common validation logic. This structure promotes code reuse and maintainability. The explicit handling of validation using Zod schemas in each operator class enhances the robustness of the rule validation process.
import { Injectable } from '@nestjs/common'; | ||
import { RuleSet } from './core/types'; | ||
|
||
export type RuleStoreServiceFindAllOptions = { | ||
databaseId: string; | ||
source: 'notion'; | ||
}; | ||
|
||
@Injectable() | ||
export class RuleStoreService { | ||
public async findAll({ databaseId, source }: RuleStoreServiceFindAllOptions): Promise<RuleSet> { | ||
throw new Error('Unsupported source'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider implementing support for multiple sources.
Currently, the findAll
method throws an error for any source. This might limit the flexibility and usability of the rule engine. Consider implementing actual retrieval logic or supporting multiple sources.
import { z } from 'zod'; | ||
import { OPERATION } from './enums'; | ||
|
||
export const PrimitiveSchema = z.union([z.number(), z.string(), z.boolean()]); | ||
|
||
export const BetweenSchema = z.object({ | ||
min: PrimitiveSchema, | ||
max: PrimitiveSchema, | ||
}); | ||
|
||
export const LastYearsSchema = z.object({ | ||
years: z.number().positive(), | ||
}); | ||
|
||
// TODO: TBD | ||
const ruleSchema = z.object({ | ||
key: z.string(), | ||
operation: z.literal(OPERATION.LAST_YEAR), | ||
value: z.object({ | ||
years: z.number(), | ||
}), | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure completion of the ruleSchema definition.
The TODO: TBD
comment above the ruleSchema
suggests that this part of the schema is not yet finalized. It's crucial to complete this to ensure the rule engine can validate and operate correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (7)
- services/workflows-service/src/rule-engine/core/errors.ts (1 hunks)
- services/workflows-service/src/rule-engine/core/operators/constants.ts (1 hunks)
- services/workflows-service/src/rule-engine/core/operators/helpers.ts (1 hunks)
- services/workflows-service/src/rule-engine/core/rule-engine.ts (1 hunks)
- services/workflows-service/src/rule-engine/core/rule-engine.unit.test.ts (1 hunks)
- services/workflows-service/src/rule-engine/core/types.ts (1 hunks)
- services/workflows-service/src/rule-engine/rule-engine.service.ts (1 hunks)
Files skipped from review as they are similar to previous changes (6)
- services/workflows-service/src/rule-engine/core/errors.ts
- services/workflows-service/src/rule-engine/core/operators/constants.ts
- services/workflows-service/src/rule-engine/core/operators/helpers.ts
- services/workflows-service/src/rule-engine/core/rule-engine.unit.test.ts
- services/workflows-service/src/rule-engine/core/types.ts
- services/workflows-service/src/rule-engine/rule-engine.service.ts
Additional context used
Biome
services/workflows-service/src/rule-engine/core/rule-engine.ts
[error] 54-56: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)
Unsafe fix: Omit the else clause.
Additional comments not posted (5)
services/workflows-service/src/rule-engine/core/rule-engine.ts (5)
7-35
: Review ofvalidateRule
function.The function's logic and error handling are well implemented. The use of lodash for checking emptiness and retrieving values is appropriate, and the structured error handling ensures that all possible errors are accounted for.
45-45
: Clarify error handling strategy.The TODO comment raises a valid concern about whether to throw an error when encountering an
OperationNotFoundError
. Consider standardizing the error handling across different cases for consistency.
38-76
: Optimize error handling inrunRuleSet
.The function effectively handles both individual rules and nested rule sets. However, simplifying the error handling could improve readability.
Tools
Biome
[error] 54-56: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)
Unsafe fix: Omit the else clause.
79-79
: Clarify the helper injection strategy.The TODO comment suggests a need to finalize how helpers are injected. Clarify or implement this part to ensure extendibility.
78-89
: Review ofRuleEngine
function.The function setup is clear and allows for flexible injection of helpers, which could be beneficial for testing and extending functionality.
} else { | ||
throw error; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unnecessary else clause.
Following the static analysis suggestion, the else clause can be omitted for cleaner and more efficient code.
- } else {
- throw error;
- }
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
} else { | |
throw error; | |
} | |
} | |
throw error; |
Tools
Biome
[error] 54-56: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)
Unsafe fix: Omit the else clause.
[OPERATION.GTE]: GTE, | ||
[OPERATION.LT]: LT, | ||
[OPERATION.LTE]: LTE, | ||
[OPERATION.LAST_YEAR]: LAST_YEAR, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is last year a a part of helper operations?
LT: 'LT', | ||
GTE: 'GTE', | ||
LTE: 'LTE', | ||
LAST_YEAR: 'LAST_YEAR', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should not be here
}; | ||
} | ||
|
||
class Between extends BaseOperator<BetweenParams> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
between should be by 2 operators, before x and after y.
this.lte = new LessThanOrEqual(); | ||
} | ||
|
||
eval: ConditionFn<BetweenParams> = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eval is the functionality of one of the conversions of the values.
evaluate is a rule valuation function
expect(result[0]).toMatchObject({ | ||
error: expect.any(MissingKeyError), | ||
message: 'Rule is missing the key field', | ||
passed: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove
Summary by CodeRabbit
New Features
Tests