12510 legacy jobs

This commit is contained in:
Arthur 2024-01-29 14:55:47 -08:00
parent 52689616e9
commit e61b960cd7
3 changed files with 50 additions and 33 deletions

View File

@ -272,18 +272,17 @@ class BaseScript(object):
def __init__(self): def __init__(self):
self._logs = {} self._logs = {}
self._failed = False self._failed = False
self._current_method = 'totals' self._current_method = 'unassigned'
self._output = '' self._output = ''
# Initiate the log # Initiate the log
self.logger = logging.getLogger(f"netbox.scripts.{self.__module__}.{self.__class__.__name__}") self.logger = logging.getLogger(f"netbox.scripts.{self.__module__}.{self.__class__.__name__}")
self.log = []
# Declare the placeholder for the current request # Declare the placeholder for the current request
self.request = None self.request = None
# Compile test methods and initialize results skeleton # Compile test methods and initialize results skeleton
self._logs['totals'] = { self._logs['unassigned'] = {
'success': 0, 'success': 0,
'info': 0, 'info': 0,
'warning': 0, 'warning': 0,
@ -454,8 +453,8 @@ class BaseScript(object):
if log_level != LogLevelChoices.LOG_DEFAULT: if log_level != LogLevelChoices.LOG_DEFAULT:
self._logs[self._current_method][log_level] += 1 self._logs[self._current_method][log_level] += 1
if self._current_method != 'totals': if self._current_method != 'unassigned':
self._logs['totals'][log_level] += 1 self._logs['unassigned'][log_level] += 1
if obj: if obj:
self.logger.log(level, f"{log_level.capitalize()} | {obj}: {message}") self.logger.log(level, f"{log_level.capitalize()} | {obj}: {message}")
@ -527,12 +526,12 @@ class BaseScript(object):
test_method() test_method()
except Exception as e: except Exception as e:
self.post_run() self.post_run()
self._current_method = 'totals' self._current_method = 'unassigned'
raise e raise e
# Perform any post-run tasks # Perform any post-run tasks
self.post_run() self.post_run()
self._current_method = 'totals' self._current_method = 'unassigned'
def run(self, data, commit): def run(self, data, commit):
self.run_test_scripts() self.run_test_scripts()
@ -650,12 +649,10 @@ def run_script(data, job, request=None, commit=True, **kwargs):
return fn(**kwargs) return fn(**kwargs)
def set_job_data(job, script): def set_job_data(script):
logs = script._logs logs = script._logs
totals = logs.pop('totals')
job.data = { job.data = {
'logs': logs, 'logs': logs,
'totals': totals,
'output': script._output, 'output': script._output,
} }
return job return job
@ -675,7 +672,7 @@ def run_script(data, job, request=None, commit=True, **kwargs):
call_with_appropriate(script.log_info, kwargs={'message': "Database changes have been reverted automatically."}) call_with_appropriate(script.log_info, kwargs={'message': "Database changes have been reverted automatically."})
if request: if request:
clear_events.send(request) clear_events.send(request)
job = set_job_data(job, script) job = set_job_data(script)
if script._failed: if script._failed:
logger.warning(f"Script failed") logger.warning(f"Script failed")
job.terminate(status=JobStatusChoices.STATUS_FAILED) job.terminate(status=JobStatusChoices.STATUS_FAILED)

View File

@ -1163,6 +1163,9 @@ class ScriptResultView(ContentTypePermissionRequiredMixin, View):
module = job.object module = job.object
script = module.scripts[job.name]() script = module.scripts[job.name]()
legacy_script = False
legacy_report = False
if job.data:
if 'logs' in job.data: if 'logs' in job.data:
legacy_script = False legacy_script = False
legacy_report = False legacy_report = False

View File

@ -89,6 +89,7 @@
{% endif %} {% endif %}
</td> </td>
</tr> </tr>
{% if last_job.data.logs %}
{% for method, stats in last_job.data.logs.items %} {% for method, stats in last_job.data.logs.items %}
<tr> <tr>
<td colspan="4" class="method"> <td colspan="4" class="method">
@ -102,6 +103,22 @@
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}
{% elif not last_job.data.log %}
{# legacy #}
{% for method, stats in last_job.data.items %}
<tr>
<td colspan="4" class="method">
<span class="ps-3">{{ method }}</span>
</td>
<td class="text-end text-nowrap report-stats">
<span class="badge bg-success">{{ stats.success }}</span>
<span class="badge bg-info">{{ stats.info }}</span>
<span class="badge bg-warning">{{ stats.warning }}</span>
<span class="badge bg-danger">{{ stats.failure }}</span>
</td>
</tr>
{% endfor %}
{% endif %}
{% endwith %} {% endwith %}
{% endfor %} {% endfor %}
{% endwith %} {% endwith %}