Skip to content

Commit b851b51

Browse files
authored
Merge pull request #398 from ansforge/feat/distribution-ids-logging
feat(converter): distributionIDs logging during enrichment
2 parents 56f6be1 + 94fb61d commit b851b51

4 files changed

Lines changed: 134 additions & 48 deletions

File tree

converter/converter/cisu/resources_info/resources_info_cisu_converter.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,19 +227,24 @@ def from_rs_to_cisu(cls, edxl_json: Dict[str, Any]) -> Dict[str, Any]:
227227

228228
logger.info("Converting from RS to CISU format for Resources Info message.")
229229
logger.debug(f"Message content: {edxl_json}")
230+
231+
logger.info(
232+
"Received RS-RI message with distributionID: %s for CISU conversion",
233+
edxl_json.get("distributionID"),
234+
)
235+
230236
output_json = cls.copy_rs_input_content(edxl_json)
231237
current_use_case = cls.copy_rs_input_use_case_content(edxl_json)
232238

233239
case_id = get_field_value(current_use_case, "caseId")
234240

235241
_, persisted_rs_sr = get_rs_messages_by_case_id(case_id)
236-
rs_sr_use_cases = [
237-
# Hardcoded RS_SR use case to avoid circular dependency
238-
# TODO: Fix
239-
cls._copy_input_use_case_content(pm.payload, "resourcesStatus")
240-
for pm in persisted_rs_sr
241-
]
242-
enriched = enrich_rs_ri_with_rs_srs(current_use_case, rs_sr_use_cases)
242+
rs_sr_edxls = [pm.payload for pm in persisted_rs_sr]
243+
244+
enriched = enrich_rs_ri_with_rs_srs(
245+
rs_ri_edxl=edxl_json,
246+
rs_sr_edxl_list=rs_sr_edxls,
247+
)
243248

244249
return cls.convert_single_rs_ri(output_json, enriched)
245250

converter/converter/cisu/resources_info/resources_info_cisu_helper.py

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from converter.conversion_mixin import ConversionMixin
12
from converter.utils import get_field_value, set_value
23
from converter.cisu.resources_info.resources_info_cisu_constants import (
34
ResourcesInfoCISUConstants,
@@ -6,26 +7,61 @@
67

78
logger = logging.getLogger(__name__)
89

10+
_DISTRIBUTION_ID_KEY = ConversionMixin.EDXL_DISTRIBUTION_ID_KEY
911

10-
def enrich_rs_ri_with_rs_srs(rs_ri: dict, rs_sr_list: list[dict]) -> dict:
11-
"""Enrich RS-RI resources with state from a list of RS-SR messages (in-place + return)."""
1212

13-
if not rs_sr_list:
14-
return rs_ri
13+
def enrich_rs_ri_with_rs_srs(
14+
rs_ri_edxl: dict,
15+
rs_sr_edxl_list: list[dict],
16+
) -> dict:
17+
"""Enrich RS-RI resources with state from RS-SR messages.
18+
Receives full EDXL envelopes, extracts use cases and distribution IDs internally.
19+
Centralizes all distribution-ID logging for both RS-RI and RS-SR conversion flows.
20+
"""
21+
rs_ri_distribution_id = rs_ri_edxl.get(_DISTRIBUTION_ID_KEY)
22+
rs_sr_distribution_ids = [sr.get(_DISTRIBUTION_ID_KEY) for sr in rs_sr_edxl_list]
1523

16-
rs_ri_resources = get_field_value(rs_ri, ResourcesInfoCISUConstants.RESOURCE_PATH)
17-
sr_by_resource_id = {sr.get("resourceId"): sr for sr in rs_sr_list}
24+
logger.info(
25+
"Persisted RS-SRs found for RS-RI message with distributionID: %s -> %s",
26+
rs_ri_distribution_id,
27+
rs_sr_distribution_ids,
28+
)
29+
30+
rs_ri_use_case = ConversionMixin._copy_input_use_case_content(
31+
rs_ri_edxl, "resourcesInfo"
32+
)
33+
34+
if not rs_sr_edxl_list:
35+
return rs_ri_use_case
36+
37+
rs_sr_entries = [
38+
(
39+
ConversionMixin._copy_input_use_case_content(sr, "resourcesStatus"),
40+
sr.get(_DISTRIBUTION_ID_KEY),
41+
)
42+
for sr in rs_sr_edxl_list
43+
]
44+
sr_by_resource_id = {
45+
use_case.get("resourceId"): (use_case, dist_id)
46+
for use_case, dist_id in rs_sr_entries
47+
}
48+
49+
rs_ri_resources = get_field_value(
50+
rs_ri_use_case, ResourcesInfoCISUConstants.RESOURCE_PATH
51+
)
52+
used_sr_distribution_ids = []
1853

1954
for resource in rs_ri_resources:
2055
resource_id = resource.get("resourceId")
21-
persisted_sr = sr_by_resource_id.get(resource_id)
22-
if persisted_sr is None:
56+
entry = sr_by_resource_id.get(resource_id)
57+
if entry is None:
2358
continue
2459

60+
persisted_sr, sr_dist_id = entry
2561
persisted_state = persisted_sr.get("state")
2662
if persisted_state is None:
2763
logger.warning(
28-
f"No state found in persisted rs-sr for resourceId: {resource_id}"
64+
"No state found in persisted RS-SR for resourceId: %s", resource_id
2965
)
3066
continue
3167

@@ -38,7 +74,6 @@ def enrich_rs_ri_with_rs_srs(rs_ri: dict, rs_sr_list: list[dict]) -> dict:
3874
ResourcesInfoCISUConstants.STATE_PATH,
3975
[persisted_state],
4076
)
41-
4277
else:
4378
latest_state = get_latest_state([*current_states, persisted_state])
4479
set_value(
@@ -47,7 +82,16 @@ def enrich_rs_ri_with_rs_srs(rs_ri: dict, rs_sr_list: list[dict]) -> dict:
4782
[latest_state],
4883
)
4984

