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 from collections import OrderedDict
import inspect import inspect
import pkgutil import pkgutil
import time
from django import forms from django import forms
from django.conf import settings 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. exists outside of the Script class to ensure it cannot be overridden by a script author.
""" """
output = None output = None
start_time = None
end_time = None
try: try:
with transaction.atomic(): with transaction.atomic():
start_time = time.time()
output = script.run(data) output = script.run(data)
end_time = time.time()
if not commit: if not commit:
raise AbortTransaction() raise AbortTransaction()
except AbortTransaction: except AbortTransaction:
@ -239,7 +244,13 @@ def run_script(script, data, commit=True):
"Database changes have been reverted automatically." "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(): def get_scripts():

View File

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

View File

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