Skip to content

Commit aff47a3

Browse files
authored
refactor(operator-trend): collapse hotspot selectors onto a parametrized base (T3-2 phase 6c) (#84)
Third structural collapse: the rebuild and rererestore hotspot selectors share one group/dedup/filter/sort algorithm differing only in the per-tier persistence/churn keys, the just-* mode token, and the holding/sustained status set. Extract _hotspots_base + a _HotspotsTierSpec; both public selectors become thin wrappers. The two tiers had also drifted on defensive coercion (rebuild wrapped score/age/ label in float()/int()/str(); rererestore was bare) -- identical on real numeric data. The base unifies on rebuild's safer coercion; verified byte-identical. Public signatures unchanged -> callers (summaries that read these keys, apply-chain) untouched. Net -72 lines. Verified: differential test (real selectors vs base+spec over targets x all modes, byte-identical both tiers) + composer golden byte-identical to main + full suite (2539 passed). ruff + mypy clean.
1 parent 1625688 commit aff47a3

1 file changed

Lines changed: 101 additions & 173 deletions

File tree

src/operator_trend_closure_forecast_reset_controls.py

Lines changed: 101 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -3544,9 +3544,29 @@ def apply_reset_reentry_rebuild_persistence_and_churn_control(
35443544
}
35453545

35463546

3547-
def closure_forecast_reset_reentry_rebuild_hotspots(
3547+
class _HotspotsTierSpec(NamedTuple):
3548+
"""Tier-specific literals for the reset-reentry hotspot selector.
3549+
3550+
The rebuild / rererestore hotspot selectors share one group/filter/sort
3551+
algorithm and differ only in the per-tier persistence/churn keys, the
3552+
``just-*`` mode token, and the holding/sustained status set.
3553+
"""
3554+
3555+
age_key: str
3556+
persistence_score_key: str
3557+
persistence_status_key: str
3558+
churn_score_key: str
3559+
churn_status_key: str
3560+
persistence_path_key: str
3561+
churn_path_key: str
3562+
just_mode: str
3563+
holding_statuses: frozenset[str]
3564+
3565+
3566+
def _hotspots_base(
35483567
resolution_targets: list[dict[str, Any]],
35493568
*,
3569+
spec: _HotspotsTierSpec,
35503570
mode: str,
35513571
target_class_key: Callable[[dict[str, Any]], str],
35523572
) -> list[dict[str, Any]]:
@@ -3558,91 +3578,118 @@ def closure_forecast_reset_reentry_rebuild_hotspots(
35583578
current = {
35593579
"scope": "class",
35603580
"label": class_key,
3561-
"closure_forecast_reset_reentry_rebuild_age_runs": target.get(
3562-
"closure_forecast_reset_reentry_rebuild_age_runs",
3563-
0,
3564-
),
3565-
"closure_forecast_reset_reentry_rebuild_persistence_score": target.get(
3566-
"closure_forecast_reset_reentry_rebuild_persistence_score",
3567-
0.0,
3568-
),
3569-
"closure_forecast_reset_reentry_rebuild_persistence_status": target.get(
3570-
"closure_forecast_reset_reentry_rebuild_persistence_status",
3571-
"none",
3572-
),
3573-
"closure_forecast_reset_reentry_rebuild_churn_score": target.get(
3574-
"closure_forecast_reset_reentry_rebuild_churn_score",
3575-
0.0,
3576-
),
3577-
"closure_forecast_reset_reentry_rebuild_churn_status": target.get(
3578-
"closure_forecast_reset_reentry_rebuild_churn_status",
3579-
"none",
3580-
),
3581-
"recent_reset_reentry_rebuild_persistence_path": target.get(
3582-
"recent_reset_reentry_rebuild_persistence_path",
3583-
"",
3584-
),
3585-
"recent_reset_reentry_rebuild_churn_path": target.get(
3586-
"recent_reset_reentry_rebuild_churn_path",
3587-
"",
3581+
spec.age_key: target.get(spec.age_key, 0),
3582+
spec.persistence_score_key: target.get(spec.persistence_score_key, 0.0),
3583+
spec.persistence_status_key: target.get(
3584+
spec.persistence_status_key, "none"
35883585
),
3586+
spec.churn_score_key: target.get(spec.churn_score_key, 0.0),
3587+
spec.churn_status_key: target.get(spec.churn_status_key, "none"),
3588+
spec.persistence_path_key: target.get(spec.persistence_path_key, ""),
3589+
spec.churn_path_key: target.get(spec.churn_path_key, ""),
35893590
}
35903591
existing = grouped.get(class_key)
35913592
if existing is None or abs(
3592-
float(current["closure_forecast_reset_reentry_rebuild_persistence_score"] or 0.0)
3593-
) > abs(float(existing["closure_forecast_reset_reentry_rebuild_persistence_score"] or 0.0)):
3593+
float(current[spec.persistence_score_key] or 0.0)
3594+
) > abs(float(existing[spec.persistence_score_key] or 0.0)):
35943595
grouped[class_key] = current
35953596
hotspots = list(grouped.values())
3596-
if mode == "just-rebuilt":
3597+
if mode == spec.just_mode:
35973598
hotspots = [
35983599
item
35993600
for item in hotspots
3600-
if item.get("closure_forecast_reset_reentry_rebuild_persistence_status")
3601-
== "just-rebuilt"
3601+
if item.get(spec.persistence_status_key) == spec.just_mode
36023602
]
36033603
hotspots.sort(
36043604
key=lambda item: (
3605-
-int(item.get("closure_forecast_reset_reentry_rebuild_age_runs", 0) or 0),
3606-
-abs(float(item.get("closure_forecast_reset_reentry_rebuild_persistence_score", 0.0) or 0.0)),
3605+
-int(item.get(spec.age_key, 0) or 0),
3606+
-abs(float(item.get(spec.persistence_score_key, 0.0) or 0.0)),
36073607
str(item.get("label", "")),
36083608
)
36093609
)
36103610
elif mode == "holding":
36113611
hotspots = [
36123612
item
36133613
for item in hotspots
3614-
if item.get("closure_forecast_reset_reentry_rebuild_persistence_status")
3615-
in {
3616-
"holding-confirmation-rebuild",
3617-
"holding-clearance-rebuild",
3618-
"sustained-confirmation-rebuild",
3619-
"sustained-clearance-rebuild",
3620-
}
3614+
if item.get(spec.persistence_status_key) in spec.holding_statuses
36213615
]
36223616
hotspots.sort(
36233617
key=lambda item: (
3624-
-int(item.get("closure_forecast_reset_reentry_rebuild_age_runs", 0) or 0),
3625-
-abs(float(item.get("closure_forecast_reset_reentry_rebuild_persistence_score", 0.0) or 0.0)),
3618+
-int(item.get(spec.age_key, 0) or 0),
3619+
-abs(float(item.get(spec.persistence_score_key, 0.0) or 0.0)),
36263620
str(item.get("label", "")),
36273621
)
36283622
)
36293623
else:
36303624
hotspots = [
36313625
item
36323626
for item in hotspots
3633-
if item.get("closure_forecast_reset_reentry_rebuild_churn_status")
3634-
in {"watch", "churn", "blocked"}
3627+
if item.get(spec.churn_status_key) in {"watch", "churn", "blocked"}
36353628
]
36363629
hotspots.sort(
36373630
key=lambda item: (
3638-
-float(item.get("closure_forecast_reset_reentry_rebuild_churn_score", 0.0) or 0.0),
3639-
-int(item.get("closure_forecast_reset_reentry_rebuild_age_runs", 0) or 0),
3631+
-float(item.get(spec.churn_score_key, 0.0) or 0.0),
3632+
-int(item.get(spec.age_key, 0) or 0),
36403633
str(item.get("label", "")),
36413634
)
36423635
)
36433636
return hotspots[:5]
36443637

36453638

3639+
_REBUILD_HOTSPOTS_SPEC = _HotspotsTierSpec(
3640+
age_key="closure_forecast_reset_reentry_rebuild_age_runs",
3641+
persistence_score_key="closure_forecast_reset_reentry_rebuild_persistence_score",
3642+
persistence_status_key="closure_forecast_reset_reentry_rebuild_persistence_status",
3643+
churn_score_key="closure_forecast_reset_reentry_rebuild_churn_score",
3644+
churn_status_key="closure_forecast_reset_reentry_rebuild_churn_status",
3645+
persistence_path_key="recent_reset_reentry_rebuild_persistence_path",
3646+
churn_path_key="recent_reset_reentry_rebuild_churn_path",
3647+
just_mode="just-rebuilt",
3648+
holding_statuses=frozenset(
3649+
{
3650+
"holding-confirmation-rebuild",
3651+
"holding-clearance-rebuild",
3652+
"sustained-confirmation-rebuild",
3653+
"sustained-clearance-rebuild",
3654+
}
3655+
),
3656+
)
3657+
3658+
3659+
_RERERESTORE_HOTSPOTS_SPEC = _HotspotsTierSpec(
3660+
age_key="closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_age_runs",
3661+
persistence_score_key="closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_persistence_score",
3662+
persistence_status_key="closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_persistence_status",
3663+
churn_score_key="closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_churn_score",
3664+
churn_status_key="closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_churn_status",
3665+
persistence_path_key="recent_reset_reentry_rebuild_reentry_restore_rererestore_persistence_path",
3666+
churn_path_key="recent_reset_reentry_rebuild_reentry_restore_rererestore_churn_path",
3667+
just_mode="just-rererestored",
3668+
holding_statuses=frozenset(
3669+
{
3670+
"holding-confirmation-rebuild-reentry-rererestore",
3671+
"holding-clearance-rebuild-reentry-rererestore",
3672+
"sustained-confirmation-rebuild-reentry-rererestore",
3673+
"sustained-clearance-rebuild-reentry-rererestore",
3674+
}
3675+
),
3676+
)
3677+
3678+
3679+
def closure_forecast_reset_reentry_rebuild_hotspots(
3680+
resolution_targets: list[dict[str, Any]],
3681+
*,
3682+
mode: str,
3683+
target_class_key: Callable[[dict[str, Any]], str],
3684+
) -> list[dict[str, Any]]:
3685+
return _hotspots_base(
3686+
resolution_targets,
3687+
spec=_REBUILD_HOTSPOTS_SPEC,
3688+
mode=mode,
3689+
target_class_key=target_class_key,
3690+
)
3691+
3692+
36463693
def closure_forecast_reset_reentry_rebuild_persistence_summary(
36473694
primary_target: dict[str, Any],
36483695
just_rebuilt_hotspots: list[dict[str, Any]],
@@ -6184,131 +6231,12 @@ def closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_hotspots(
61846231
mode: str,
61856232
target_class_key: Callable[[dict[str, Any]], str],
61866233
) -> list[dict[str, Any]]:
6187-
grouped: dict[str, dict[str, Any]] = {}
6188-
for target in resolution_targets:
6189-
class_key = target_class_key(target)
6190-
if not class_key:
6191-
continue
6192-
current = {
6193-
"scope": "class",
6194-
"label": class_key,
6195-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_age_runs": target.get(
6196-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_age_runs",
6197-
0,
6198-
),
6199-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_persistence_score": target.get(
6200-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_persistence_score",
6201-
0.0,
6202-
),
6203-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_persistence_status": target.get(
6204-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_persistence_status",
6205-
"none",
6206-
),
6207-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_churn_score": target.get(
6208-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_churn_score",
6209-
0.0,
6210-
),
6211-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_churn_status": target.get(
6212-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_churn_status",
6213-
"none",
6214-
),
6215-
"recent_reset_reentry_rebuild_reentry_restore_rererestore_persistence_path": target.get(
6216-
"recent_reset_reentry_rebuild_reentry_restore_rererestore_persistence_path",
6217-
"",
6218-
),
6219-
"recent_reset_reentry_rebuild_reentry_restore_rererestore_churn_path": target.get(
6220-
"recent_reset_reentry_rebuild_reentry_restore_rererestore_churn_path",
6221-
"",
6222-
),
6223-
}
6224-
existing = grouped.get(class_key)
6225-
if existing is None or abs(
6226-
current[
6227-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_persistence_score"
6228-
]
6229-
) > abs(
6230-
existing[
6231-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_persistence_score"
6232-
]
6233-
):
6234-
grouped[class_key] = current
6235-
hotspots = list(grouped.values())
6236-
if mode == "just-rererestored":
6237-
hotspots = [
6238-
item
6239-
for item in hotspots
6240-
if item.get(
6241-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_persistence_status"
6242-
)
6243-
== "just-rererestored"
6244-
]
6245-
hotspots.sort(
6246-
key=lambda item: (
6247-
-item.get(
6248-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_age_runs",
6249-
0,
6250-
),
6251-
-abs(
6252-
item.get(
6253-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_persistence_score",
6254-
0.0,
6255-
)
6256-
),
6257-
item.get("label", ""),
6258-
)
6259-
)
6260-
elif mode == "holding":
6261-
hotspots = [
6262-
item
6263-
for item in hotspots
6264-
if item.get(
6265-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_persistence_status"
6266-
)
6267-
in {
6268-
"holding-confirmation-rebuild-reentry-rererestore",
6269-
"holding-clearance-rebuild-reentry-rererestore",
6270-
"sustained-confirmation-rebuild-reentry-rererestore",
6271-
"sustained-clearance-rebuild-reentry-rererestore",
6272-
}
6273-
]
6274-
hotspots.sort(
6275-
key=lambda item: (
6276-
-item.get(
6277-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_age_runs",
6278-
0,
6279-
),
6280-
-abs(
6281-
item.get(
6282-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_persistence_score",
6283-
0.0,
6284-
)
6285-
),
6286-
item.get("label", ""),
6287-
)
6288-
)
6289-
else:
6290-
hotspots = [
6291-
item
6292-
for item in hotspots
6293-
if item.get(
6294-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_churn_status"
6295-
)
6296-
in {"watch", "churn", "blocked"}
6297-
]
6298-
hotspots.sort(
6299-
key=lambda item: (
6300-
-item.get(
6301-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_churn_score",
6302-
0.0,
6303-
),
6304-
-item.get(
6305-
"closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_age_runs",
6306-
0,
6307-
),
6308-
item.get("label", ""),
6309-
)
6310-
)
6311-
return hotspots[:5]
6234+
return _hotspots_base(
6235+
resolution_targets,
6236+
spec=_RERERESTORE_HOTSPOTS_SPEC,
6237+
mode=mode,
6238+
target_class_key=target_class_key,
6239+
)
63126240

63136241

63146242
def closure_forecast_reset_reentry_rebuild_reentry_restore_rererestore_persistence_summary(

0 commit comments

Comments
 (0)