mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Initial work on reports API
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
from __future__ import unicode_literals
|
||||
import importlib
|
||||
import inspect
|
||||
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.utils import timezone
|
||||
|
||||
from extras.reports import Report
|
||||
from extras.models import ReportResult
|
||||
from extras.reports import get_reports
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
@@ -18,43 +17,34 @@ class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
|
||||
# Gather all reports to be run
|
||||
reports = []
|
||||
for module_name in options['reports']:
|
||||
|
||||
# Split the report name off if one has been provided.
|
||||
report_name = None
|
||||
if '.' in module_name:
|
||||
module_name, report_name = module_name.split('.', 1)
|
||||
|
||||
# Import the report module
|
||||
try:
|
||||
report_module = importlib.import_module('reports.report_{}'.format(module_name))
|
||||
except ImportError:
|
||||
self.stdout.write(
|
||||
"Report module '{}' not found. Ensure that the report has been saved as 'report_{}.py' in the "
|
||||
"reports directory.".format(module_name, module_name)
|
||||
)
|
||||
return
|
||||
|
||||
# If the name of a particular report has been given, run that. Otherwise, run all reports in the module.
|
||||
if report_name is not None:
|
||||
report_cls = getattr(report_module, report_name)
|
||||
reports = [(report_name, report_cls)]
|
||||
else:
|
||||
for name, report_cls in inspect.getmembers(report_module, inspect.isclass):
|
||||
if report_cls in Report.__subclasses__():
|
||||
reports.append((name, report_cls))
|
||||
reports = get_reports()
|
||||
|
||||
# Run reports
|
||||
for name, report_cls in reports:
|
||||
self.stdout.write("[{:%H:%M:%S}] Running {}...".format(timezone.now(), name))
|
||||
report = report_cls()
|
||||
results = report.run()
|
||||
status = self.style.ERROR('FAILED') if report.failed else self.style.SUCCESS('SUCCESS')
|
||||
self.stdout.write("[{:%H:%M:%S}] {}: {}".format(timezone.now(), name, status))
|
||||
for test_name, attrs in results.items():
|
||||
self.stdout.write(" {}: {} success, {} info, {} warning, {} failed".format(
|
||||
test_name, attrs['success'], attrs['info'], attrs['warning'], attrs['failed']
|
||||
))
|
||||
for module_name, report in reports:
|
||||
for report_name, report_cls in report:
|
||||
report_name_full = '{}.{}'.format(module_name, report_name)
|
||||
if module_name in options['reports'] or report_name_full in options['reports']:
|
||||
|
||||
self.stdout.write("[{:%H:%M:%S}] Finished".format(timezone.now()))
|
||||
# Run the report
|
||||
self.stdout.write(
|
||||
"[{:%H:%M:%S}] Running {}.{}...".format(timezone.now(), module_name, report_name)
|
||||
)
|
||||
report = report_cls()
|
||||
results = report.run()
|
||||
|
||||
# Report on success/failure
|
||||
status = self.style.ERROR('FAILED') if report.failed else self.style.SUCCESS('SUCCESS')
|
||||
for test_name, attrs in results.items():
|
||||
self.stdout.write(
|
||||
"\t{}: {} success, {} info, {} warning, {} failed".format(
|
||||
test_name, attrs['success'], attrs['info'], attrs['warning'], attrs['failed']
|
||||
)
|
||||
)
|
||||
self.stdout.write(
|
||||
"[{:%H:%M:%S}] {}.{}: {}".format(timezone.now(), module_name, report_name, status)
|
||||
)
|
||||
|
||||
# Wrap things up
|
||||
self.stdout.write(
|
||||
"[{:%H:%M:%S}] Finished".format(timezone.now())
|
||||
)
|
||||
|
Reference in New Issue
Block a user