Skip to content

Feat/1996 support device history search for inactive devices#3665

Merged
Simrayz merged 4 commits intomasterfrom
feat/1996-support-device-history-search-for-inactive-devices
Dec 8, 2025
Merged

Feat/1996 support device history search for inactive devices#3665
Simrayz merged 4 commits intomasterfrom
feat/1996-support-device-history-search-for-inactive-devices

Conversation

@Simrayz
Copy link
Copy Markdown
Contributor

@Simrayz Simrayz commented Dec 4, 2025

Scope and purpose

Resolves #1996

This PR adds support for searching for serial numbers that are no longer associated with equipment in the database, such as Netboxes, modules or power supplies. These are named "Inactive Devices", as they are not registered on any component in the system. This is necessary to enable users of NAV to find alert history events for devices that have been "disconnected" from a component.

Screenshots

image

How to test

Adding alerts to an inactive device

Your local database is unlikely to contain any history for inactive devices, as the alert history is empty by default when dumping from a production database. I solved this by finding an unused device and adding a few alerts manually. You can use this python snippet (when using django-admin shell)

from nav.models.event import AlertHistory
from datetime import datetime, timedelta

serial = 'SMC1145003Z'
device = AlertHistory.objects.filter(device__serial=serial).first()

AlertHistory.objects.create(device=device, source_id='pping', start_time=datetime.now(), end_time=datetime.now() + timedelta(days=2), event_type_id='boxState', alert_type_id=1, value=42)
AlertHistory.objects.create(device=device, source_id='pping', start_time=datetime.now(), end_time=datetime.now() + timedelta(days=2), event_type_id='boxState', alert_type_id=3, value=42)

Querying the database for inactive devices

You can query the database by using the shell and the Device model, or with a sql query. Here are the two options.

Device.objects.filter(serial__isnull=False, entities__isnull=True, power_supplies_or_fans__isnull=True, modules__isnull=True)
SELECT d.*
FROM manage.device d
LEFT JOIN manage.netboxentity nbe ON nbe.deviceid = d.deviceid
LEFT JOIN manage.module m ON m.deviceid = d.deviceid
LEFT JOIN manage.powersupply_or_fan ps ON ps.deviceid = d.deviceid
WHERE d.serial IS NOT NULL
  AND nbe.deviceid IS NULL
  AND m.deviceid IS NULL
  AND ps.deviceid IS NULL;

NOTE: They don't return the same number of records, which is strange.

Contributor Checklist

Every pull request should have this checklist filled out, no matter how small it is.
More information about contributing to NAV can be found in the
Hacker's guide to NAV.

  • Added a changelog fragment for towncrier
  • Added/amended tests for new/changed code
  • Added/changed documentation
  • Linted/formatted the code with ruff, easiest by using pre-commit
  • Wrote the commit message so that the first line continues the sentence "If applied, this commit will ...", starts with a capital letter, does not end with punctuation and is 50 characters or less long. See https://cbea.ms/git-commit/
  • Based this pull request on the correct upstream branch: For a patch/bugfix affecting the latest stable version, it should be based on that version's branch (<major>.<minor>.x). For a new feature or other additions, it should be based on master.
  • If applicable: Created new issues if this PR does not fix the issue completely/there is further work to be done
  • If it's not obvious from a linked issue, described how to interact with NAV in order for a reviewer to observe the effects of this change first-hand (commands, URLs, UI interactions)
  • If this results in changes in the UI: Added screenshots of the before and after
  • If this adds a new Python source code file: Added the boilerplate header to that file

@github-actions
Copy link
Copy Markdown

github-actions bot commented Dec 4, 2025

Test results

    27 files      27 suites   45m 33s ⏱️
 2 707 tests  2 707 ✅ 0 💤 0 ❌
20 046 runs  20 046 ✅ 0 💤 0 ❌

Results for commit 4201fc3.

♻️ This comment has been updated with latest results.

@codecov
Copy link
Copy Markdown

codecov bot commented Dec 4, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 62.96%. Comparing base (ce9853f) to head (4201fc3).
⚠️ Report is 404 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3665      +/-   ##
==========================================
+ Coverage   62.95%   62.96%   +0.01%     
==========================================
  Files         611      611              
  Lines       45150    45163      +13     
  Branches       43       43              
==========================================
+ Hits        28424    28438      +14     
+ Misses      16716    16715       -1     
  Partials       10       10              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Simrayz Simrayz force-pushed the feat/1996-support-device-history-search-for-inactive-devices branch 2 times, most recently from d07d922 to bfa049c Compare December 5, 2025 09:02
@Simrayz Simrayz requested a review from a team December 5, 2025 09:41
@Simrayz Simrayz marked this pull request as ready for review December 5, 2025 10:15
Copy link
Copy Markdown
Member

@lunkwill42 lunkwill42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does the job and LGTM :)

Comment on lines +158 to +170
def create_alert_history_for_device(device, count=1):
for day in range(count):
activity = AlertHistory(
device=device,
event_type_id='boxState',
alert_type_id=1,
source_id='pping',
start_time=datetime(2025, 1, day + 1),
end_time=datetime(2025, 1, day + 10),
value=42,
)
activity.save()
return AlertHistory.objects.filter(device=device)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will litter the test database with records, by itself, though.

It might help that it is always used in conjunction with a fixture that depends on the db fixture, but in isolation this will potentially leave a lot of stuff in the db.

Not a showstopper, but it could bite us down the line. Maybe this too should be a fixture?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the "db" fixture to the test that calls this function, that should fix it right?

@lunkwill42
Copy link
Copy Markdown
Member

Your local database is unlikely to contain any history for inactive devices, as the alert history is empty by default when dumping from a production database. I solved this by finding an unused device and adding a few alerts manually.

There's also the option to leave alert history intact when dumping the db from a remote production server. Something like this would do it:

diff --git a/tools/dump-remote-db.sh b/tools/dump-remote-db.sh
--- a/tools/dump-remote-db.sh
+++ b/tools/dump-remote-db.sh
@@ -10,4 +10,4 @@ echo "Dumping NAV database from $REMOTE"
 # This filters potentially huge amounts of old log data and also user alert
 # profiles (so as not to start sending notifications to real users from a dev
 # environment):
-ssh "$REMOTE" navpgdump --only-open-arp --only-open-cam -e alertprofile -e alerthist
+ssh "$REMOTE" navpgdump --only-open-arp --only-open-cam -e alertprofile

@Simrayz Simrayz force-pushed the feat/1996-support-device-history-search-for-inactive-devices branch from bfa049c to 4201fc3 Compare December 8, 2025 07:31
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Dec 8, 2025

@Simrayz Simrayz merged commit 5149e64 into master Dec 8, 2025
20 checks passed
@Simrayz Simrayz deleted the feat/1996-support-device-history-search-for-inactive-devices branch December 8, 2025 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make it possible to show historical information on devices based on serialnumber

2 participants