50-
return rs_ri
85+
used_sr_distribution_ids.append(sr_dist_id)
86+
87+
logger.info(
88+
"Enriched RS-RI (%s) with %s RS-SR (%s) before conversion",
89+
rs_ri_distribution_id,
90+
len(used_sr_distribution_ids),
91+
used_sr_distribution_ids,
92+
)
93+
94+
return rs_ri_use_case
5195

5296

5397
def get_latest_state(states: list[dict]) -> dict:

converter/converter/cisu/resources_status/resources_status_converter.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
from converter.utils import get_field_value
1717

18+
import logging
19+
20+
logger = logging.getLogger(__name__)
21+
1822

1923
class ResourcesStatusConverter(BaseCISUConverter):
2024
@classmethod
@@ -29,6 +33,11 @@ def get_cisu_message_type(cls) -> str:
2933
def from_rs_to_cisu(
3034
cls, edxl_json: Dict[str, Any]
3135
) -> Dict[str, Any] | list[Dict[str, Any]]:
36+
logger.info(
37+
"Received RS-SR message with distributionID: %s for CISU conversion",
38+
edxl_json.get("distributionID"),
39+
)
40+
3241
current_use_case = cls.copy_rs_input_use_case_content(edxl_json)
3342
case_id = get_field_value(current_use_case, ResourcesStatusConstants.CASE_ID)
3443

