Skip to content

Commit 1bff09f

Browse files
authored
Merge pull request #400 from ansforge/feat/rs-to-cisu-logging
Feat/cisu to rs logging
2 parents f08c94a + 9209019 commit 1bff09f

3 files changed

Lines changed: 97 additions & 7 deletions

File tree

converter/converter/cisu/resources_info/resources_info_cisu_converter.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from converter.cisu.resources_info.resources_info_cisu_helper import (
1010
enrich_rs_ri_with_rs_srs,
1111
get_latest_state,
12+
log_cisu_to_rs_converted_messages_ids,
1213
)
1314
from converter.repositories.message_repository import (
1415
get_last_rc_ri_by_case_id,
@@ -181,6 +182,9 @@ def from_cisu_to_rs(cls, edxl_json: Dict[str, Any]) -> List[Dict[str, Any]]:
181182
converted_messages.append(
182183
cls._build_rs_sr_from_resource(edxl_json, resource, case_id)
183184
)
185+
log_cisu_to_rs_converted_messages_ids(
186+
edxl_json, converted_messages[0], converted_messages[1:]
187+
)
184188
return converted_messages
185189

186190
# Known caseId — compare resources and emit only what changed
@@ -196,15 +200,16 @@ def from_cisu_to_rs(cls, edxl_json: Dict[str, Any]) -> List[Dict[str, Any]]:
196200
"modified_status_resources"
197201
]
198202

199-
messages: List[Dict[str, Any]] = []
203+
converted_rs_ri = None
200204

201205
if engaged_resources_updated:
202206
logger.info(
203207
"Resources added/removed for caseId %s — adding RS-RI to output.",
204208
case_id,
205209
)
206-
rs_ri = cls._build_rs_ri_from_cisu(edxl_json)
207-
messages.append(rs_ri)
210+
converted_rs_ri = cls._build_rs_ri_from_cisu(edxl_json)
211+
212+
converted_rs_sr_messages: List[Dict[str, Any]] = []
208213

209214
for idx, resource in enumerate(modified_status_resources):
210215
logger.info(
@@ -217,12 +222,18 @@ def from_cisu_to_rs(cls, edxl_json: Dict[str, Any]) -> List[Dict[str, Any]]:
217222
rs_sr = cls._build_rs_sr_from_resource(
218223
edxl_json, resource, case_id, should_use_original_distribution_id
219224
)
220-
messages.append(rs_sr)
225+
converted_rs_sr_messages.append(rs_sr)
221226

222-
if not messages:
223-
logger.info("No resource changes detected for caseId %s.", case_id)
227+
converted_messages = []
228+
if converted_rs_ri is not None:
229+
converted_messages.append(converted_rs_ri)
230+
converted_messages += converted_rs_sr_messages
231+
232+
log_cisu_to_rs_converted_messages_ids(
233+
edxl_json, converted_rs_ri, converted_rs_sr_messages
234+
)
224235

225-
return messages
236+
return converted_messages
226237

227238
@classmethod
228239
def from_rs_to_cisu(cls, edxl_json: Dict[str, Any]) -> Dict[str, Any]:

converter/converter/cisu/resources_info/resources_info_cisu_helper.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,26 @@ def enrich_rs_ri_with_rs_srs(
9797
def get_latest_state(states: list[dict]) -> dict:
9898
"""Return the state with the most recent datetime from a list of states."""
9999
return sorted(states, key=lambda x: x.get("datetime", ""))[-1]
100+
101+
102+
def log_cisu_to_rs_converted_messages_ids(
103+
original_message: dict, rs_ri: dict | None, rs_sr_list: list[dict]
104+
) -> None:
105+
if rs_ri is None and len(rs_sr_list) == 0:
106+
logger.info(
107+
f"No RS message produced when converting RC-RI with distributionId {original_message.get('distributionID')}."
108+
)
109+
110+
else:
111+
log_message = f"Converted RC-RI with distributionID {original_message.get('distributionID')} into"
112+
113+
if rs_ri is not None:
114+
log_message += f" RS-RI {rs_ri.get('distributionID')}"
115+
116+
if rs_sr_list:
117+
if rs_ri is not None:
118+
log_message += " and"
119+
rs_sr_ids = [rs_sr.get("distributionID") for rs_sr in rs_sr_list]
120+
log_message += f" {len(rs_sr_list)} RS-SR(s) {rs_sr_ids}"
121+
122+
logger.info(log_message)

converter/tests/cisu/test_resources_info_cisu_helper.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from converter.cisu.resources_info.resources_info_cisu_helper import (
22
enrich_rs_ri_with_rs_srs,
3+
log_cisu_to_rs_converted_messages_ids,
34
)
45

56

@@ -124,3 +125,58 @@ def test_current_state_and_persisted_rs_sr():
124125
assert result is not None
125126
assert result["resource"][0]["state"][0]["status"] == "PENDING"
126127
assert result["resource"][1]["state"][0]["status"] == "DONE2"
128+
129+
130+
class TestLogCisuToRsConvertedMessagesIds:
131+
def test_with_rs_ri_and_rs_srs(self, caplog):
132+
original = {"distributionID": "orig-123"}
133+
rs_ri = {"distributionID": "ri-456"}
134+
rs_sr_list = [
135+
{"distributionID": "sr-1"},
136+
{"distributionID": "sr-2"},
137+
]
138+
139+
with caplog.at_level("INFO"):
140+
log_cisu_to_rs_converted_messages_ids(original, rs_ri, rs_sr_list)
141+
142+
assert len(caplog.records) == 1
143+
assert caplog.records[0].message == (
144+
"Converted RC-RI with distributionID orig-123 into RS-RI ri-456 and 2 RS-SR(s) ['sr-1', 'sr-2']"
145+
)
146+
147+
def test_without_rs_ri(self, caplog):
148+
original = {"distributionID": "orig-123"}
149+
rs_sr_list = [
150+
{"distributionID": "sr-1"},
151+
]
152+
153+
with caplog.at_level("INFO"):
154+
log_cisu_to_rs_converted_messages_ids(original, None, rs_sr_list)
155+
156+
assert len(caplog.records) == 1
157+
assert caplog.records[0].message == (
158+
"Converted RC-RI with distributionID orig-123 into 1 RS-SR(s) ['sr-1']"
159+
)
160+
161+
def test_with_rs_ri_and_no_rs_srs(self, caplog):
162+
original = {"distributionID": "orig-123"}
163+
rs_ri = {"distributionID": "ri-456"}
164+
165+
with caplog.at_level("INFO"):
166+
log_cisu_to_rs_converted_messages_ids(original, rs_ri, [])
167+
168+
assert len(caplog.records) == 1
169+
assert caplog.records[0].message == (
170+
"Converted RC-RI with distributionID orig-123 into RS-RI ri-456"
171+
)
172+
173+
def test_without_rs_ri_and_no_rs_srs(self, caplog):
174+
original = {"distributionID": "orig-123"}
175+
176+
with caplog.at_level("INFO"):
177+
log_cisu_to_rs_converted_messages_ids(original, None, [])
178+
179+
assert len(caplog.records) == 1
180+
assert caplog.records[0].message == (
181+
"No RS message produced when converting RC-RI with distributionId orig-123."
182+
)

0 commit comments

Comments
 (0)