From 4049c8e915b3b84b5ee6d1f8edf470bcfbde7abb Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Thu, 19 Nov 2020 12:35:09 +1100
Subject: [PATCH] Adds framework for "server health" display

- Adds global context object "system_healthy" (boolean)
- Framework for running system health checks
- Updated system info forms
- Displays warning next to user menu if system health errors exist

(cherry picked from commit ce6d626ab39147fc389de90815ca8baae2385d82)
---
 InvenTree/InvenTree/context.py  |  9 ++++++++
 InvenTree/InvenTree/settings.py |  1 +
 InvenTree/InvenTree/status.py   | 39 +++++++++++++++++++++++++++++++++
 InvenTree/InvenTree/views.py    |  2 +-
 InvenTree/templates/about.html  |  5 -----
 InvenTree/templates/navbar.html | 15 +++++++++++--
 InvenTree/templates/stats.html  | 27 +++++++++++++++++++++++
 7 files changed, 90 insertions(+), 8 deletions(-)
 create mode 100644 InvenTree/InvenTree/status.py

diff --git a/InvenTree/InvenTree/context.py b/InvenTree/InvenTree/context.py
index aa66402ed6..8753749e3f 100644
--- a/InvenTree/InvenTree/context.py
+++ b/InvenTree/InvenTree/context.py
@@ -7,9 +7,18 @@ Provides extra global data to all templates.
 from InvenTree.status_codes import SalesOrderStatus, PurchaseOrderStatus
 from InvenTree.status_codes import BuildStatus, StockStatus
 
+import InvenTree.status
+
 from users.models import RuleSet
 
 
