From ff73d3966c91523d4e8ec6e4deb291a0900dc302 Mon Sep 17 00:00:00 2001
From: Alexander Kaschta <alexander.kaschta9@kit.edu>
Date: Wed, 26 Jul 2023 18:25:06 +0200
Subject: [PATCH] UPD: Add maintenance info (WIP)

---
 frontend/src/App.vue                          | 20 +++++++++++++++++++
 .../src/api-services/system_info.service.js   |  6 ++++++
 frontend/src/util/dateutil.js                 |  5 +++++
 3 files changed, 31 insertions(+)

diff --git a/frontend/src/App.vue b/frontend/src/App.vue
index 0b28c3a9c..5f55507b6 100644
--- a/frontend/src/App.vue
+++ b/frontend/src/App.vue
@@ -129,6 +129,7 @@ import SystemInfoService from './api-services/system_info.service'
 import colorutil from '@/util/colorutil'
 import SystemAlertDisplay from '@/components/SystemAlertDisplay.vue'
 import SpecReady from '@/views/SpecReady.vue'
+import dateutil from '@/util/dateutil'
 
 export default {
   components: { SpecReady, SystemAlertDisplay, GlobalSearch, SideNavbar, Sidebar, NetvsFooter, Navbar, CopyField },
@@ -167,6 +168,25 @@ export default {
         this.$store.commit('updateAlerts', alerts.data)
       }).catch(() => {
       }) // Fail silently
+      SystemInfoService.getMaintenance(this.$store.state.netdb_axios_config).then((items) => {
+        const alerts = this.$store.state.alerts
+        // Iterate over all maintenance announcements and add them to the alerts
+        for (const item of items.data.maint_state_list) {
+          // NOTE: The text generation is only a temporary solution until the api supports multiple languages
+          alerts.push({
+            title: { de: item.subject, en: item.subject },
+            content:
+              {
+                de: 'Von ' + dateutil.format_date(new Date(Date.parse(item.ts_begin))) + ' bis ' + item.ts_end + ' wird es für ' + item.duration + 'h Wartungsarbeiten geben.',
+                en: 'Test'
+              }
+          })
+        }
+        console.log(alerts)
+        // Update the alerts
+        this.$store.commit('updateAlerts', alerts)
+      }).catch(() => {
+      }) // Fail silently
       SystemInfoService.getFrontendVersion().then((sysinfo) => {
         if (config.COMMIT_SHORT_SHA !== sysinfo.data.commit_short) {
           EventBus.$emit('update_available', sysinfo.data.version)
diff --git a/frontend/src/api-services/system_info.service.js b/frontend/src/api-services/system_info.service.js
index 23be60c57..c60e4e7bd 100644
--- a/frontend/src/api-services/system_info.service.js
+++ b/frontend/src/api-services/system_info.service.js
@@ -20,4 +20,10 @@ export default {
   getAlerts() {
     return Axios.get('/api/alerts.json?' + Math.random().toString())
   },
+  getMaintenance(config) {
+    const ta = [
+      { name: 'wapi.maint_state.list', old: { sorting_params_list: ['is_active', 'ts_begin'] } }
+    ]
+    return TransactionService.execute(config, ta)
+  }
 }
diff --git a/frontend/src/util/dateutil.js b/frontend/src/util/dateutil.js
index a012239a6..1ff36da38 100644
--- a/frontend/src/util/dateutil.js
+++ b/frontend/src/util/dateutil.js
@@ -8,5 +8,10 @@ export default {
       fmt[part] = i++
     })
     return new Date(parts[fmt.YYYY], parts[fmt.MM] - 1, parts[fmt.DD], parts[fmt.hh], parts[fmt.mm], parts[fmt.ss], parts[fmt.ms])
+  },
+  format_date(date) {
+    return date.getUTCFullYear() + '-' + String(date.getUTCMonth() + 1).padStart(2, '0') + '-' +
+      String(date.getUTCDate()).padStart(2, '0') + ' ' + String(date.getUTCHours()).padStart(2, '0') +
+      ':' + String(date.getUTCMinutes()).padStart(2, '0') + ':' + String(date.getUTCSeconds()).padStart(2, '0') + ' (UTC)'
   }
 }
-- 
GitLab