Skip to content

Commit

Permalink
fix: Add actions to rules
Browse files Browse the repository at this point in the history
  • Loading branch information
niliayu committed Dec 10, 2024
1 parent a22640a commit 4cf7d20
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
11 changes: 5 additions & 6 deletions python/examples/asset_agnostic_rules/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}),
],
)

Expand Down
4 changes: 2 additions & 2 deletions python/examples/asset_agnostic_rules/rule_modules/voltage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]
asset_names:
Expand All @@ -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: [email protected]
asset_names:
Expand Down
16 changes: 16 additions & 0 deletions python/lib/sift_py/rule/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
RuleActionKind,
RuleConfig,
)
from sift_py.rule.config import RuleAction
from sift_py.yaml.rule import RuleActionAnnotationKind


class RuleService:
Expand Down Expand Up @@ -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"]),
Expand Down

0 comments on commit 4cf7d20

Please sign in to comment.