@@ -38,15 +47,14 @@ def from_rs_to_cisu(
3847

3948
rs_ri = rs_ri_msg.payload
4049

41-
rs_sr_use_cases = [
42-
cls.copy_rs_input_use_case_content(pm.payload) for pm in persisted_rs_sr
43-
]
44-
rs_sr_use_cases.append(current_use_case)
50+
rs_sr_edxl_list = [pm.payload for pm in persisted_rs_sr]
51+
rs_sr_edxl_list.append(edxl_json)
4552

4653
output_json = ResourcesInfoCISUConverter.copy_rs_input_content(rs_ri)
47-
rs_ri_use_case = ResourcesInfoCISUConverter.copy_rs_input_use_case_content(
48-
rs_ri
54+
55+
enriched = enrich_rs_ri_with_rs_srs(
56+
rs_ri_edxl=rs_ri,
57+
rs_sr_edxl_list=rs_sr_edxl_list,
4958
)
50-
enriched = enrich_rs_ri_with_rs_srs(rs_ri_use_case, rs_sr_use_cases)
5159

5260
return ResourcesInfoCISUConverter.convert_single_rs_ri(output_json, enriched)

converter/tests/cisu/test_resources_info_cisu_helper.py

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,31 @@
33
)
44

55

6-
def _make_rs_ri(resources: list[dict]) -> dict:
7-
"""Wrap a resource list in a minimal RS-RI use-case dict."""
8-
return {"resource": resources}
6+
def _wrap_edxl(
7+
use_case: dict, message_type: str, distribution_id: str = "dist-test"
8+
) -> dict:
9+
"""Wrap a use-case dict in a minimal EDXL envelope."""
10+
return {
11+
"distributionID": distribution_id,
12+
"content": [
13+
{
14+
"jsonContent": {
15+
"embeddedJsonContent": {"message": {message_type: use_case}}
16+
}
17+
}
18+
],
19+
}
20+
21+
22+
def _make_rs_ri_edxl(resources: list[dict], distribution_id: str = "dist-ri") -> dict:
23+
"""Wrap a resource list in a minimal RS-RI EDXL envelope."""
24+
25+
return _wrap_edxl({"resource": resources}, "resourcesInfo", distribution_id)
26+
27+
28+
def _make_rs_sr_edxl(use_case: dict, distribution_id: str = "dist-sr") -> dict:
29+
"""Wrap an RS-SR use-case in a minimal EDXL envelope."""
30+
return _wrap_edxl(use_case, "resourcesStatus", distribution_id)
931

1032

1133
def test_no_persisted_rs_sr():
@@ -15,16 +37,14 @@ def test_no_persisted_rs_sr():
1537
ne pas modifier la ressource originale.
1638
"""
1739

18-
rs_ri = _make_rs_ri(
40+
rs_ri_edxl = _make_rs_ri_edxl(
1941
[
2042
{"resourceId": "r1"},
2143
{"resourceId": "r2"},
2244
]
2345
)
2446

25-
rs_sr_list = []
26-
27-
result = enrich_rs_ri_with_rs_srs(rs_ri, rs_sr_list)
47+
result = enrich_rs_ri_with_rs_srs(rs_ri_edxl, [])
2848

2949
assert result is not None
3050
assert result["resource"][0] == {"resourceId": "r1"}
@@ -38,19 +58,19 @@ def test_no_current_state_but_persisted_rs_sr():
3858
remplacer le status de la ressource original par celui de la ressource persistée
3959
"""
4060

41-
rs_ri = _make_rs_ri(
61+
rs_ri_edxl = _make_rs_ri_edxl(
4262
[
4363
{"resourceId": "r1"},
4464
{"resourceId": "r2"},
4565
]
4666
)
4767

48-
rs_sr_list = [
49-
{"resourceId": "r1", "state": {"status": "OK"}},
50-
{"resourceId": "r2", "state": {"status": "KO"}},
68+
rs_sr_edxl_list = [
69+
_make_rs_sr_edxl({"resourceId": "r1", "state": {"status": "OK"}}, "dist-sr-1"),
70+
_make_rs_sr_edxl({"resourceId": "r2", "state": {"status": "KO"}}, "dist-sr-2"),
5171
]
5272

53-
result = enrich_rs_ri_with_rs_srs(rs_ri, rs_sr_list)
73+
result = enrich_rs_ri_with_rs_srs(rs_ri_edxl, rs_sr_edxl_list)
5474

5575
assert result is not None
5676
assert result["resource"][0]["state"] == [{"status": "OK"}]
@@ -64,7 +84,7 @@ def test_current_state_and_persisted_rs_sr():
6484
remplacer le status de la ressource original par le status le plus récent en se basant sur le champ datetime contenu dans le status.
6585
"""
6686

67-
rs_ri = _make_rs_ri(
87+
rs_ri_edxl = _make_rs_ri_edxl(
6888
[
6989
{
7090
"resourceId": "r1",
@@ -79,18 +99,27 @@ def test_current_state_and_persisted_rs_sr():
7999
]
80100
)
81101

82-
rs_sr_list = [
83-
{
84-
"resourceId": "r1",
85-
"state": {"status": "PENDING2", "datetime": "2025-05-18T17:00:00+02:00"},
86-
},
87-
{
88-
"resourceId": "r2",
89-
"state": {"status": "DONE2", "datetime": "2025-05-18T20:00:00+02:00"},
90-
},
102+
rs_sr_edxl_list = [
103+
_make_rs_sr_edxl(
104+
{
105+
"resourceId": "r1",
106+
"state": {
107+
"status": "PENDING2",
108+
"datetime": "2025-05-18T17:00:00+02:00",
109+
},
110+
},
111+
"dist-sr-1",
112+
),
113+
_make_rs_sr_edxl(
114+
{
115+
"resourceId": "r2",
116+
"state": {"status": "DONE2", "datetime": "2025-05-18T20:00:00+02:00"},
117+
},
118+
"dist-sr-2",
119+
),
91120
]
92121

93-
result = enrich_rs_ri_with_rs_srs(rs_ri, rs_sr_list)
122+
result = enrich_rs_ri_with_rs_srs(rs_ri_edxl, rs_sr_edxl_list)
94123

95124
assert result is not None
96125
assert result["resource"][0]["state"][0]["status"] == "PENDING"

0 commit comments

Comments
 (0)