-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Description
NetBox version
v2.11.7
Feature type
Change to existing functionality
Proposed functionality
State of the art
As of Netbox 2.11.x, reports are automatically discovered and rendered in the UI (extras/reports/). The order of the reports is based on alphabetical order.
As an exemple, if you have a python module named DCIM-report.py with 3 Reports subclasses named report_a, report_b and report_z, the order in which reports are rendered in the report list depends upon their name.
- report_a
- report_b
- report_z
The code handling this list can be found here:
netbox/netbox/extras/reports.py
Lines 46 to 65 in 10847e2
| def get_reports(): | |
| """ | |
| Compile a list of all reports available across all modules in the reports path. Returns a list of tuples: | |
| [ | |
| (module_name, (report, report, report, ...)), | |
| (module_name, (report, report, report, ...)), | |
| ... | |
| ] | |
| """ | |
| module_list = [] | |
| # Iterate through all modules within the reports path. These are the user-created files in which reports are | |
| # defined. | |
| for importer, module_name, _ in pkgutil.iter_modules([settings.REPORTS_ROOT]): | |
| module = importer.find_module(module_name).load_module(module_name) | |
| report_list = [cls() for _, cls in inspect.getmembers(module, is_report)] | |
| module_list.append((module_name, report_list)) | |
| return module_list |
Proposal
Use case
We propose the possibility for the developer to order the report's listing inside a python module. This can possibly help Ops team to follow each report and correct corresponding errors one after another.
Implementation
The Report class should have a new attribute named order with a default value of -1. Developer would have the possibility to order their reports based on this attribute, the biggest number being the first report in the list.
# Report base class (netbox/extras/report.py)
Report(object):
[...]
order = -1
[...]
# some custom reports under netbox/reports/my_custom_report.py
MyCustomReport(Report)
[...]
order = 999
MyCustomReport2(Report)
[...]
order = 1000Netbox source code should also take into account this attribute in order to reorder the report list inside each python module.
def get_reports():
"""
Compile a list of all reports available across all modules in the reports path. Returns a list of tuples:
[
(module_name, (report, report, report, ...)),
(module_name, (report, report, report, ...)),
...
]
"""
module_list = []
# Iterate through all modules within the reports path. These are the user-created files in which reports are
# defined.
for importer, module_name, _ in pkgutil.iter_modules([settings.REPORTS_ROOT]):
module = importer.find_module(module_name).load_module(module_name)
report_list = [cls() for _, cls in inspect.getmembers(module, is_report)]
report_list.sort(key=lambda x: x.order, reverse=True)
module_list.append((module_name, report_list))
return module_list Use case
Ordering reports on the WebUI would guide Ops team to check each report and correct the pinpoint errors one after another.
Database changes
N/A
External dependencies
N/A