+def health_status(request):
+
+    return {
+        "system_healthy": InvenTree.status.check_system_health(),
+    }
+
+
 def status_codes(request):
 
     return {
diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py
index 3fc16464c6..d0695c4b7d 100644
--- a/InvenTree/InvenTree/settings.py
+++ b/InvenTree/InvenTree/settings.py
@@ -227,6 +227,7 @@ TEMPLATES = [
                 'django.template.context_processors.i18n',
                 'django.contrib.auth.context_processors.auth',
                 'django.contrib.messages.context_processors.messages',
+                'InvenTree.context.health_status',
                 'InvenTree.context.status_codes',
                 'InvenTree.context.user_roles',
             ],
diff --git a/InvenTree/InvenTree/status.py b/InvenTree/InvenTree/status.py
new file mode 100644
index 0000000000..ec2422a254
--- /dev/null
+++ b/InvenTree/InvenTree/status.py
@@ -0,0 +1,39 @@
+"""
+Provides system status functionality checks.
+"""
+
+from django.utils.translation import ugettext as _
+
+import logging
+
+
+logger = logging.getLogger(__name__)
+
+
+def check_system_health(**kwargs):
+    """
+    Check that the InvenTree system is running OK.
+
+    Returns True if all system checks pass.
+    """
+
+    result = True
+
+    if not check_celery_worker(**kwargs):
+        result = False
+        logger.warning(_("Celery worker check failed"))
+
+    if not result:
+        logger.warning(_("InvenTree system health checks failed"))
+
+    return result
+
+
+def check_celery_worker(**kwargs):
+    """
+    Check that a celery worker is running.
+    """
+
+    # TODO - Checks that the configured celery worker thing is running
+
+    return True
diff --git a/InvenTree/InvenTree/views.py b/InvenTree/InvenTree/views.py
index b2ef8f965a..d392c4cb62 100644
--- a/InvenTree/InvenTree/views.py
+++ b/InvenTree/InvenTree/views.py
@@ -792,7 +792,7 @@ class DatabaseStatsView(AjaxView):
     """ View for displaying database statistics """
 
     ajax_template_name = "stats.html"
-    ajax_form_title = _("Database Statistics")
+    ajax_form_title = _("System Information")
 
     def get_context_data(self, **kwargs):
 
diff --git a/InvenTree/templates/about.html b/InvenTree/templates/about.html
index 0fbeef8454..80b29742ef 100644
--- a/InvenTree/templates/about.html
+++ b/InvenTree/templates/about.html
@@ -16,11 +16,6 @@
                 <div>
                     <table class='table table-striped table-condensed'>
                         <col width='25'>
-                        <tr>
-                            <td></td>
-                            <td>{% trans "Instance Name" %}</td>
-                            <td>{% inventree_instance_name %}</td>
-                        </tr>
                         <tr>
                             <td><span class='fas fa-hashtag'></span></td>
                             <td>{% trans "InvenTree Version" %}</td><td><a href="https://github.com/inventree/InvenTree/releases">{% inventree_version %}</a></td>
diff --git a/InvenTree/templates/navbar.html b/InvenTree/templates/navbar.html
index 148a96c583..9dafd5769e 100644
--- a/InvenTree/templates/navbar.html
+++ b/InvenTree/templates/navbar.html
@@ -52,7 +52,11 @@
             </button>
           </li>
           <li class='dropdown'>
-            <a class='dropdown-toggle' data-toggle='dropdown' href="#"><span class="fas fa-user"></span> <b>{{ user.get_username }}</b></a>
+            <a class='dropdown-toggle' data-toggle='dropdown' href="#">
+              {% if not system_healthy %}
+              <span title='{% trans "InvenTree server issues detected" %}' class='fas fa-exclamation-triangle icon-red'></span>
+              {% endif %}
+              <span class="fas fa-user"></span> <b>{{ user.get_username }}</b></a>
             <ul class='dropdown-menu'>
                 {% if user.is_authenticated %}
                 {% if user.is_staff %}
@@ -65,8 +69,15 @@
                 <li><a href="{% url 'login' %}"><span class="fas fa-sign-in-alt"></span> {% trans "Login" %}</a></li>
                 {% endif %}
                 <hr>  
+                <li id='launch-stats'><a href='#'>
+                  {% if system_healthy %}
+                  <span class='fas fa-server'>
+                  {% else %}
+                  <span class='fas fa-exclamation-triangle icon-red'>
+                  {% endif %}
+                  </span> {% trans "System Information" %}
+                </a></li>
                 <li id='launch-about'><a href='#'><span class="fas fa-info-circle"></span> {% trans "About InvenTree" %}</a></li>
-                <li id='launch-stats'><a href='#'><span class='fas fa-chart-pie'></span> {% trans "Statistics" %}</a></li>
               </ul>
             </li> 
       </ul>
diff --git a/InvenTree/templates/stats.html b/InvenTree/templates/stats.html
index 5b194f17a5..7b8a9bb93a 100644
--- a/InvenTree/templates/stats.html
+++ b/InvenTree/templates/stats.html
@@ -4,6 +4,33 @@
 
 <table class='table table-striped table-condensed'>
     <col width='25'>
+
+    <tr>
+        <td colspan="3"><b>{% trans "Server" %}</b></td>
+    </tr>
+    <tr>
+        <td><span class='fas fa-server'></span></td>
+        <td>{% trans "Instance Name" %}</td>
+        <td>{% inventree_instance_name %}</td>
+    </tr>
+    <tr>
+        <td><span class='fas fa-exclamation-triangle'></span></td>
+        <td>{% trans "Server status" %}</td>
+        <td>
+            {% if system_healthy %}
+            <span class='label label-green'>{% trans "Healthy" %}</span>
+            {% else %}
+            <span class='label label-red'>{% trans "Issues detected" %}</span>
+            {% endif %}
+        </td>
+    </tr>
+
+    {% if not system_healthy %}
+    {% for issue in system_issues %}
+    <!-- TODO - Enumerate system issues here! -->
+    {% endfor %}
+    {% endif %}
+
     <tr>
         <td colspan='3'><b>{% trans "Parts" %}</b></td>
     </tr>