dashboard: printer supplies, expiring warranties, mis-numbered bays
Some checks failed
CI / backend (push) Failing after 8s
CI / naming (push) Successful in 2s
CI / frontend (push) Successful in 9s
CI / migrations-mysql (push) Failing after 7s

Wave one complete. Three cards, no new data and no migrations.

Printer supplies reuses the existing low-supplies query and its five-minute
cache; a Zabbix round-trip per printer on every dashboard load would make this
the slowest page in the app. One row per printer listing every depleted
cartridge, criticals first - a row per cartridge would report one printer three
times and read as three problems, and showing only the worst class would hide a
low cartridge behind a critical one on the same machine when whoever walks out
there wants to carry both.

While there: the low-supplies REPORT itself was including healthy cartridges. A
printer with one empty black and three full colour ones listed all four, so the
reader had to find the problem inside the row. It now lists only what needs
replacing, and the test that asserted the old behaviour now asserts the new.

Expiring warranties keeps already-expired entries on the list rather than
dropping them the day they lapse, which is how they get missed. Horizon is
warranty_expiringdays, default 90, because that suits a site budgeting
quarterly and nobody else.

Mis-numbered bays promotes check-shared-machines out of a CLI command nobody
will remember to run - it found seven bays that had been wrong for weeks. It
reports only numbers with NO child assets, so part markers legitimately sharing
an operation stay silent: that distinction is the whole card, and without it it
would list correct data beside faults and be ignored.

Printers also loses its dead component-named widget; notifications, network and
machines still have theirs.
This commit is contained in:
cproudlock
2026-08-11 14:13:26 -04:00
parent 6c975a107c
commit 7151b68bdd
9 changed files with 339 additions and 13 deletions

View File

@@ -1005,12 +1005,17 @@ def _get_low_supplies_data():
model_number = model.modelnumber if model else None
modelnumberid = model.modelnumberid if model else None
# ONLY the supplies that need attention. A printer reporting one empty
# black cartridge alongside three full colour ones was listing all four,
# so the reader had to find the problem inside the row rather than being
# shown it. The whole report exists to answer "what needs replacing".
annotated = []
has_low = False
for s in supplies:
item = _annotate_supply(s, vendor_name, modelnumberid)
if item['status'] != 'ok':
has_low = True
if item['status'] == 'ok':
continue
has_low = True
annotated.append(item)
if has_low:
@@ -1388,3 +1393,45 @@ def delete_model_supply(modelsupplyid: int):
db.session.delete(supply)
db.session.commit()
return success_response(message='Supply deleted')
@printers_asset_bp.route('/dashboard/supplies', methods=['GET'])
@jwt_required()
@require_permission('printers.view')
def dashboard_supplies():
"""Printers needing a cartridge, flattened to one row per printer.
Reuses the existing low-supplies query and its five-minute cache, so the
card costs nothing extra: a Zabbix round-trip per printer on every dashboard
load would make this the slowest page in the app.
Critical first, then low. A printer with several depleted cartridges appears
once, listing them - a row per cartridge would report one printer three
times and read as three problems.
"""
data = _get_low_supplies_data()
rows = []
for printer in data.get('printers', []):
criticals = [s for s in printer['supplies'] if s['status'] == 'critical']
lows = [s for s in printer['supplies'] if s['status'] == 'low']
if not criticals and not lows:
continue
# Every depleted supply, criticals first. Listing only the worst class
# would hide a low cartridge behind a critical one on the same printer,
# and whoever walks out there wants to carry both.
worst = criticals + lows
rows.append({
'printerid': printer['printerid'],
'printername': printer['printername'] or printer['assetnumber'],
'location': printer['location'],
'status': 'critical' if criticals else 'low',
'supplies': ', '.join(
'{} {}%'.format(s.get('name') or s.get('type') or 'supply',
s.get('percent'))
for s in worst),
'iscritical': bool(criticals),
})
rows.sort(key=lambda r: (not r['iscritical'], r['printername']))
return success_response(rows)

View File

@@ -287,14 +287,28 @@ class PrintersPlugin(BasePlugin):
return [printerscli]
def get_dashboard_widgets(self) -> List[Dict]:
"""Return dashboard widget definitions."""
"""Dashboard card: printers needing a cartridge.
Replaces a declaration naming a component nobody wrote. Reuses the
low-supplies query and its cache - a Zabbix round-trip per printer on
every dashboard load would make this the slowest page in the app.
"""
return [
{
'name': 'Printer Status',
'component': 'PrinterStatusWidget',
'endpoint': '/api/printers/dashboard/summary',
'size': 'medium',
'position': 10,
'id': 'printers-supplies',
'title': 'Printer supplies',
'endpoint': '/api/printers/dashboard/supplies',
'render': 'exceptions',
'severity': 'warning',
'permission': 'printers.view',
'empty': 'hide',
'position': 40,
'map': {
'title': 'printername',
'detail': 'supplies',
'meta': [{'key': 'location'}, {'key': 'status'}],
'link': '/printers/{printerid}',
},
},
]