From 4cf7d200575b6a4bf4959c1e92619c23873f8db7 Mon Sep 17 00:00:00 2001 From: Ailin Yu Date: Mon, 9 Dec 2024 17:44:22 -0800 Subject: [PATCH] fix: Add actions to rules --- python/examples/asset_agnostic_rules/main.py | 11 +++++------ .../rule_modules/voltage.yml | 4 ++-- python/lib/sift_py/rule/service.py | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/python/examples/asset_agnostic_rules/main.py b/python/examples/asset_agnostic_rules/main.py index 1fae7e3d..b08eeedd 100644 --- a/python/examples/asset_agnostic_rules/main.py +++ b/python/examples/asset_agnostic_rules/main.py @@ -54,17 +54,16 @@ velocity_rules_src, ], sub_expressions=[ - SubExpression("voltage.overvoltage", {"$1": 75}), - SubExpression("voltage.undervoltage", {"$1": 30}), - SubExpression("velocity.vehicle_not_stopped", {"$2": 10}), + SubExpression("voltage.overvoltage", {"overvoltage_threshold": 75}), + SubExpression("voltage.undervoltage", {"undervoltage_threshold": 30}), ], channel_references=[ - RuleChannelReference("overvoltage", {"$2": "vehicle_state"}), - RuleChannelReference("undervoltage", {"$2": "vehicle_state"}), + RuleChannelReference("overvoltage", {"$1": "mainmotor.voltage", "$2": "vehicle_state"}), + RuleChannelReference("undervoltage", {"$1": "mainmotor.voltage", "$2": "vehicle_state"}), RuleChannelReference( "vehicle_stuck", {"$1": "vehicle_state", "$2": "mainmotor.velocity"} ), - RuleChannelReference("vehicle_not_stopped", {"$1": "vehicle_state"}), + RuleChannelReference("vehicle_not_stopped", {"$1": "vehicle_state", "$2": "mainmotor.velocity"}), ], ) diff --git a/python/examples/asset_agnostic_rules/rule_modules/voltage.yml b/python/examples/asset_agnostic_rules/rule_modules/voltage.yml index eb225c5e..d4999501 100644 --- a/python/examples/asset_agnostic_rules/rule_modules/voltage.yml +++ b/python/examples/asset_agnostic_rules/rule_modules/voltage.yml @@ -4,7 +4,7 @@ rules: - name: overvoltage rule_client_key: overvoltage-rule description: Checks for overvoltage while accelerating - expression: $2 == "Accelerating" && voltage > $1 + expression: $2 == "Accelerating" && $1 > overvoltage_threshold type: review assignee: benjamin@siftstack.com asset_names: @@ -13,7 +13,7 @@ rules: - name: undervoltage rule_client_key: undervoltage-rule description: Checks for undervoltage while accelerating - expression: $2 == "Accelerating" && voltage < $1 + expression: $2 == "Accelerating" && $1 < undervoltage_threshold type: review assignee: benjamin@siftstack.com asset_names: diff --git a/python/lib/sift_py/rule/service.py b/python/lib/sift_py/rule/service.py index b9c7b659..f5e11d9e 100644 --- a/python/lib/sift_py/rule/service.py +++ b/python/lib/sift_py/rule/service.py @@ -37,6 +37,8 @@ RuleActionKind, RuleConfig, ) +from sift_py.rule.config import RuleAction +from sift_py.yaml.rule import RuleActionAnnotationKind class RuleService: @@ -139,11 +141,25 @@ def parse_channel_refs(channel_ref: Dict[str, Any]): rule_fqn = f"{namespace}.{rule_name}" rule_subexpr = interpolation_map.get(rule_fqn, {}) + expression = rule_yaml["expression"] + if isinstance(expression, dict): # Handle named expressions + expression = expression.get("name", "") + + tags = rule_yaml.get("tags", []) + annotation_type = RuleActionAnnotationKind.from_str(rule_yaml["type"]) + action: RuleAction = RuleActionCreatePhaseAnnotation(tags) + if annotation_type == RuleActionAnnotationKind.REVIEW: + action = RuleActionCreateDataReviewAnnotation( + assignee=rule_yaml.get("assignee"), + tags=tags, + ) + rule_configs.append( RuleConfig( name=rule_yaml["name"], namespace=namespace, namespace_rules=namespaced_rules, + action=action, rule_client_key=rule_yaml.get("rule_client_key"), description=rule_yaml.get("description", ""), expression=cast(str, rule_yaml["expression"]),