Buildings and levels for the floor map, and make every identifier searchable
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

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.
This commit is contained in:
cproudlock
2026-08-17 12:55:51 -04:00
parent 7d9a54ca0f
commit 3324dbd91e
60 changed files with 5313 additions and 895 deletions

View File

@@ -0,0 +1,109 @@
"""Building + MapLevel models: which drawing renders an asset, at what size.
See ADR-017. A site used to have one floor map, described by four settings, and
`assets.mapx`/`mapy` were pixels in that one image. A second level and a likely
second building make that a table rather than a setting.
The alternative - stacking levels on one tall canvas - was rejected because it
turns "which level is this on" into `mapy > 2550`: an inference over a magic
number that changes whenever the drawing is re-exported.
"""
from shopdb.extensions import db
from .base import BaseModel
class Building(BaseModel):
"""A building at this site. Groups levels; holds nothing a level needs.
Separate from Location deliberately (ADR-017): a Location answers which
operation owns an asset, a building groups the drawings it appears on.
"""
__tablename__ = 'buildings'
buildingid = db.Column(db.Integer, primary_key=True)
buildingname = db.Column(db.String(100), nullable=False, unique=True)
# Display order. Buildings have no natural ordering and their names are not
# reliably ordinal ('Main', 'Annex', 'Building 2'), so the order is stated.
sortorder = db.Column(db.Integer, nullable=False, default=0)
levels = db.relationship(
'MapLevel', back_populates='building',
order_by='MapLevel.sortorder', cascade='all, delete-orphan')
def __repr__(self):
return f"<Building {self.buildingname}>"
def to_dict(self):
data = super().to_dict()
data['levels'] = [level.to_dict() for level in self.levels
if level.isactive]
return data
class MapLevel(BaseModel):
"""One drawing: a level of a building, with its own blueprint and size.
WHY THE DIMENSIONS LIVE HERE. They were site-wide settings, which cannot
express a mezzanine drawn at a different scale from the floor below it, and
certainly not a second building. `mapx`/`mapy` are absolute pixels in THIS
level's coordinate space, so a level without its own dimensions cannot place
a marker correctly.
Name and order are separate columns on purpose: levels are not reliably
numbered (basement, ground, mezzanine, roof), and `sortorder` gives
adjacency and up/down navigation without pretending the names are ordinal.
It also lets a mezzanine be inserted later without renumbering anything.
"""
__tablename__ = 'maplevels'
levelid = db.Column(db.Integer, primary_key=True)
buildingid = db.Column(
db.Integer, db.ForeignKey('buildings.buildingid'), nullable=False,
index=True)
levelname = db.Column(db.String(100), nullable=False)
sortorder = db.Column(db.Integer, nullable=False, default=0)
# Both themes, because the map renders in whichever the viewer is using and
# a light-on-white blueprint is unreadable in dark mode. Either may be
# blank; the renderer falls back to the other rather than to nothing.
blueprintlight = db.Column(db.String(255), nullable=True)
blueprintdark = db.Column(db.String(255), nullable=True)
# Native pixel size of the blueprint. Positions are absolute pixels in this
# space (ADR-017), so these are what a marker's coordinates mean.
mapwidth = db.Column(db.Integer, nullable=False, default=3300)
mapheight = db.Column(db.Integer, nullable=False, default=2550)
# Exactly one level carries this. It is where an asset with no level lands,
# and what the map opens on. Enforced in the API rather than by a constraint,
# because "exactly one" across rows is not a column-level rule.
isdefault = db.Column(db.Boolean, nullable=False, default=False)
building = db.relationship('Building', back_populates='levels')
__table_args__ = (
db.UniqueConstraint('buildingid', 'levelname',
name='uq_maplevel_building_name'),
)
def __repr__(self):
return f"<MapLevel {self.levelname}>"
def to_dict(self):
data = super().to_dict()
data['buildingname'] = self.building.buildingname if self.building else None
return data
@classmethod
def default_level(cls):
"""The default level, or the lowest-sorted one if none is marked.
Never returns None on a seeded database: the migration that created this
table also created one level from the settings it replaced.
"""
level = cls.query.filter_by(isdefault=True, isactive=True).first()
if level is not None:
return level
return (cls.query.filter_by(isactive=True)
.order_by(cls.sortorder, cls.levelid).first())