Files
shopdb-flask/shopdb/core/models/mapsnapshot.py
cproudlock 3324dbd91e
Some checks failed
CI / backend (push) Failing after 9s
CI / naming (push) Successful in 2s
CI / frontend (push) Successful in 10s
CI / migrations-mysql (push) Failing after 7s
Buildings and levels for the floor map, and make every identifier searchable
The map was one picture of one floor. A second floor was added, the blueprint
changed size, and machines moved, so a position now records WHICH DRAWING its
coordinates belong to.

Buildings and levels (ADR-017). Each level owns its blueprint per theme and its
own native pixel size; assets.mapx/mapy are pixels of assets.levelid, not of the
site. A position whose level is unknown renders "level unknown" and is never
drawn on the default level, because a marker on the wrong floor plan looks
entirely correct while pointing at the wrong place.

Repositioning in bulk: filter by unplaced, needs-review or level, search, place,
confirm. Landmark recalibration solves the transform PER AXIS from landmark
pairs and never from image dimensions - the canvas grew taller without
rescaling, so a dimension-derived scale would stretch Y by 1.57 and be wrong
everywhere. It defaults to a dry run, reports what would land off the drawing,
snapshots before applying, and clears mapverifiedat because a transform is a
guess awaiting review. Snapshots restore, including the level and the review
state, and a restore snapshots first so an undo is undoable.

Search: gaugelabreference was matched only for measuring tools and
maintenancereference was matched nowhere at all, for any asset type, while
Settings happily offers both identifiers on machines and PCs. A tag an operator
is told to record has to be findable or it is a write-only field. USB devices
and printed items were unreachable from search entirely - neither is an asset,
so the generic asset search could not see them and no searcher existed; they
now match on serial, asset tag, label, bin code and gage-lab tag, honouring
isactive, with Settings toggles and result labels to match.

The retired-application rule was half a rule: GET /api/knowledgebase hid
articles whose topic application is retired while global search still returned
them and printed the retired application as the subject. A filter is only real
if every path that reaches the row applies it.

Contract to 0.20.0 (additive): Asset gained levelid and mapverifiedat, Location
gained levelid, and resolve_asset_position returns the levelid belonging to
whichever source supplied the coordinates. The five plugins that write a map
position are re-pinned. The install-list text format gained levelid as a NINTH
field, appended, because the shipped Pascal installer reads fields 0-7 by index.

That installer still compiles in one drawing's dimensions and bundles one
blueprint, so its map is accurate for the default level only; /api/maplevels is
deliberately unauthenticated so it can read both at runtime once rebuilt.
Recorded in PRINTER-INSTALLER.md section 6 along with the other known gaps.

Migration 7d33 converts an existing single-map site into one building and one
default level carrying the old map_* settings, then assigns every placed asset
and location to it. Nothing moves on screen. Old settings rows are kept so a
rollback still finds them. Verified end to end on MySQL 5.6 from a
production-shaped database.
2026-08-17 12:55:51 -04:00

77 lines
2.9 KiB
Python

"""A saved set of marker positions, so a bulk change can be undone.
Positions had no history. A landmark transform rewrites every marker on a level
in one statement, and without a way back the honest instruction would be "take a
database backup first" - which nobody does before clicking a button in a UI, so
in practice the feature would either not be used or be used once, badly.
One row holds the whole set as JSON rather than a row per asset. A snapshot is
read back whole or not at all, so restore stays a single statement, and there is
no orphan-child case to reason about.
"""
import json
from shopdb.extensions import db
from .base import BaseModel
class MapPositionSnapshot(BaseModel):
__tablename__ = 'mappositionsnapshots'
snapshotid = db.Column(db.Integer, primary_key=True)
# Which level the operation targeted. Nullable because a snapshot may span
# levels (moving assets between them), and then no single level owns it.
levelid = db.Column(db.Integer, nullable=True)
reason = db.Column(db.String(255), nullable=True)
assetcount = db.Column(db.Integer, nullable=False, default=0)
positionsjson = db.Column(db.Text, nullable=False)
# Set when this snapshot has been restored, so the history reads as what
# happened rather than as a list of identical-looking saves.
restoredat = db.Column(db.DateTime, nullable=True)
createdby = db.Column(db.String(100), nullable=True)
def __repr__(self):
return f"<MapPositionSnapshot {self.snapshotid} ({self.assetcount})>"
@property
def positions(self):
try:
return json.loads(self.positionsjson or '[]')
except ValueError:
return []
def to_dict(self):
"""Metadata only. The positions themselves are large and nobody browsing
a list of snapshots wants them."""
data = super().to_dict()
data.pop('positionsjson', None)
return data
@classmethod
def capture(cls, assets, reason, levelid=None, createdby=None):
"""Record the CURRENT positions of these assets, before they change.
Includes levelid and mapverifiedat, not just the coordinates: a restore
has to put a marker back on the level it was on and with the review state
it had, or undo would silently mark reviewed work as unreviewed.
"""
rows = [{
'assetid': asset.assetid,
'mapx': asset.mapx,
'mapy': asset.mapy,
'levelid': asset.levelid,
'mapverifiedat': asset.mapverifiedat.isoformat()
if asset.mapverifiedat else None,
} for asset in assets]
snapshot = cls(
levelid=levelid,
reason=reason,
assetcount=len(rows),
positionsjson=json.dumps(rows, separators=(',', ':')),
createdby=createdby,
)
db.session.add(snapshot)
db.session.flush()
return snapshot