pc-relationships bidirectional

This commit is contained in:
2026-07-29 13:16:16 -04:00
parent c17d170870
commit 366f49c969
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 = [{