From 130d5283a4da1a742f848cfb10015a20b9bf4cc1 Mon Sep 17 00:00:00 2001 From: Arthur Date: Thu, 18 May 2023 09:17:46 -0700 Subject: [PATCH] 6347 add management command --- netbox/utilities/management/__init__.py | 0 .../utilities/management/commands/__init__.py | 0 .../commands/calculate_cached_counts.py | 36 +++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 netbox/utilities/management/__init__.py create mode 100644 netbox/utilities/management/commands/__init__.py create mode 100644 netbox/utilities/management/commands/calculate_cached_counts.py diff --git a/netbox/utilities/management/__init__.py b/netbox/utilities/management/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/netbox/utilities/management/commands/__init__.py b/netbox/utilities/management/commands/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/netbox/utilities/management/commands/calculate_cached_counts.py b/netbox/utilities/management/commands/calculate_cached_counts.py new file mode 100644 index 000000000..d6f3bcbf2 --- /dev/null +++ b/netbox/utilities/management/commands/calculate_cached_counts.py @@ -0,0 +1,36 @@ +from django.core.management.base import BaseCommand +from django.core.management.color import no_style + +from dcim.models import Device +from virtualization.models import VirtualMachine + + +def recalculate_device_counts(): + for device in Device.objects.all(): + device._console_port_count = device.consoleports.count() + device._console_server_port_count = device.consoleserverports.count() + device._interface_count = device.interfaces.count() + device._front_port_count = device.frontports.count() + device._rear_port_count = device.rearports.count() + device._device_bay_count = device.devicebays.count() + device._inventory_item_count = device.inventoryitems.count() + device._power_port_count = device.powerports.count() + device._power_outlet_count = device.poweroutlets.count() + device.save() + + +def recalculate_virtual_machine_counts(): + for vm in VirtualMachine.objects.all(): + vm._interface_count = vm.interfaces.count() + vm.save() + + +class Command(BaseCommand): + help = "Recalculate cached counts" + + def handle(self, *model_names, **options): + self.stdout.write('Recalculating device counts...') + recalculate_device_counts() + self.stdout.write('Recalculating virtual machine counts...') + recalculate_virtual_machine_counts() + self.stdout.write(self.style.SUCCESS('Finished.'))