warranty: show the machine a covered PC drives, with a map on hover

A shopfloor PC is bought, warranted and replaced as a PC, but it is FOUND by
the machine it drives - nobody walks the floor looking for an asset number. The
warranty tables listed the covered asset and left the reader to work out where
that is.

Both tables gain a Machine # column. The payload resolves it by walking the
asset relationship graph in BOTH directions: the canonical edge is
PC --controls--> machine, but a dual-bay pair carries controls on both bays and
hand-made links are not reliably oriented.

Hovering the chip shows the floor map with the machine marked, so the row
answers "where do I go" without opening anything. The blueprint follows the
viewer's theme and the marker is placed from mapx/mapy as a percentage of the
configured map dimensions, since the preview is a few hundred pixels wide
rather than the full plan. It renders only while hovered, so a long table does
not build a blueprint per row.

A machine with no map position still gets its chip and says so, rather than
being dropped: against real data 142 PCs resolve to a machine and 125 of those
are placed, so 17 rows would otherwise have silently lost their number.

The chip is deliberately not a link. /machines/:id is keyed by machineid, not
assetid, and resolving one to the other here would make the warranty plugin
import the machines plugin (ADR-014). Worth noting separately: the existing
assetLink() in these tables already sends machine-type assets to
/machines/<assetid>, which is that same mismatch and predates this change.
This commit is contained in:
cproudlock
2026-08-10 09:41:48 -04:00
parent c0a7655aab
commit 9e271b4c03
4 changed files with 186 additions and 1 deletions

View File

@@ -33,12 +33,57 @@ def _parse_date(value):
return None
def _related_machine(asset):
"""The machine a covered asset is associated with, or None.
A shopfloor PC is bought, warranted and replaced as a PC, but it is FOUND
by the machine it drives - nobody walks the floor looking for an asset
number. So a warranty row for a PC carries the machine's number, and its
map position so the row can point at where to go.
Walks the asset relationship graph in BOTH directions: the canonical edge is
PC --controls--> machine, but a dual-bay pair carries controls on both bays
and hand-made links are not guaranteed to be oriented. Returns the first
active related asset whose type is 'machine'.
"""
from shopdb.api import Asset as CoreAsset
related = []
for rel in (getattr(asset, 'outgoing_relationships', None) or []):
if rel.isactive and rel.targetasset:
related.append(rel.targetasset)
for rel in (getattr(asset, 'incoming_relationships', None) or []):
if rel.isactive and rel.sourceasset:
related.append(rel.sourceasset)
for candidate in related:
typename = candidate.assettype.assettype if candidate.assettype else None
if typename != 'machine':
continue
return {
'assetid': candidate.assetid,
'machinenumber': candidate.assetnumber,
'name': candidate.name,
# Map position for the hover preview. Either may be None: a machine
# that has never been placed on the floor map still has a number
# worth showing, so the caller decides what to do with a missing
# position rather than the row being dropped.
'mapx': candidate.mapx,
'mapy': candidate.mapy,
'locationid': candidate.locationid,
'locationname': (candidate.location.locationname
if candidate.location else None),
}
return None
def _asset_summary(asset):
return {
'assetid': asset.assetid,
'assetnumber': asset.assetnumber,
'name': asset.name,
'assettypename': asset.assettype.assettype if asset.assettype else None,
'machine': _related_machine(asset),
}

View File

