Files
shopdb-flask/shopdb/core/services/dualpath.py
cproudlock b130ef43f3
All checks were successful
CI / backend (push) Successful in 1m13s
CI / naming (push) Successful in 1s
CI / frontend (push) Successful in 7s
Add the dualpath-as-single-machine site toggle
Most facilities consider a Dualpath pair one physical dual-bay machine.
New site setting dualpath_single_machine (default on): the machines
list, dashboard counts, machines-by-type report, and floor map collapse
each pair to its primary bay (lower assetnumber), with combined
2007 / 2008 labels; pagination totals stay honest. Detail pages remain
per-bay and always show a dual-bay sibling banner linking the partner.
Pair resolution lives in core services and joins the plugin contract
surface (0.8.0 -> 0.9.0).

On the WJ dataset: 31 pairs collapse, machine counts 262 -> 231, map
470 assets. Toggle verified live in both states, left on.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-12 06:23:39 -04:00

117 lines
4.5 KiB
Python

"""Dualpath single-machine collapse resolution.
A Dualpath relationship pair is ONE physical dual-bay machine (single
controller, one bay-selector switch) recorded as two asset rows. Most
facilities want lists, counts, and the map to show that pair as a single
machine; the data model always keeps both rows. This module resolves the
pairs so the consumers (machines list, dashboard/report counts, floor map)
can collapse them, and is exposed via the plugin contract surface
(shopdb.api) so the machines plugin can reach it contract-purely.
PRIMARY = the pair member with the lower natural-sort assetnumber; the other
member is SECONDARY and is the one hidden when collapsing. Pairs are derived
from active assetrelationships rows whose type is named 'Dualpath' (either
direction; mirrored rows in both directions collapse to one pair). Only pairs
where BOTH assets are active are resolved.
"""
import re
from collections import namedtuple
from shopdb.extensions import db
from shopdb.core.models import Asset, AssetRelationship, RelationshipType
# secondaryassetids: set of the non-primary bay asset ids (hide when collapsing)
# partnerbyasset: {assetid -> {'assetid', 'assetnumber'}} for EVERY pair member,
# primary and secondary alike, so detail pages can show a sibling
# banner from whichever bay you land on.
DualpathCollapse = namedtuple('DualpathCollapse', ['secondaryassetids', 'partnerbyasset'])
DUALPATH_TYPE_NAME = 'Dualpath'
def _naturalkey(assetnumber):
# split into digit/non-digit chunks so 2007 sorts before 2008 and before 10a
parts = re.split(r'(\d+)', assetnumber or '')
return [int(p) if p.isdigit() else p.lower() for p in parts]
def resolve_dualpath_pairs():
"""Resolve active Dualpath pairs into a DualpathCollapse.
Direction-blind and dedup-safe: rows stored in either direction (or both)
for the same two assets collapse to one pair. Ignores the site toggle;
callers gate on dualpath_single_machine_enabled() where the collapse should
only apply when the setting is on (the detail-page banner shows always).
"""
reltype = RelationshipType.query.filter_by(
relationshiptype=DUALPATH_TYPE_NAME).first()
if not reltype:
return DualpathCollapse(set(), {})
rows = AssetRelationship.query.filter(
AssetRelationship.relationshiptypeid == reltype.relationshiptypeid,
AssetRelationship.isactive == True,
).all()
if not rows:
return DualpathCollapse(set(), {})
# batch-load the involved assets (assetnumber + active flag) in one query
involved = set()
for row in rows:
involved.add(row.sourceassetid)
involved.add(row.targetassetid)
assetbyid = {
a.assetid: a
for a in Asset.query.filter(Asset.assetid.in_(involved)).all()
}
secondaryassetids = set()
partnerbyasset = {}
seenpairs = set()
for row in rows:
aid, bid = row.sourceassetid, row.targetassetid
if aid == bid:
continue # defensive: no self-pairs
pairkey = frozenset((aid, bid))
if pairkey in seenpairs:
continue # mirrored row already handled
seenpairs.add(pairkey)
aone = assetbyid.get(aid)
atwo = assetbyid.get(bid)
# collapse only affects visible/counted (active) assets
if not aone or not atwo or not aone.isactive or not atwo.isactive:
continue
# PRIMARY = lower natural-sort assetnumber; SECONDARY is the other bay
if _naturalkey(aone.assetnumber) <= _naturalkey(atwo.assetnumber):
primary, secondary = aone, atwo
else:
primary, secondary = atwo, aone
secondaryassetids.add(secondary.assetid)
partnerbyasset[primary.assetid] = {
'assetid': secondary.assetid,
'assetnumber': secondary.assetnumber,
}
partnerbyasset[secondary.assetid] = {
'assetid': primary.assetid,
'assetnumber': primary.assetnumber,
}
return DualpathCollapse(secondaryassetids, partnerbyasset)
def dualpath_single_machine_enabled():
"""True when the site treats Dualpath pairs as one machine (default true).
Reads the cached settings; the row is absent on an un-seeded site, in which
case the default (true - 'most places' consider a dual-bay pair one machine)
applies.
"""
# imported here to avoid a settings<->api import cycle at module load
from shopdb.core.api.settings import get_cached_settings
settings = get_cached_settings()
return bool(settings.get('dualpath_single_machine', True))