Sfoglia il codice sorgente

Record script execution time

Jeremy Stretch 6 anni fa
parent
commit
c562af3a13
3 ha cambiato i file con 26 aggiunte e 4 eliminazioni
  1. 12 1
      netbox/extras/scripts.py
  2. 3 1
      netbox/extras/views.py
  3. 11 2
      netbox/templates/extras/script.html

+ 12 - 1
netbox/extras/scripts.py

@@ -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():

+ 3 - 1
netbox/extras/views.py

@@ -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,
         })

+ 11 - 2
netbox/templates/extras/script.html

@@ -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>