reports: pc-relationships matches PC<->machine links in either direction

Prod had 331 relationships, 268 computers, 204 machines, but the report came
back empty. The query only matched computer(source) -> machine(target), while
the import stores the general machinerelationships as machine(source) ->
PC(target) (only the synthetic measuring-tool links are PC -> tool). So the real
shop-floor edges never matched.

Make the query direction-agnostic (UNION of both orientations); a PC-runs-machine
report is conceptually undirected. Also drop the comtypeid=1 filter so the IP is
taken from the primary communication regardless of its type.

Test: a machine(source) -> PC(target) edge now appears in the report.
This commit is contained in:
cproudlock
2026-07-29 13:16:15 -04:00
parent 3eaaee0e50
commit cb075a278f
2 changed files with 70 additions and 10 deletions

View File

@@ -467,9 +467,14 @@ def pc_relationships():
Query parameters:
- format: 'json' (default) or 'csv'
"""
# Asset relationships where a computer (source) relates to a machine
# (target) - the asset-model equivalent of the legacy PC->machine links.
sql = db.text("""
# PC <-> machine relationships in EITHER direction. The import orients edges
# both ways: the general machinerelationships migration stores machine(source)
# -> PC(target), while the synthetic measuring-tool links store PC(source) ->
# tool(target). Match both so the report is not empty just because of edge
# direction (this is a conceptually undirected "PC runs machine" report).
# Each half joins a computer on one end and a machine on the other; the
# machine end supplies machine_number/vendor/model, the PC end hostname/ip.
half = """
SELECT
eq.assetnumber AS machine_number,
v.vendor AS vendor,
@@ -477,17 +482,22 @@ def pc_relationships():
COALESCE(cpc.hostname, pc.assetnumber) AS hostname,
c.ipaddress AS ip
FROM assetrelationships ar
JOIN assets pc ON ar.sourceassetid = pc.assetid
JOIN computers cpc ON cpc.assetid = pc.assetid
JOIN assets eq ON ar.targetassetid = eq.assetid
JOIN machines eqx ON eqx.assetid = eq.assetid
LEFT JOIN communications c ON c.assetid = pc.assetid AND c.isprimary = 1 AND c.comtypeid = 1
JOIN computers cpc ON cpc.assetid = ar.{pc_end}
JOIN assets pc ON pc.assetid = ar.{pc_end}
JOIN machines eqx ON eqx.assetid = ar.{machine_end}
JOIN assets eq ON eq.assetid = ar.{machine_end}
LEFT JOIN communications c ON c.assetid = pc.assetid AND c.isprimary = 1
LEFT JOIN models mo ON eqx.modelnumberid = mo.modelnumberid
LEFT JOIN vendors v ON mo.vendorid = v.vendorid
WHERE ar.isactive = 1
AND eq.assetnumber IS NOT NULL AND eq.assetnumber != ''
ORDER BY eq.assetnumber
""")
"""
sql = db.text(
half.format(pc_end='sourceassetid', machine_end='targetassetid')
+ ' UNION '
+ half.format(pc_end='targetassetid', machine_end='sourceassetid')
+ ' ORDER BY machine_number'
)
results = db.session.execute(sql).fetchall()
data = [{