Skip to content

Commit 0e738e2

Browse files
authored
python(feat): Add support for is_live to rules in sift_py (#427)
1 parent 7391e42 commit 0e738e2

7 files changed

Lines changed: 21 additions & 4 deletions

File tree

python/examples/ingestion_with_python_config/telemetry_config.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from pathlib import Path
2-
31
from sift_py.ingestion.channel import (
42
ChannelBitFieldElement,
53
ChannelConfig,
@@ -12,8 +10,6 @@
1210
RuleConfig,
1311
)
1412

15-
RULE_NAMESPACES_DIR = Path().joinpath("rule_modules")
16-
1713

1814
def nostromos_lv_426() -> TelemetryConfig:
1915
log_channel = ChannelConfig(
@@ -85,6 +81,7 @@ def nostromos_lv_426() -> TelemetryConfig:
8581
FlowConfig(name="logs", channels=[log_channel]),
8682
],
8783
rules=[
84+
# Add `is_live=True` if you want these rules to run on live data.
8885
RuleConfig(
8986
name="overheating",
9087
description="Checks for vehicle overheating",

python/examples/ingestion_with_yaml_config/rule_modules/nostromo.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Example rules configs. Add 'is_live' if you want these rules to run on live data.
2+
13
rules:
24
- name: overheating
35
description: Checks for vehicle overheating

python/examples/ingestion_with_yaml_config/rule_modules/velocity.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Example rules configs. Add 'is_live: true' if you want these rules to run on live data.
2+
13
rules:
24
- name: vehicle_stuck
35
description: Triggers if the vehicle velocity is not 0 for 5s after entering accelerating state

python/examples/ingestion_with_yaml_config/rule_modules/voltage.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# Example rules configs. Add 'is_live: true' if you want these rules to run on live data.
12
rules:
23
- name: overvoltage
34
description: Checks for overvoltage while accelerating

python/lib/sift_py/rule/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class RuleConfig(AsJson):
2727
- `tag_names`: A list of asset names that this rule should be applied to. ONLY VALID if defining rules outside of a telemetry config.
2828
- `contextual_channels`: A list of channel names that provide context but aren't directly used in the expression.
2929
- `is_external`: If this is an external rule.
30+
- `is_live`: If set to True then this rule will be evaluated on live data, otherwise live rule evaluation will be disabled.
31+
This rule can still be used, however, in report generation.
3032
"""
3133

3234
name: str
@@ -38,6 +40,7 @@ class RuleConfig(AsJson):
3840
asset_names: List[str]
3941
contextual_channels: List[str]
4042
is_external: bool
43+
is_live: bool
4144
_rule_id: Optional[str] # Allow passing of rule_id when existing config retrieved from API
4245

4346
def __init__(
@@ -55,6 +58,7 @@ def __init__(
5558
sub_expressions: Dict[str, Any] = {},
5659
contextual_channels: Optional[List[str]] = None,
5760
is_external: bool = False,
61+
is_live: bool = False,
5862
):
5963
self.channel_references = _channel_references_from_dicts(channel_references)
6064
self.contextual_channels = contextual_channels or []
@@ -66,6 +70,7 @@ def __init__(
6670
self.description = description
6771
self.expression = self.__class__.interpolate_sub_expressions(expression, sub_expressions)
6872
self.is_external = is_external
73+
self.is_live = is_live
6974
self._rule_id = None
7075

7176
def as_json(self) -> Any:
@@ -83,6 +88,7 @@ def as_json(self) -> Any:
8388
"description": self.description,
8489
"expression": self.expression,
8590
"is_external": self.is_external,
91+
"is_live": self.is_live,
8692
}
8793

8894
hash_map["expression_channel_references"] = self.channel_references

python/lib/sift_py/rule/service.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def create_external_rules_from_yaml(
9898
"""
9999
Creates external rules from a YAML spec in the Sift API.
100100
For more on rule YAML definitions, see `sift_py.ingestion.config.yaml.spec.RuleYamlSpec`.
101+
If is_external is set in the YAML, this overrides that.
101102
102103
Args:
103104
paths: The list of YAML paths to load.
@@ -229,6 +230,8 @@ def _parse_rules_from_yaml(
229230
contextual_channels=contextual_channels,
230231
asset_names=rule_yaml.get("asset_names", []),
231232
sub_expressions=subexpr,
233+
is_external=rule_yaml.get("is_external", False),
234+
is_live=rule_yaml.get("is_live", False),
232235
)
233236
)
234237

@@ -510,6 +513,7 @@ def _update_req_from_rule_config(
510513
),
511514
contextual_channels=ContextualChannels(channels=contextual_channel_names),
512515
is_external=config.is_external,
516+
is_live_evaluation_enabled=config.is_live,
513517
)
514518

515519
def get_rule(self, rule: str) -> Optional[RuleConfig]:

python/lib/sift_py/yaml/rule.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ class RuleYamlSpec(TypedDict):
270270
`sub_expressions`: A list of sub-expressions which is a mapping of place-holders to sub-expressions. Only used if using named expressions.
271271
`asset_names`: A list of asset names that this rule should be applied to. ONLY VALID if defining rules outside of a telemetry config.
272272
`tag_names`: A list of tag names that this rule should be applied to. ONLY VALID if defining rules outside of a telemetry config.
273+
`is_external`: If this is an external rule.
274+
`is_live`: If set to True then this rule will be evaluated on live data, otherwise live rule evaluation will be disabled.
275+
This rule can still be used, however, in report generation.
273276
274277
Channel references:
275278
A channel reference is a string containing a numerical value prefixed with "$". Examples include "$1", "$2", "$11", and so on.
@@ -326,6 +329,8 @@ class RuleYamlSpec(TypedDict):
326329
sub_expressions: NotRequired[List[Dict[str, str]]]
327330
asset_names: NotRequired[List[str]]
328331
tag_names: NotRequired[List[str]]
332+
is_external: NotRequired[bool]
333+
is_live: NotRequired[bool]
329334

330335

331336
class NamedExpressionYamlSpec(TypedDict):

0 commit comments

Comments
 (0)