@@ -0,0 +1,124 @@
<template>
<span
v-if="machine"
class="mmc"
@mouseenter="open = true"
@mouseleave="open = false"
>
<!-- Deliberately not a link. /machines/:id is keyed by machineid, not
assetid, and resolving one to the other here would make the warranty
plugin import the machines plugin (ADR-014). The number plus the map
is what someone needs to walk to the bay. -->
<span class="mmc-chip">{{ machine.machinenumber || machine.name || '-' }}</span>
<!-- Hover preview: where to walk to. Rendered only while hovered so a
table of 200 rows does not build 200 blueprints. -->
<span v-if="open && hasPosition" class="mmc-pop">
<span class="mmc-pop-title">{{ machine.name || machine.machinenumber }}</span>
<span v-if="machine.locationname" class="mmc-pop-loc">{{ machine.locationname }}</span>
<span class="mmc-map">
<img :src="blueprint" alt="" class="mmc-map-img" />
<span class="mmc-marker" :style="markerStyle"></span>
</span>
</span>
<!-- A machine with a number but no map position still deserves the chip;
say why there is no picture rather than showing an empty box. -->
<span v-else-if="open" class="mmc-pop mmc-pop-plain">
<span class="mmc-pop-title">{{ machine.name || machine.machinenumber }}</span>
<span class="mmc-pop-loc">{{ machine.locationname || 'Not placed on the floor map' }}</span>
</span>
</span>
<span v-else class="muted">-</span>
</template>
<script setup>
import { ref, computed, onMounted } from 'vue'
import { state as mapState, loadMapConfig, blueprintUrlFor } from '@/composables/mapConfig'
const props = defineProps({
machine: { type: Object, default: null }
})
const open = ref(false)
onMounted(() => { loadMapConfig() })
const hasPosition = computed(() =>
props.machine && props.machine.mapx != null && props.machine.mapy != null)
// Follow the viewer's theme the same way the full map does, so the preview is
// not a white rectangle on a dark page.
const blueprint = computed(() => {
const dark = document.documentElement.dataset.theme === 'dark'
|| (!document.documentElement.dataset.theme
&& window.matchMedia('(prefers-color-scheme: dark)').matches)
return blueprintUrlFor(dark ? 'dark' : 'light')
})
// mapx/mapy are pixel coordinates in the blueprint's own space, so they have to
// be expressed as a percentage of its configured dimensions - the preview is a
// few hundred pixels wide, not the full plan.
const markerStyle = computed(() => {
if (!hasPosition.value) return {}
const width = mapState.width || 3300
const height = mapState.height || 2550
return {
left: `${(props.machine.mapx / width) * 100}%`,
top: `${(props.machine.mapy / height) * 100}%`
}
})
</script>
<style scoped>
.mmc { position: relative; display: inline-block; }
.mmc-chip {
display: inline-block;
padding: 0.1rem 0.5rem;
border-radius: 999px;
background: var(--bg);
border: 1px solid var(--border);
color: var(--text);
font-size: 0.8rem;
font-family: ui-monospace, Menlo, Consolas, monospace;
text-decoration: none;
}
.mmc:hover .mmc-chip { border-color: var(--primary); }
.mmc-pop {
position: absolute;
z-index: 40;
left: 0;
top: calc(100% + 0.35rem);
display: flex;
flex-direction: column;
gap: 0.25rem;
padding: 0.5rem;
width: 18rem;
background: var(--bg-card-solid);
border: 1px solid var(--border);
border-radius: 6px;
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}
.mmc-pop-plain { width: 14rem; }
.mmc-pop-title { font-weight: 600; font-size: 0.85rem; color: var(--text); }
.mmc-pop-loc { font-size: 0.78rem; color: var(--text-light); }
.mmc-map { position: relative; display: block; line-height: 0; }
.mmc-map-img {
width: 100%;
height: auto;
border-radius: 4px;
border: 1px solid var(--border);
}
.mmc-marker {
position: absolute;
width: 12px;
height: 12px;
margin: -6px 0 0 -6px;
border-radius: 50%;
background: var(--danger);
border: 2px solid #fff;
box-shadow: 0 0 0 2px var(--danger);
}
</style>

View File

@@ -40,6 +40,7 @@
<th>Service Level</th>
<th>Ends</th>
<th>Covers</th>
<th>Machine #</th>
<th>Actions</th>
</tr>
</thead>
@@ -53,6 +54,12 @@
<span v-if="!w.assets.length" class="muted">-</span>
<router-link v-for="a in w.assets" :key="a.assetid" :to="assetLink(a)" class="asset-chip" :title="a.name || a.assetnumber">{{ a.assetnumber }}</router-link>
</td>
<td>
<template v-for="a in w.assets" :key="`m-${a.assetid}`">
<MachineMapChip v-if="a.machine" :machine="a.machine" />
</template>
<span v-if="!w.assets.some(a => a.machine)" class="muted">-</span>
</td>
<td class="actions">
<button v-if="w.provider !== 'manual'" class="btn btn-secondary btn-sm" @click="refresh(w)">Refresh</button>
<button class="btn btn-secondary btn-sm" @click="openModal(w)">Edit</button>
@@ -163,6 +170,7 @@ import { colorStyle } from '@/utils/colorStyle'
import { warrantyApi, assetsApi, vendorsApi } from '@/api'
import { useToast } from '@/composables/toast'
import { apiError } from '@/utils/apiError'
import MachineMapChip from '../components/MachineMapChip.vue'
const toast = useToast()
const route = useRoute()
@@ -390,7 +398,7 @@ async function refresh(w) {
.asset-search { position: relative; }
.asset-results {
position: absolute; z-index: 10; left: 0; right: 0; margin: 2px 0 0;
padding: 0; list-style: none; background: var(--bg-card);
padding: 0; list-style: none; background: var(--bg-card-solid);
border: 1px solid var(--border); border-radius: 6px; max-height: 200px; overflow-y: auto;
}
.asset-results li { padding: 0.45rem 0.6rem; cursor: pointer; }

View File

@@ -33,6 +33,7 @@
<th>Service Level</th>
<th>Ends</th>
<th>Covers</th>
<th>Machine #</th>
</tr>
</thead>
<tbody>
@@ -44,6 +45,12 @@
<router-link v-for="a in w.assets" :key="a.assetid" :to="assetLink(a)" class="asset-chip">{{ a.assetnumber }}</router-link>
<span v-if="!w.assets.length" class="muted">-</span>
</td>
<td>
<template v-for="a in w.assets" :key="`m-${a.assetid}`">
<MachineMapChip v-if="a.machine" :machine="a.machine" />
</template>
<span v-if="!w.assets.some(a => a.machine)" class="muted">-</span>
</td>
</tr>
</tbody>
</table>
@@ -58,6 +65,7 @@
import { ref, computed, onMounted } from 'vue'
import { warrantyApi } from '@/api'
import EmailReportButton from '@/components/EmailReportButton.vue'
import MachineMapChip from '../components/MachineMapChip.vue'
const loading = ref(true)
const counts = ref({})