Skip to content

Commit

Permalink
fix mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
solidiquis committed Jun 24, 2024
1 parent ab8b9fc commit 3e27098
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 37 deletions.
2 changes: 1 addition & 1 deletion python/lib/sift_py/ingestion/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def from_pb(cls, message: ChannelConfigPb) -> Self:

def fqn(self) -> str:
"""
The fully-qualified channel name of a channel called 'voltage' is simply `voltage'. The
The fully-qualified channel name of a channel called 'voltage' is simply `voltage`. The
fully qualified name of a channel called 'temperature' of component 'motor' is a `motor.temperature'.
"""
return channel_fqn(self)
Expand Down
5 changes: 4 additions & 1 deletion python/lib/sift_py/ingestion/config/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from sift_py.ingestion.flow import FlowConfig
from sift_py.ingestion.rule.config import (
ExpressionChannelReference,
ExpressionChannelReferenceChannelConfig,
RuleAction,
RuleActionAnnotationKind,
RuleActionCreateDataReviewAnnotation,
Expand Down Expand Up @@ -191,7 +192,9 @@ def _from_yaml(
tags=tags,
)

channel_references: List[ExpressionChannelReference] = []
channel_references: List[
ExpressionChannelReference | ExpressionChannelReferenceChannelConfig
] = []

for channel_reference in rule.get("channel_references", []):
for ref, val in channel_reference.items():
Expand Down
46 changes: 41 additions & 5 deletions python/lib/sift_py/ingestion/rule/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from abc import ABC, abstractmethod
from enum import Enum
from typing import Any, Dict, List, Optional, TypedDict
from typing import Any, Dict, List, Optional, TypedDict, cast

from sift.annotations.v1.annotations_pb2 import AnnotationType
from sift.rules.v1.rules_pb2 import ActionKind

from sift_py._internal.convert.json import AsJson
from sift_py.ingestion.channel import ChannelConfig


class RuleConfig(AsJson):
Expand Down Expand Up @@ -40,13 +41,38 @@ def __init__(
description: str,
expression: str,
action: RuleAction,
channel_references: List[ExpressionChannelReference],
channel_references: List[
ExpressionChannelReference | ExpressionChannelReferenceChannelConfig
],
sub_expressions: Dict[str, Any] = {},
):
self.channel_references = []

for channel_reference in channel_references:
config = channel_reference.get("channel_config")

if config is not None:
config = cast(ChannelConfig, config)

self.channel_references.append(
{
"channel_reference": channel_reference["channel_reference"],
"channel_identifier": config.fqn(),
}
)
else:
channel_ref = cast(ExpressionChannelReference, channel_reference)

self.channel_references.append(
{
"channel_reference": channel_ref["channel_reference"],
"channel_identifier": channel_ref["channel_identifier"],
}
)

self.name = name
self.description = description
self.action = action
self.channel_references = channel_references
self.expression = self.__class__.interpolate_sub_expressions(expression, sub_expressions)

def as_json(self) -> Any:
Expand Down Expand Up @@ -182,9 +208,19 @@ class RuleActionKindStrRep(Enum):

class ExpressionChannelReference(TypedDict):
"""
`reference`: The channel reference (e.g. '$1') used in the expression.
`identifier`: The fully qualified channel name. See `sift_py.ingestion.channel.channel_fqn`.
`channel_reference`: The channel reference (e.g. '$1') used in the expression.
`channel_identifier`: The fully qualified channel name. See `sift_py.ingestion.channel.channel_fqn`.
"""

channel_reference: str
channel_identifier: str


class ExpressionChannelReferenceChannelConfig(TypedDict):
"""
`channel_reference`: The channel reference (e.g. '$1') used in the expression.
`channel_config`: Instance of `sift_py.ingestion.channel.ChannelConfig`.
"""

channel_reference: str
channel_config: ChannelConfig
75 changes: 45 additions & 30 deletions python/lib/sift_py/ingestion/rule/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ def test_rule_config_json():
description="Rock & Roll",
expression=voltage_rule_expression,
action=RuleActionCreatePhaseAnnotation(),
channel_references={
"$1": ChannelConfig(
name="voltage",
data_type=ChannelDataType.DOUBLE,
),
},
channel_references=[
{
"channel_reference": "$1",
"channel_config": ChannelConfig(
name="voltage",
data_type=ChannelDataType.DOUBLE,
),
}
],
)
assert voltage_rule_config.expression == voltage_rule_expression

Expand All @@ -32,18 +35,24 @@ def test_rule_config_json():
tags=["foo", "bar"],
assignee="[email protected]",
),
channel_references={
"$1": ChannelConfig(
name="vehicle_state",
component="motor",
data_type=ChannelDataType.INT_32,
),
"$2": ChannelConfig(
name="temperature",
component="motor",
data_type=ChannelDataType.INT_32,
),
},
channel_references=[
{
"channel_reference": "$1",
"channel_config": ChannelConfig(
name="vehicle_state",
component="motor",
data_type=ChannelDataType.INT_32,
),
},
{
"channel_reference": "$2",
"channel_config": ChannelConfig(
name="temperature",
component="motor",
data_type=ChannelDataType.INT_32,
),
},
],
sub_expressions={
"$3": 80,
},
Expand All @@ -59,12 +68,15 @@ def test_rule_config_json():
tags=["foo", "bar"],
assignee="[email protected]",
),
channel_references={
"$1": ChannelConfig(
name="log",
data_type=ChannelDataType.INT_32,
),
},
channel_references=[
{
"channel_reference": "$1",
"channel_config": ChannelConfig(
name="log",
data_type=ChannelDataType.INT_32,
),
},
],
sub_expressions={
"$2": "Error",
},
Expand All @@ -80,12 +92,15 @@ def test_rule_named_expressions():
description="checks high periods of energy output",
expression=kinetic_energy_gt_expression,
action=RuleActionCreatePhaseAnnotation(),
channel_references={
"$1": ChannelConfig(
name="log",
data_type=ChannelDataType.INT_32,
),
},
channel_references=[
{
"channel_reference": "$1",
"channel_config": ChannelConfig(
name="velocity",
data_type=ChannelDataType.INT_32,
),
},
],
sub_expressions={
"$mass": 10,
"$threshold": 35,
Expand Down

0 comments on commit 3e27098

Please sign in to comment.