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

View File

@ -1163,15 +1163,18 @@ class ScriptResultView(ContentTypePermissionRequiredMixin, View):
module = job.object
script = module.scripts[job.name]()
if 'logs' in job.data:
legacy_script = False
legacy_report = False
elif 'log' in job.data:
legacy_script = True
legacy_report = False
else:
legacy_script = False
legacy_report = True
legacy_script = False
legacy_report = False
if job.data:
if 'logs' in job.data:
legacy_script = False
legacy_report = False
elif 'log' in job.data:
legacy_script = True
legacy_report = False
else:
legacy_script = False
legacy_report = True
# If this is an HTMX request, return only the result HTML
if request.htmx:

View File

@ -89,19 +89,36 @@
{% endif %}
</td>
</tr>
{% for method, stats in last_job.data.logs.items %}
<tr>
<td colspan="4" class="method">
<span class="ps-3">{{ method }}</span>
</td>
<td class="text-end text-nowrap script-stats">
<span class="badge text-bg-success">{{ stats.success }}</span>
<span class="badge text-bg-info">{{ stats.info }}</span>
<span class="badge text-bg-warning">{{ stats.warning }}</span>
<span class="badge text-bg-danger">{{ stats.failure }}</span>
</td>
</tr>
{% endfor %}
{% if last_job.data.logs %}
{% for method, stats in last_job.data.logs.items %}
<tr>
<td colspan="4" class="method">
<span class="ps-3">{{ method }}</span>
</td>
<td class="text-end text-nowrap script-stats">
<span class="badge text-bg-success">{{ stats.success }}</span>
<span class="badge text-bg-info">{{ stats.info }}</span>
<span class="badge text-bg-warning">{{ stats.warning }}</span>
<span class="badge text-bg-danger">{{ stats.failure }}</span>
</td>
</tr>
{% 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 %}
{% endfor %}
{% endwith %}