netbox-community / customizations

A collection of community submitted and maintained customizations including reports, scripts, validators and export templates
MIT License
204 stars 55 forks source link

Add script to find orphaned cables #92

Closed DanSheps closed 7 months ago

DanSheps commented 1 year ago

Pretty basic, query for orphaned or partially orphaned cables and return the list.

kkthxbye-code commented 1 year ago

One-liner if interested:

from django.db.models import Count

Cable.objects.annotate(
    aterm=Count('terminations', filter=Q(terminations__cable_end="A")),
    bterm=Count('terminations', filter=Q(terminations__cable_end="B")),
).filter(Q(aterm=1, bterm=0) | Q(aterm=0, bterm=1) | Q(aterm=0, bterm=0))
kkthxbye-code commented 1 year ago

Slight edit, added __gte:

Cable.objects.annotate(
    aterm=Count('terminations', filter=Q(terminations__cable_end="A")),
    bterm=Count('terminations', filter=Q(terminations__cable_end="B")),
).filter(Q(aterm__gte=1, bterm=0) | Q(aterm=0, bterm__gte=1) | Q(aterm=0, bterm=0))
DanSheps commented 1 year ago

Slight edit, added __gte:

Cable.objects.annotate(
    aterm=Count('terminations', filter=Q(terminations__cable_end="A")),
    bterm=Count('terminations', filter=Q(terminations__cable_end="B")),
).filter(Q(aterm__gte=1, bterm=0) | Q(aterm=0, bterm__gte=1) | Q(aterm=0, bterm=0))

Looks like that works, and doesn't have the problems I was coming up against.