[PR #12632] [MERGED] 6347 Cache the number of each component type assigned to devices/VMs #14025

Closed
opened 2025-12-29 23:22:06 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/12632
Author: @arthanson
Created: 5/16/2023
Status: Merged
Merged: 7/25/2023
Merged by: @jeremystretch

Base: featureHead: 6347-cache-counts


📝 Commits (10+)

📊 Changes

23 files changed (+623 additions, -35 deletions)

View changed files

📝 docs/development/application-registry.md (+4 -0)
📝 netbox/dcim/api/serializers.py (+21 -5)
📝 netbox/dcim/apps.py (+5 -1)
netbox/dcim/migrations/0175_device_component_counters.py (+100 -0)
📝 netbox/dcim/models/device_components.py (+11 -10)
📝 netbox/dcim/models/devices.py (+43 -1)
📝 netbox/dcim/tables/devices.py (+33 -3)
📝 netbox/dcim/views.py (+9 -9)
📝 netbox/netbox/models/__init__.py (+1 -0)
📝 netbox/netbox/registry.py (+1 -0)
netbox/utilities/counters.py (+93 -0)
📝 netbox/utilities/fields.py (+42 -0)
netbox/utilities/management/__init__.py (+0 -0)
netbox/utilities/management/commands/__init__.py (+0 -0)
netbox/utilities/management/commands/calculate_cached_counts.py (+52 -0)
netbox/utilities/tests/test_counters.py (+69 -0)
netbox/utilities/tracking.py (+78 -0)
📝 netbox/virtualization/api/serializers.py (+5 -1)
📝 netbox/virtualization/apps.py (+5 -0)
netbox/virtualization/migrations/0035_virtualmachine_interface_count.py (+35 -0)

...and 3 more files

📄 Description

Fixes: #6347

Adds counter fields to cache related model counts. Does this by adding a CachedCounterField type that hooks up post-save and post-delete signals on the related model, these hooked up by calling connect_counter in apps.py

also has change tracking, there is a TrackingModelMixin which I added to the base classes - by default it does nothing but if you set change_tracking_fields on the model to the fields to track it will do change tracking on them. The connect_counter method automatically does the correct fields (generally device_id) and then checks this on post_save signal to see if it has been moved and updates the counts on the old and new parent models appropriately.

The change tracking is probably useful elsewhere - you can check model.tracker.changed to see any changed fields. Note: these fields must be listed in change_tracking_fields.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/12632 **Author:** [@arthanson](https://github.com/arthanson) **Created:** 5/16/2023 **Status:** ✅ Merged **Merged:** 7/25/2023 **Merged by:** [@jeremystretch](https://github.com/jeremystretch) **Base:** `feature` ← **Head:** `6347-cache-counts` --- ### 📝 Commits (10+) - [`2e6bc7b`](https://github.com/netbox-community/netbox/commit/2e6bc7b53a65828267ae74c07fab75d15963865e) 6347 initial counter field - [`acb58f0`](https://github.com/netbox-community/netbox/commit/acb58f0a31a8dac9c1c05d88c00c1a62ae4deb10) 6347 connect the counters - [`c955528`](https://github.com/netbox-community/netbox/commit/c9555284160378f9cf4e2df37709b62187d56218) 6347 migrate initial counts - [`450da31`](https://github.com/netbox-community/netbox/commit/450da3125f23fd1c032e25079efa319b0622cd36) 6347 migrate initial counts - [`73c9d85`](https://github.com/netbox-community/netbox/commit/73c9d8584a6f73582f222ff408e9fbfee81373f0) 6347 fix count - [`4f193b6`](https://github.com/netbox-community/netbox/commit/4f193b63c43fd6fb308eb1d0898dfbb7652350ce) 6347 comment - [`9fce8b8`](https://github.com/netbox-community/netbox/commit/9fce8b8efe893bf9fdaa7ad8448b63aef7a31b00) 6347 reindent migration - [`c7e6f5e`](https://github.com/netbox-community/netbox/commit/c7e6f5e55693f7d6a66e26be7647282ab5bf36c8) 6347 virtual machine interface count - [`eb0c901`](https://github.com/netbox-community/netbox/commit/eb0c901a9fb59bdc34ddaa24408065cbb8dcbbac) 6347 add missing migration - [`c7a4fd1`](https://github.com/netbox-community/netbox/commit/c7a4fd14ee3fb8e213eee4e0dd62890b4946a5ba) 6347 only update count if created ### 📊 Changes **23 files changed** (+623 additions, -35 deletions) <details> <summary>View changed files</summary> 📝 `docs/development/application-registry.md` (+4 -0) 📝 `netbox/dcim/api/serializers.py` (+21 -5) 📝 `netbox/dcim/apps.py` (+5 -1) ➕ `netbox/dcim/migrations/0175_device_component_counters.py` (+100 -0) 📝 `netbox/dcim/models/device_components.py` (+11 -10) 📝 `netbox/dcim/models/devices.py` (+43 -1) 📝 `netbox/dcim/tables/devices.py` (+33 -3) 📝 `netbox/dcim/views.py` (+9 -9) 📝 `netbox/netbox/models/__init__.py` (+1 -0) 📝 `netbox/netbox/registry.py` (+1 -0) ➕ `netbox/utilities/counters.py` (+93 -0) 📝 `netbox/utilities/fields.py` (+42 -0) ➕ `netbox/utilities/management/__init__.py` (+0 -0) ➕ `netbox/utilities/management/commands/__init__.py` (+0 -0) ➕ `netbox/utilities/management/commands/calculate_cached_counts.py` (+52 -0) ➕ `netbox/utilities/tests/test_counters.py` (+69 -0) ➕ `netbox/utilities/tracking.py` (+78 -0) 📝 `netbox/virtualization/api/serializers.py` (+5 -1) 📝 `netbox/virtualization/apps.py` (+5 -0) ➕ `netbox/virtualization/migrations/0035_virtualmachine_interface_count.py` (+35 -0) _...and 3 more files_ </details> ### 📄 Description ### Fixes: #6347 Adds counter fields to cache related model counts. Does this by adding a CachedCounterField type that hooks up post-save and post-delete signals on the related model, these hooked up by calling connect_counter in apps.py also has change tracking, there is a TrackingModelMixin which I added to the base classes - by default it does nothing but if you set change_tracking_fields on the model to the fields to track it will do change tracking on them. The connect_counter method automatically does the correct fields (generally device_id) and then checks this on post_save signal to see if it has been moved and updates the counts on the old and new parent models appropriately. The change tracking is probably useful elsewhere - you can check model.tracker.changed to see any changed fields. Note: these fields must be listed in change_tracking_fields. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-29 23:22:06 +01:00
adam closed this issue 2025-12-29 23:22:06 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#14025