|
4 | 4 | from collections import defaultdict |
5 | 5 | from collections.abc import Collection, Iterator, Mapping |
6 | 6 | from dataclasses import dataclass, field |
| 7 | +from functools import cached_property |
7 | 8 | from itertools import groupby |
8 | 9 | from typing import Any |
9 | 10 |
|
@@ -45,6 +46,13 @@ def campaigns_grouped_by_condition_name( |
45 | 46 | ): |
46 | 47 | yield condition_name, list(campaign_group) |
47 | 48 |
|
| 49 | + @cached_property |
| 50 | + def person_cohorts(self)-> set[str]: |
| 51 | + cohorts_row: Mapping[str, dict[str, dict[str, dict[str, Any]]]] = next( |
| 52 | + (row for row in self.person_data if row.get("ATTRIBUTE_TYPE") == "COHORTS"), {} |
| 53 | + ) |
| 54 | + return set(cohorts_row.get("COHORT_MAP", {}).get("cohorts", {}).get("M", {}).keys()) |
| 55 | + |
48 | 56 | def evaluate_eligibility(self) -> eligibility.EligibilityStatus: |
49 | 57 | """Iterates over campaign groups, evaluates eligibility, and returns a consolidated status.""" |
50 | 58 |
|
@@ -78,12 +86,7 @@ def check_base_eligibility(self, iteration: rules.Iteration) -> bool: |
78 | 86 | } |
79 | 87 | if magic_cohort in iteration_cohorts: |
80 | 88 | return True |
81 | | - |
82 | | - cohorts_row: Mapping[str, dict[str, dict[str, dict[str, Any]]]] = next( |
83 | | - (row for row in self.person_data if row.get("ATTRIBUTE_TYPE") == "COHORTS"), {} |
84 | | - ) |
85 | | - person_cohorts: set[str] = set(cohorts_row.get("COHORT_MAP", {}).get("cohorts", {}).get("M", {}).keys()) |
86 | | - return bool(iteration_cohorts & person_cohorts) |
| 89 | + return bool(iteration_cohorts & self.person_cohorts) |
87 | 90 |
|
88 | 91 | def evaluate_eligibility_by_iteration_rules( |
89 | 92 | self, campaign_group: list[rules.CampaignConfig] |
@@ -123,8 +126,12 @@ def evaluate_priority_group( |
123 | 126 | worst_status_so_far_for_condition: eligibility.Status, |
124 | 127 | ) -> tuple[eligibility.Status, list[eligibility.Reason], list[eligibility.Reason]]: |
125 | 128 | exclusion_reasons, actionable_reasons = [], [] |
| 129 | + |
| 130 | + |
126 | 131 | exclude_capable_rules = [ |
127 | | - ir for ir in iteration_rule_group if ir.type in (rules.RuleType.filter, rules.RuleType.suppression) |
| 132 | + ir for ir in iteration_rule_group |
| 133 | + if ir.type in (rules.RuleType.filter, rules.RuleType.suppression) |
| 134 | + and (ir.cohort_label is None or ir.cohort_label in self.person_cohorts) |
128 | 135 | ] |
129 | 136 |
|
130 | 137 | best_status = eligibility.Status.not_eligible if exclude_capable_rules else eligibility.Status.actionable |
|
0 commit comments