Reciprocal controls links, dead chip links, and a resumable legacy import
The machine page now shows the PC that controls it. The endpoint was correct all along and returned both directions; AssetTriageBand discarded every peer whose type was not 'machine' and was mounted only on the PC page. The counterpart type, label and relationship types are now props. Fixed while adopting it more widely: every band chip linked to /assets/<id>, and there is no such route. The resolver that AssetRelationships.vue kept privately is now frontend/src/utils/assetRoute.js, used by both - and its type lookup is case-insensitive, because seed data capitalises the type names while this site's database does not. The legacy import is now safe to resume. The 409-resolve asked for per_page=10000 against a server that clamps to 100, so it silently searched the first 100 rows and dropped links past that on every re-run. The crosswalk is now bound to the database it describes, so one carried across a rebuild is refused rather than attaching rows to the wrong assets. Source database names are resolved from the environment, so the loader can be aimed without editing it. Full suite 2178 passed, frontend 280 passed.
This commit is contained in:
@@ -186,6 +186,7 @@
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { assetRoute } from '@/utils/assetRoute'
|
||||
import { ref, computed, onMounted, watch } from 'vue'
|
||||
import { Cog, Monitor, Printer, Globe, Package, Ruler } from 'lucide-vue-next'
|
||||
import { assetsApi, relationshipTypesApi } from '../api'
|
||||
@@ -472,28 +473,7 @@ function getAssetIcon(assettype) {
|
||||
return icons[assettype] || Package
|
||||
}
|
||||
|
||||
function getAssetRoute(asset) {
|
||||
if (!asset) return '#'
|
||||
|
||||
const routeMap = {
|
||||
'machine': '/machines',
|
||||
'computer': '/pcs',
|
||||
'printer': '/printers',
|
||||
'network_device': '/network',
|
||||
'measuring_tool': '/measuringtools'
|
||||
}
|
||||
|
||||
// serializer sends assettypename; old shape used assettype
|
||||
const typename = asset.assettypename || asset.assettype
|
||||
const basePath = routeMap[typename] || '/assets'
|
||||
|
||||
// pluginid is the extension-table id the detail routes key on
|
||||
const id = asset.pluginid
|
||||
|| asset.typedata?.networkdeviceid
|
||||
|| asset.typedata?.machineid
|
||||
|| asset.assetid
|
||||
return `${basePath}/${id}`
|
||||
}
|
||||
const getAssetRoute = assetRoute
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
@@ -8,13 +8,13 @@
|
||||
</div>
|
||||
|
||||
<div v-if="machines.length" class="triage-item">
|
||||
<span class="triage-label">{{ machines.length === 1 ? 'Serves machine' : 'Serves machines' }}</span>
|
||||
<span class="triage-label">{{ counterpartheading }}</span>
|
||||
<span class="triage-values">
|
||||
<router-link
|
||||
v-for="machine in machines"
|
||||
:key="machine.assetid"
|
||||
class="triage-chip link"
|
||||
:to="`/assets/${machine.assetid}`"
|
||||
:to="assetRoute(machine)"
|
||||
>{{ machine.assetnumber || machine.name }}</router-link>
|
||||
</span>
|
||||
</div>
|
||||
@@ -63,6 +63,7 @@
|
||||
*/
|
||||
import { ref, computed, onMounted, watch, useSlots } from 'vue'
|
||||
import api from '@/api'
|
||||
import { assetRoute } from '@/utils/assetRoute'
|
||||
|
||||
const props = defineProps({
|
||||
assetid: { type: Number, required: true },
|
||||
@@ -71,11 +72,30 @@ const props = defineProps({
|
||||
// Shape: { level: 'ok'|'warning'|'critical'|'unknown', label: 'Reporting, 4m ago' }
|
||||
liveness: { type: Object, default: null },
|
||||
accessmethods: { type: Array, default: () => [] },
|
||||
locationname: { type: String, default: '' }
|
||||
locationname: { type: String, default: '' },
|
||||
// Which side of a 'controls' edge this page wants to see, by asset type
|
||||
// name. A PC page wants the machines it serves; a machine page wants the
|
||||
// PCs that drive it. Defaults to 'machine' so the PC page - the only
|
||||
// caller when this was written - keeps its exact behaviour.
|
||||
counterparttype: { type: String, default: 'machine' },
|
||||
// Overrides the heading. Empty keeps the shop-floor phrasing that earned
|
||||
// its place: a PC matters because a machine stops without it.
|
||||
counterpartlabel: { type: String, default: '' },
|
||||
// Which relationship types link this asset to its counterpart. Not every
|
||||
// pairing is 'controls': a PC controls a machine, but a machine reaches a
|
||||
// printer through usesprinter and defaultprinter. Hardcoding 'controls'
|
||||
// left the band blank on the printer page, which is the one page where the
|
||||
// link is not obvious from the asset itself.
|
||||
relationshiptypes: { type: Array, default: () => ['controls'] }
|
||||
})
|
||||
|
||||
const machines = ref([])
|
||||
|
||||
const counterpartheading = computed(() => {
|
||||
if (props.counterpartlabel) return props.counterpartlabel
|
||||
return machines.value.length === 1 ? 'Serves machine' : 'Serves machines'
|
||||
})
|
||||
|
||||
const slots = useSlots()
|
||||
|
||||
const hasAnything = computed(() =>
|
||||
@@ -87,7 +107,7 @@ const hasAnything = computed(() =>
|
||||
)
|
||||
|
||||
onMounted(loadMachines)
|
||||
watch(() => props.assetid, loadMachines)
|
||||
watch(() => [props.assetid, props.counterparttype, props.relationshiptypes], loadMachines)
|
||||
|
||||
async function loadMachines() {
|
||||
machines.value = []
|
||||
@@ -96,9 +116,11 @@ async function loadMachines() {
|
||||
const response = await api.get(`/assets/${props.assetid}/relationships`)
|
||||
const data = response.data.data || {}
|
||||
const found = []
|
||||
// BOTH directions. The canonical edge is PC controls machine, but
|
||||
// production also holds reciprocal rows, so reading one direction shows
|
||||
// the link on half the pages and not the other half.
|
||||
// BOTH directions, then deduped. Production holds reciprocal rows for
|
||||
// the same link, so reading one direction shows it on half the pages and
|
||||
// not the other half - and reading both without deduping shows it twice.
|
||||
// Direction is therefore not trustworthy here and is deliberately not
|
||||
// used to label anything.
|
||||
for (const edge of data.outgoing || []) collect(edge, edge.targetasset, found)
|
||||
for (const edge of data.incoming || []) collect(edge, edge.sourceasset, found)
|
||||
machines.value = found
|
||||
@@ -111,10 +133,19 @@ async function loadMachines() {
|
||||
|
||||
function collect(edge, other, found) {
|
||||
if (!other) return
|
||||
if ((edge.relationshiptypename || '').toLowerCase() !== 'controls') return
|
||||
if ((other.assettypename || '').toLowerCase() !== 'machine') return
|
||||
const wanted = props.relationshiptypes.map(t => t.toLowerCase())
|
||||
if (!wanted.includes((edge.relationshiptypename || '').toLowerCase())) return
|
||||
if ((other.assettypename || '').toLowerCase() !== props.counterparttype.toLowerCase()) return
|
||||
if (found.some(m => m.assetid === other.assetid)) return
|
||||
found.push({ assetid: other.assetid, assetnumber: other.assetnumber, name: other.name })
|
||||
// pluginid and assettypename are what assetRoute() needs; without them the
|
||||
// chip falls back to /assets/<assetid>, which is not a route.
|
||||
found.push({
|
||||
assetid: other.assetid,
|
||||
assetnumber: other.assetnumber,
|
||||
name: other.name,
|
||||
pluginid: other.pluginid,
|
||||
assettypename: other.assettypename
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
43
frontend/src/utils/assetRoute.js
Normal file
43
frontend/src/utils/assetRoute.js
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Where an asset's detail page lives.
|
||||
*
|
||||
* Extracted from AssetRelationships.vue because a second caller appeared (the
|
||||
* triage band) and got it wrong: it linked to `/assets/<assetid>`, which is not
|
||||
* a route. There is no `/assets/:id` page and no catch-all, so every chip was a
|
||||
* dead link. One copy, so a third caller cannot repeat it.
|
||||
*
|
||||
* Two ids are in play and they are not interchangeable. `assetid` is the core
|
||||
* row; the detail routes key on `pluginid`, the id of the extension-table row
|
||||
* (machineid, computerid, printerid). Sending assetid to /machines/:id opens
|
||||
* the wrong machine, or none.
|
||||
*/
|
||||
|
||||
// Asset type NAME to route base. The names are seed data, and seeding is not
|
||||
// consistent about case: shopdb/core/services/csvimport.py seeds 'Computer'
|
||||
// and 'Machine' capitalised, while this site's database holds them lowercase.
|
||||
// The lookup is therefore lowercased on both sides - a case-sensitive match
|
||||
// silently produced dead links at whichever site seeded the other way.
|
||||
const ROUTE_BY_TYPE = {
|
||||
machine: '/machines',
|
||||
computer: '/pcs',
|
||||
printer: '/printers',
|
||||
network_device: '/network',
|
||||
'network device': '/network',
|
||||
measuring_tool: '/measuringtools',
|
||||
'measuring tool': '/measuringtools'
|
||||
}
|
||||
|
||||
export function assetRoute(asset) {
|
||||
if (!asset) return '#'
|
||||
|
||||
const typename = String(asset.assettypename || asset.assettype || '').toLowerCase()
|
||||
const basePath = ROUTE_BY_TYPE[typename] || '/assets'
|
||||
|
||||
const id = asset.pluginid
|
||||
|| asset.typedata?.networkdeviceid
|
||||
|| asset.typedata?.machineid
|
||||
|| asset.assetid
|
||||
return `${basePath}/${id}`
|
||||
}
|
||||
|
||||
export default assetRoute
|
||||
54
frontend/src/utils/assetRoute.spec.js
Normal file
54
frontend/src/utils/assetRoute.spec.js
Normal file
@@ -0,0 +1,54 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { assetRoute } from './assetRoute'
|
||||
|
||||
describe('assetRoute', () => {
|
||||
// The bug this file exists to prevent: the triage band linked to
|
||||
// /assets/<assetid>, and there is no /assets/:id route and no catch-all,
|
||||
// so every chip was a dead link on five detail pages.
|
||||
it('routes by asset type, never to /assets', () => {
|
||||
expect(assetRoute({ assettypename: 'machine', pluginid: 223, assetid: 313 }))
|
||||
.toBe('/machines/223')
|
||||
expect(assetRoute({ assettypename: 'computer', pluginid: 279, assetid: 304 }))
|
||||
.toBe('/pcs/279')
|
||||
expect(assetRoute({ assettypename: 'printer', pluginid: 1, assetid: 594 }))
|
||||
.toBe('/printers/1')
|
||||
})
|
||||
|
||||
it('uses pluginid, not assetid - they are different rows', () => {
|
||||
// /machines/:id keys on machineid. Passing assetid opens the wrong
|
||||
// machine, or none, and looks like a data problem rather than a link bug.
|
||||
const route = assetRoute({ assettypename: 'machine', pluginid: 223, assetid: 313 })
|
||||
expect(route).toBe('/machines/223')
|
||||
expect(route).not.toContain('313')
|
||||
})
|
||||
|
||||
it('matches the type name whatever case the site seeded it in', () => {
|
||||
// csvimport.py seeds 'Computer' and 'Machine' capitalised; this site's
|
||||
// database holds them lowercase. A case-sensitive lookup produced dead
|
||||
// links at whichever site seeded the other way.
|
||||
expect(assetRoute({ assettypename: 'Computer', pluginid: 7 })).toBe('/pcs/7')
|
||||
expect(assetRoute({ assettypename: 'MACHINE', pluginid: 8 })).toBe('/machines/8')
|
||||
})
|
||||
|
||||
it('accepts the older assettype key as well as assettypename', () => {
|
||||
expect(assetRoute({ assettype: 'printer', pluginid: 4 })).toBe('/printers/4')
|
||||
})
|
||||
|
||||
it('falls back through typedata when pluginid is absent', () => {
|
||||
expect(assetRoute({ assettypename: 'network_device', typedata: { networkdeviceid: 12 } }))
|
||||
.toBe('/network/12')
|
||||
expect(assetRoute({ assettypename: 'machine', typedata: { machineid: 30 } }))
|
||||
.toBe('/machines/30')
|
||||
})
|
||||
|
||||
it('returns a harmless anchor for nothing', () => {
|
||||
expect(assetRoute(null)).toBe('#')
|
||||
expect(assetRoute(undefined)).toBe('#')
|
||||
})
|
||||
|
||||
it('handles both spellings of the multiword type names', () => {
|
||||
expect(assetRoute({ assettypename: 'network device', pluginid: 2 })).toBe('/network/2')
|
||||
expect(assetRoute({ assettypename: 'measuring_tool', pluginid: 3 })).toBe('/measuringtools/3')
|
||||
expect(assetRoute({ assettypename: 'measuring tool', pluginid: 3 })).toBe('/measuringtools/3')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user