refactor: extract shared union-or-nothing helper in _compute_query_time_filter
levels and model_ids (SIENTIAPDE-2149) both computed a per-group union with identical "union only if every active group restricts, else unrestricted" logic. Extracting _union_when_all_restrict removes the duplicated for/if block for each axis, addressing the SonarQube quality gate failure on this PR (cognitive complexity). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -147,14 +147,11 @@ class LoadNotificationPackage:
|
|||||||
if not sending_configs:
|
if not sending_configs:
|
||||||
return default_filter
|
return default_filter
|
||||||
|
|
||||||
sending_configs_levels = [group.get('levels') for group in sending_configs]
|
levels_union = self._union_when_all_restrict(
|
||||||
|
[group.get('levels') for group in sending_configs]
|
||||||
if all(sending_configs_levels):
|
)
|
||||||
levels_union: set[str] = set()
|
if levels_union is not None:
|
||||||
for levels in sending_configs_levels:
|
query_filter = {**default_filter, 'level': {'$in': levels_union}}
|
||||||
if levels:
|
|
||||||
levels_union.update(levels)
|
|
||||||
query_filter = {**default_filter, 'level': {'$in': sorted(levels_union)}}
|
|
||||||
else:
|
else:
|
||||||
query_filter = dict(default_filter)
|
query_filter = dict(default_filter)
|
||||||
|
|
||||||
@@ -166,21 +163,40 @@ class LoadNotificationPackage:
|
|||||||
]
|
]
|
||||||
models_intersection = set.intersection(*sending_configs_ignore_models_id)
|
models_intersection = set.intersection(*sending_configs_ignore_models_id)
|
||||||
|
|
||||||
sending_configs_model_ids = [group.get('model_ids') for group in sending_configs]
|
model_ids_union = self._union_when_all_restrict(
|
||||||
|
[group.get('model_ids') for group in sending_configs]
|
||||||
|
)
|
||||||
|
|
||||||
model_id_filter: dict[str, Any] = {}
|
model_id_filter: dict[str, Any] = {}
|
||||||
|
if model_ids_union is not None:
|
||||||
if all(sending_configs_model_ids):
|
model_id_filter['$in'] = model_ids_union
|
||||||
model_ids_union: set[str] = set()
|
|
||||||
for model_ids in sending_configs_model_ids:
|
|
||||||
if model_ids:
|
|
||||||
model_ids_union.update(model_ids)
|
|
||||||
model_id_filter['$in'] = sorted(model_ids_union)
|
|
||||||
|
|
||||||
if models_intersection:
|
if models_intersection:
|
||||||
model_id_filter['$nin'] = sorted(models_intersection)
|
model_id_filter['$nin'] = sorted(models_intersection)
|
||||||
|
|
||||||
if model_id_filter:
|
if model_id_filter:
|
||||||
query_filter['model_id'] = model_id_filter
|
query_filter['model_id'] = model_id_filter
|
||||||
|
|
||||||
return query_filter
|
return query_filter
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _union_when_all_restrict(per_group_values: list[list[str] | None]) -> list[str] | None:
|
||||||
|
"""
|
||||||
|
Union a list of per-group optional restrictions, but only when every group has one.
|
||||||
|
|
||||||
|
Shared by `levels` and `model_ids` (SIENTIAPDE-2149) in `_compute_query_time_filter`,
|
||||||
|
which both follow the same rule: if ANY active group is unrestricted on that axis
|
||||||
|
(missing/empty value), that group wants everything, so the union can't restrict the
|
||||||
|
query either — the caller falls back to no filter on that axis in that case (`None`).
|
||||||
|
|
||||||
|
Args:
|
||||||
|
per_group_values: Each active group's raw value for the axis being unioned
|
||||||
|
(e.g. `group.get('levels')` for every group).
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Sorted union of every group's values, or None if any group was unrestricted.
|
||||||
|
"""
|
||||||
|
if not all(per_group_values):
|
||||||
|
return None
|
||||||
|
union: set[str] = set()
|
||||||
|
for values in per_group_values:
|
||||||
|
union.update(values or [])
|
||||||
|
return sorted(union)
|
||||||
|
|||||||
Reference in New Issue
Block a user