1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Record script execution time

This commit is contained in:
Jeremy Stretch
2019-08-14 10:12:30 -04:00
parent 30e14db881
commit c562af3a13
3 changed files with 26 additions and 4 deletions

View File

@ -1,6 +1,7 @@
from collections import OrderedDict
import inspect
import pkgutil
import time
from django import forms
from django.conf import settings
@ -220,10 +221,14 @@ def run_script(script, data, commit=True):
exists outside of the Script class to ensure it cannot be overridden by a script author.
"""
output = None
start_time = None
end_time = None
try:
with transaction.atomic():
start_time = time.time()
output = script.run(data)
end_time = time.time()
if not commit:
raise AbortTransaction()
except AbortTransaction:
@ -239,7 +244,13 @@ def run_script(script, data, commit=True):
"Database changes have been reverted automatically."
)
return output
# Calculate execution time
if end_time is not None:
execution_time = end_time - start_time
else:
execution_time = None
return output, execution_time
def get_scripts():

View File

@ -402,14 +402,16 @@ class ScriptView(PermissionRequiredMixin, View):
script = self._get_script(module, name)
form = script.as_form(request.POST)
output = None
execution_time = None
if form.is_valid():
commit = form.cleaned_data.pop('_commit')
run_script(script, form.cleaned_data, commit)
output, execution_time = run_script(script, form.cleaned_data, commit)
return render(request, 'extras/script.html', {
'module': module,
'script': script,
'form': form,
'output': output,
'execution_time': execution_time,
})

View File

@ -30,12 +30,12 @@
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="run">
{% if script.log %}
{% if execution_time %}
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<strong>Script Output</strong>
<strong>Script Log</strong>
</div>
<table class="table table-hover panel-body">
<tr>
@ -49,8 +49,17 @@
<td>{% log_level level %}</td>
<td>{{ message }}</td>
</tr>
{% empty %}
<tr>
<td colspan="3" class="text-center text-muted">
No log output
</td>
</tr>
{% endfor %}
</table>
<div class="panel-footer text-right text-muted">
<small>Exec time: {{ execution_time|floatformat:3 }}s</small>
</div>
</div>
</div>
</div>