Skip to content

Commit

Permalink
feature/mx-1649 more rule editing prep (#212)
Browse files Browse the repository at this point in the history
### PR Context
- prep for robert-koch-institut/mex-editor#192

### Changes
- harmonize error handling for transforming raw rule-sets to responses
- return 404 on GET rule-set endpoint, when no rules are found

### Removed
- removed not needed `mex.backend.constants` module
  • Loading branch information
cutoffthetop authored Dec 5, 2024
1 parent d690927 commit 5880fcc
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 170 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changes

- harmonize error handling for transforming raw rule-sets to responses
- return 404 on GET rule-set endpoint, when no rules are found

### Deprecated

### Removed

- removed not needed `mex.backend.constants` module

### Fixed

### Security
Expand Down
1 change: 0 additions & 1 deletion mex/backend/constants.py

This file was deleted.

14 changes: 5 additions & 9 deletions mex/backend/merged/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from pydantic import Field, TypeAdapter, ValidationError

from mex.backend.constants import NUMBER_OF_RULE_TYPES
from mex.backend.fields import MERGEABLE_FIELDS_BY_CLASS_NAME
from mex.backend.graph.connector import GraphConnector
from mex.backend.graph.exceptions import InconsistentGraphError
Expand Down Expand Up @@ -153,23 +152,20 @@ def merge_search_result_item(item: dict[str, Any]) -> AnyMergedModel:
for component in item["components"]
if component["entityType"] in EXTRACTED_MODEL_CLASSES_BY_NAME
]
rules_raw = [
raw_rules = [
component
for component in item["components"]
if component["entityType"] in RULE_MODEL_CLASSES_BY_NAME
]
if len(rules_raw) == NUMBER_OF_RULE_TYPES:
rule_set_response = transform_raw_rules_to_rule_set_response(rules_raw)
elif len(rules_raw) == 0:
rule_set_response = None
if raw_rules:
rule_set = transform_raw_rules_to_rule_set_response(raw_rules)
else:
msg = f"Unexpected number of rules found in graph: {len(rules_raw)}"
raise InconsistentGraphError(msg)
rule_set = None

return create_merged_item(
identifier=item["identifier"],
extracted_items=extracted_items,
rule_set=rule_set_response,
rule_set=rule_set,
)


Expand Down
20 changes: 11 additions & 9 deletions mex/backend/rules/helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from collections.abc import Mapping
from typing import Any, Final

from mex.backend.constants import NUMBER_OF_RULE_TYPES
from mex.backend.graph.connector import GraphConnector
from mex.backend.graph.exceptions import InconsistentGraphError
from mex.common.exceptions import MExError
from mex.common.models import (
ADDITIVE_MODEL_CLASSES_BY_NAME,
Expand Down Expand Up @@ -32,9 +32,9 @@ def transform_raw_rules_to_rule_set_response(
response: dict[str, Any] = {}
model: type[AnyRuleModel] | None

if len(raw_rules) != NUMBER_OF_RULE_TYPES:
msg = "inconsistent rule item count"
raise MExError(msg)
if (num_raw_rules := len(raw_rules)) != len(MODEL_CLASS_LOOKUP_BY_FIELD_NAME):
msg = f"inconsistent number of rules found: {num_raw_rules}"
raise InconsistentGraphError(msg)

for rule in raw_rules:
for field_name, model_class_lookup in MODEL_CLASS_LOOKUP_BY_FIELD_NAME.items():
Expand All @@ -45,10 +45,10 @@ def transform_raw_rules_to_rule_set_response(

if len(set(stem_types)) != 1:
msg = "inconsistent rule item stem types"
raise MExError(msg)
raise InconsistentGraphError(msg)
if len(set(stable_target_ids)) != 1:
msg = "inconsistent rule item stableTargetIds"
raise MExError(msg)
msg = f"inconsistent rule item stableTargetIds: {', '.join(stable_target_ids)}"
raise InconsistentGraphError(msg)

response["stableTargetId"] = stable_target_ids[0]
response_class_name = ensure_postfix(stem_types[0], "RuleSetResponse")
Expand Down Expand Up @@ -83,7 +83,7 @@ def create_and_get_rule_set(

def get_rule_set_from_graph(
stable_target_id: Identifier,
) -> AnyRuleSetResponse:
) -> AnyRuleSetResponse | None:
"""Read a rule set from the graph."""
connector = GraphConnector.get()
graph_result = connector.fetch_rule_items(
Expand All @@ -93,7 +93,9 @@ def get_rule_set_from_graph(
0,
3,
)
return transform_raw_rules_to_rule_set_response(graph_result.one()["items"])
if raw_rules := graph_result.one()["items"]:
return transform_raw_rules_to_rule_set_response(raw_rules)
return None


def update_and_get_rule_set(
Expand Down
6 changes: 4 additions & 2 deletions mex/backend/rules/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi import APIRouter
from fastapi import APIRouter, HTTPException
from starlette import status

from mex.backend.rules.helpers import (
Expand All @@ -21,7 +21,9 @@ def create_rule_set(rule_set: AnyRuleSetRequest) -> AnyRuleSetResponse:
@router.get("/rule-set/{stableTargetId}", tags=["editor"])
def get_rule_set(stableTargetId: Identifier) -> AnyRuleSetResponse:
"""Get a rule set."""
return get_rule_set_from_graph(stableTargetId)
if rule_set := get_rule_set_from_graph(stableTargetId):
return rule_set
raise HTTPException(status.HTTP_404_NOT_FOUND, "no rules found")


@router.put("/rule-set/{stableTargetId}", tags=["editor"])
Expand Down
2 changes: 2 additions & 0 deletions mex/backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ def reraising(
try:
return fn(*args, **kwargs)
except original_error as error:
if isinstance(reraise_as, type):
reraise_as = reraise_as(*error.args)
raise reraise_as from error
Loading

0 comments on commit 5880fcc

Please sign in to comment.