Skip to content

Commit

Permalink
refactor: rename item and camera rules with same nomenclature
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptiste O'Jeanson committed Oct 19, 2023
1 parent 23e7d72 commit e35da7e
Show file tree
Hide file tree
Showing 30 changed files with 95 additions and 76 deletions.
4 changes: 2 additions & 2 deletions edge_orchestrator/config/inventory.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"max_nb_objects_rule"
],
"item_rules": [
"threshold_ratio_rule",
"min_threshold_KO_rule"
"min_threshold_ok_ratio_rule",
"min_threshold_ko_rule"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
},
"item_rule": {
"name": "min_threshold_KO_rule",
"name": "min_threshold_ko_rule",
"parameters": {
"threshold": 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
},
"item_rule": {
"name": "min_threshold_KO_rule",
"name": "min_threshold_ko_rule",
"parameters": {
"threshold": 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
},
"item_rule": {
"name": "min_threshold_KO_rule",
"name": "min_threshold_ko_rule",
"parameters": {
"threshold": 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
}
},
"item_rule": {
"name": "min_threshold_KO_rule",
"name": "min_threshold_ko_rule",
"parameters": {
"threshold": 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
},
"item_rule": {
"name": "min_threshold_KO_rule",
"name": "min_threshold_ko_rule",
"parameters": {
"threshold": 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
}
},
"item_rule": {
"name": "min_threshold_KO_rule",
"name": "min_threshold_ko_rule",
"parameters": {
"threshold": 1
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Type

from edge_orchestrator.domain.models.business_rule.camera_rule.camera_rule import (
CameraRule,
)
from edge_orchestrator.domain.models.business_rule.camera_rule.expected_label_rule import (
ExpectedLabelRule,
)
from edge_orchestrator.domain.models.business_rule.camera_rule.max_nb_objects_rule import (
MaxNbObjectsRule,
)
from edge_orchestrator.domain.models.business_rule.camera_rule.min_nb_objects_rule import (
MinNbObjectsRule,
)

AVAILABLE_CAMERA_RULES = {
"expected_label_rule": ExpectedLabelRule,
"min_nb_objects_rule": MinNbObjectsRule,
"max_nb_objects_rule": MaxNbObjectsRule,
}


def get_camera_rule(rule_name: str) -> Type[CameraRule]:
try:
return AVAILABLE_CAMERA_RULES[rule_name]
except KeyError as error:
raise NotImplementedError(f"Unknown camera rule name: {rule_name}") from error
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Dict, Union

from edge_orchestrator.domain.models.business_rules.camera_rule import CameraRule
from edge_orchestrator.domain.models.business_rule.camera_rule.camera_rule import (
CameraRule,
)
from edge_orchestrator.domain.models.decision import Decision


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Dict, Union

from edge_orchestrator.domain.models.business_rules.camera_rule import CameraRule
from edge_orchestrator.domain.models.business_rule.camera_rule.camera_rule import (
CameraRule,
)
from edge_orchestrator.domain.models.decision import Decision


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Dict, Union

from edge_orchestrator.domain.models.business_rules.camera_rule import CameraRule
from edge_orchestrator.domain.models.business_rule.camera_rule.camera_rule import (
CameraRule,
)
from edge_orchestrator.domain.models.decision import Decision


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import Type

from edge_orchestrator.domain.models.business_rule.item_rule.item_rule import (
ItemRule,
)
from edge_orchestrator.domain.models.business_rule.item_rule.min_threshold_ko_rule import (
MinThresholdKORule,
)
from edge_orchestrator.domain.models.business_rule.item_rule.min_threshold_ok_ratio_rule import (
MinThresholdOKRatioRule,
)

AVAILABLE_ITEM_RULES = {
"min_threshold_ok_ratio_rule": MinThresholdOKRatioRule,
"min_threshold_ko_rule": MinThresholdKORule,
}


def get_item_rule(rule_name: str) -> Type[ItemRule]:
try:
return AVAILABLE_ITEM_RULES[rule_name]
except KeyError as error:
raise NotImplementedError(f"Unknown item rule name: {rule_name}") from error
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Dict

from edge_orchestrator.domain.models.business_rules.item_rule import ItemRule
from domain.models.business_rule.item_rule.item_rule import ItemRule
from edge_orchestrator.domain.models.decision import Decision


class ThresholdRule(ItemRule):
class MinThresholdKORule(ItemRule):
def __init__(self, threshold: int):
self.threshold = threshold

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Dict

from edge_orchestrator.domain.models.business_rules.item_rule import ItemRule
from domain.models.business_rule.item_rule.item_rule import ItemRule
from edge_orchestrator.domain.models.decision import Decision


class ThresholdRatioRule(ItemRule):
class MinThresholdOKRatioRule(ItemRule):
def __init__(self, min_threshold: float):
self.min_threshold = min_threshold

Expand Down
24 changes: 1 addition & 23 deletions edge_orchestrator/edge_orchestrator/domain/models/camera.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
from abc import abstractmethod, ABC
from typing import Dict, Type, Union

from edge_orchestrator.domain.models.business_rules.camera_business_rules.expected_label_rule import (
ExpectedLabelRule,
)
from edge_orchestrator.domain.models.business_rules.camera_business_rules.max_nb_objects_rule import (
MaxNbObjectsRule,
)
from edge_orchestrator.domain.models.business_rules.camera_business_rules.min_nb_objects_rule import (
MinNbObjectsRule,
)
from edge_orchestrator.domain.models.business_rules.camera_rule import CameraRule


def get_camera_rule(rule_name) -> Type[CameraRule]:
if rule_name == "expected_label_rule":
return ExpectedLabelRule
elif rule_name == "min_nb_objects_rule":
return MinNbObjectsRule
elif rule_name == "max_nb_objects_rule":
return MaxNbObjectsRule
else:
raise NotImplementedError
from typing import Dict, Union


def get_last_inference_by_camera(inference: Dict) -> Dict:
Expand Down
22 changes: 2 additions & 20 deletions edge_orchestrator/edge_orchestrator/domain/models/item.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
import datetime as dt
import uuid
from typing import Dict, Type
from typing import Dict

from edge_orchestrator.domain.models.business_rules.item_business_rule.item_threshold_ratio_rule import (
ThresholdRatioRule,
)
from edge_orchestrator.domain.models.business_rules.item_business_rule.item_threshold_rule import (
ThresholdRule,
)
from edge_orchestrator.domain.models.business_rules.item_rule import ItemRule


def generate_id():
def generate_id() -> str:
return str(uuid.uuid4())


def get_item_rule(item_rule_name) -> Type[ItemRule]:
if item_rule_name == "threshold_ratio_rule":
return ThresholdRatioRule

elif item_rule_name == "min_threshold_KO_rule":
return ThresholdRule
else:
raise NotImplementedError


class Item:
def __init__(
self,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from abc import abstractmethod
from typing import Dict


class TelemetrySink:
@abstractmethod
async def send(self, message: str):
async def send(self, message: Dict):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from PIL import Image

from domain.models.business_rule.camera_rule.camera_rule_factory import get_camera_rule
from domain.models.business_rule.item_rule.item_rule_factory import get_item_rule
from edge_orchestrator.api_config import (
get_binary_storage,
get_edge_station,
Expand All @@ -16,12 +18,10 @@
logger,
)
from edge_orchestrator.domain.models.camera import (
get_camera_rule,
get_last_inference_by_camera,
)
from edge_orchestrator.domain.models.decision import Decision
from edge_orchestrator.domain.models.item import Item
from edge_orchestrator.domain.models.item import get_item_rule
from edge_orchestrator.domain.models.model_infos import ModelInfos
from edge_orchestrator.domain.models.supervisor_state import SupervisorState

Expand Down Expand Up @@ -89,11 +89,11 @@ async def set_inferences(item: Item):
@self.save_item_metadata
async def set_decision(item: Item):
decision = self.apply_business_rules(item)
item.decision = decision
item.decision = decision.value
telemetry_msg = {
"item_id": item.id,
"config": item.station_config,
"decision": decision,
"decision": decision.value,
}

await self.telemetry_sink.send(telemetry_msg)
Expand Down Expand Up @@ -200,7 +200,7 @@ async def get_inference(
return inference_output

@abstractmethod
def apply_business_rules(self, item: Item) -> str:
def apply_business_rules(self, item: Item) -> Decision:
camera_decisions = {}

if item.inferences == Decision.NO_DECISION:
Expand Down Expand Up @@ -241,7 +241,7 @@ def apply_business_rules(self, item: Item) -> str:
item_rule = get_item_rule(item_rule_name)(**item_rule_parameters)
item_decision = item_rule.get_item_decision(camera_decisions)

return item_decision.value
return item_decision


def get_labels(inferences):
Expand Down
4 changes: 2 additions & 2 deletions edge_orchestrator/tests/config/inventory_TEST.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"min_nb_objects_rule"
],
"item_rules": [
"threshold_ratio_rule",
"min_threshold_KO_rule"
"min_threshold_ok_ratio_rule",
"min_threshold_ko_rule"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
},
"item_rule": {
"name": "min_threshold_KO_rule",
"name": "min_threshold_ko_rule",
"parameters": {
"threshold": 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}
},
"item_rule": {
"name": "min_threshold_KO_rule",
"name": "min_threshold_ko_rule",
"parameters": {
"threshold": 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Feature: The client set an active configuration
}
},
"item_rule": {
"name": "min_threshold_KO_rule",
"name": "min_threshold_ko_rule",
"parameters": {
"threshold": 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ Feature: The client requests the inventory available on the station
"min_nb_objects_rule"
],
"item_rules": [
"threshold_ratio_rule",
"min_threshold_KO_rule"
"min_threshold_ok_ratio_rule",
"min_threshold_ko_rule"
]
}
"""
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from domain.models.business_rule.camera_rule.camera_rule_factory import get_camera_rule

from edge_orchestrator.domain.models.camera import (
get_camera_rule,
get_last_inference_by_camera,
)
from edge_orchestrator.domain.use_cases.supervisor import get_labels
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from domain.models.business_rule.item_rule.item_rule_factory import get_item_rule

from edge_orchestrator.domain.models.decision import Decision
from edge_orchestrator.domain.models.item import get_item_rule


class TestItemBusinessRule:
Expand All @@ -10,7 +11,7 @@ def test_item_decision_should_return_decision_ko_when_one_or_more_than_one_camer
camera_decisions = {"camera_id1": "KO", "camera_id2": "OK", "camera_id3": "OK"}

# When
item_rule_name = "min_threshold_KO_rule"
item_rule_name = "min_threshold_ko_rule"
item_rule_parameters = {"threshold": 1}

item_rule = get_item_rule(item_rule_name)(**item_rule_parameters)
Expand All @@ -26,7 +27,7 @@ def test_item_decision_should_return_decision_ok_when_more_than_50_pct_of_camera
camera_decisions = {"camera_id1": "OK", "camera_id2": "OK", "camera_id3": "OK"}

# When
item_rule_name = "threshold_ratio_rule"
item_rule_name = "min_threshold_ok_ratio_rule"
item_rule_parameters = {"min_threshold": 0.5}

item_rule = get_item_rule(item_rule_name)(**item_rule_parameters)
Expand Down

0 comments on commit e35da7e

Please sign in to comment.