From 0c85b6b27f948c72604c223460e6c1ef2a6eae57 Mon Sep 17 00:00:00 2001
From: Nico Vermaas <vermaas@astron.nl>
Date: Mon, 31 Jan 2022 10:47:28 +0100
Subject: [PATCH] https://support.astron.nl/jira/browse/SDC-499 add service
 monitor to monitoring page

---
 atdb/rollback.bat                                 |  1 +
 atdb/taskdatabase/models.py                       | 15 +++++++++++++++
 atdb/taskdatabase/services/algorithms.py          |  7 +++++--
 atdb/taskdatabase/static/taskdatabase/style.css   |  2 +-
 .../templates/taskdatabase/index.html             |  2 +-
 atdb/taskdatabase/views.py                        |  4 ++--
 6 files changed, 25 insertions(+), 6 deletions(-)
 create mode 100644 atdb/rollback.bat

diff --git a/atdb/rollback.bat b/atdb/rollback.bat
new file mode 100644
index 00000000..dc8c5fa1
--- /dev/null
+++ b/atdb/rollback.bat
@@ -0,0 +1 @@
+python manage.py migrate taskdatabase 0010 --settings=atdb.settings.dev
\ No newline at end of file
diff --git a/atdb/taskdatabase/models.py b/atdb/taskdatabase/models.py
index e97856d8..4aa07ef4 100644
--- a/atdb/taskdatabase/models.py
+++ b/atdb/taskdatabase/models.py
@@ -145,3 +145,18 @@ class Monitor(models.Model):
     # the representation of the value in the REST API
     def __str__(self):
         return str(self.name)+' - '+str(self.timestamp) + ' - ' + self.status
+
+
+class LatestMonitor(models.Model):
+    name = models.CharField(max_length=50, default="unknown")
+    type = models.CharField(max_length=20, default="ldv-service", null=True, blank=True)
+    timestamp = models.DateTimeField(default=datetime.utcnow, blank=True)
+    hostname = models.CharField(max_length=50, default="unknown")
+    process_id = models.IntegerField(null=True, blank=True)
+    description = models.CharField(max_length=255, blank=True, null=True)
+    status = models.CharField(max_length=50, default="ok", null=True)
+    metadata = models.JSONField(null=True, blank=True)
+
+    # the representation of the value in the REST API
+    def __str__(self):
+        return str(self.name)+' - '+str(self.timestamp) + ' - ' + self.status
\ No newline at end of file
diff --git a/atdb/taskdatabase/services/algorithms.py b/atdb/taskdatabase/services/algorithms.py
index a1690309..f2784480 100644
--- a/atdb/taskdatabase/services/algorithms.py
+++ b/atdb/taskdatabase/services/algorithms.py
@@ -209,10 +209,13 @@ def convert_monitor_to_html(monitor_data):
             key = record.name
             value = record.status
             filter = record.description
-            line = '<tr class="' + record.status + '" >'
+            line = '<tr>'
+            if "error" in record.status:
+                line = '<tr class="' + record.status + '" >'
+
             line += "<td><b>" + str(record.name) + "</b></td>"
             line += "<td>" + str(record.hostname) + "</td>"
-            line += "<td>" + str(record.status) + "</td>"
+            line += '<td class="' + record.status + '" >' + str(record.status) + "</td>"
             line += "<td>" + str(record.timestamp) + "</td>"
             line += "<td>" + str(record.process_id) + "</td>"
             line += "<td>" + str(record.description) + "</td>"
diff --git a/atdb/taskdatabase/static/taskdatabase/style.css b/atdb/taskdatabase/static/taskdatabase/style.css
index 3b9a2cdd..2bbe0efe 100644
--- a/atdb/taskdatabase/static/taskdatabase/style.css
+++ b/atdb/taskdatabase/static/taskdatabase/style.css
@@ -39,7 +39,7 @@ TD {
   font-weight: bold;
 }
 
-.processed {
+.processed,.ok {
  color: green;
  font-weight: bold;
 }
diff --git a/atdb/taskdatabase/templates/taskdatabase/index.html b/atdb/taskdatabase/templates/taskdatabase/index.html
index 60a3fc26..9933d22e 100644
--- a/atdb/taskdatabase/templates/taskdatabase/index.html
+++ b/atdb/taskdatabase/templates/taskdatabase/index.html
@@ -80,7 +80,7 @@
         {% include 'taskdatabase/pagination.html' %}
        </div>
     </div>
-    <p class="footer"> Version 1.0.0 (28 jan 2021 - 14:00)
+    <p class="footer"> Version 1.0.0 (31 jan 2021 - 10:00)
 
 </div>
 
diff --git a/atdb/taskdatabase/views.py b/atdb/taskdatabase/views.py
index af5af31d..a1afc585 100644
--- a/atdb/taskdatabase/views.py
+++ b/atdb/taskdatabase/views.py
@@ -326,9 +326,9 @@ def WorkflowDetails(request, id):
 
 def ShowMonitoring(request):
     # get the latest value of each unique combination of service name and hostname.
-    monitor_data = Monitor.objects.all()
+    distinct_services_per_host = Monitor.objects.all().order_by('name', 'hostname', '-timestamp').distinct('name', 'hostname')
 
-    monitor_results = algorithms.convert_monitor_to_html(monitor_data)
+    monitor_results = algorithms.convert_monitor_to_html(distinct_services_per_host)
     return render(request, "taskdatabase/monitoring.html", {'monitor_results': monitor_results})
 
 
-- 
GitLab