diff client/src/store/gauges.js @ 4291:81ab34bd2d0d

Legend: Improve display of data availability In order to show Nash Sutcliffe data for bottleneckts the according gauge has to be determined. Every gauge is loaded at initial startup, data will be available when necessary for identification of bottlenecks. The Nash Sutcliffe data is lazy loaded and used here. Perhaps we could use this cache for the style layer too.
author Thomas Junk <thomas.junk@intevation.de>
date Fri, 30 Aug 2019 12:11:35 +0200
parents d85d8de8c28c
children 18a34d9b289c
line wrap: on
line diff
--- a/client/src/store/gauges.js	Fri Aug 30 11:30:11 2019 +0200
+++ b/client/src/store/gauges.js	Fri Aug 30 12:11:35 2019 +0200
@@ -11,9 +11,17 @@
  * Author(s):
  * Markus Kottländer <markus@intevation.de>
  */
+import Vue from "vue";
 import { HTTP } from "@/lib/http";
 import { WFS } from "ol/format";
-import { isPast, startOfDay, endOfDay, format } from "date-fns";
+import {
+  isPast,
+  startOfDay,
+  endOfDay,
+  format,
+  isBefore,
+  addMinutes
+} from "date-fns";
 
 let dateFrom = new Date();
 dateFrom.setDate(dateFrom.getDate() - 30);
@@ -31,6 +39,7 @@
     yearWaterlevels: [],
     nashSutcliffe: null,
     nashSutcliffeOverview: [],
+    nashSutcliffeCache: {},
     dateFrom: dateFrom,
     dateTo: dateTo,
     yearCompare: new Date().getFullYear()
@@ -54,6 +63,13 @@
     }
   },
   mutations: {
+    addNSCtoCache: (state, entry) => {
+      const { isrsCode, nsc, timestamp } = entry;
+      Vue.set(state.nashSutcliffeCache, isrsCode, {
+        nsc,
+        timestamp
+      });
+    },
     gauges: (state, gauges) => {
       state.gauges = gauges;
     },
@@ -100,6 +116,35 @@
     }
   },
   actions: {
+    getNashSutcliffeForISRS: ({ state, commit }, isrsCode) => {
+      return new Promise((resolve, reject) => {
+        const now = new Date();
+        let nashSutcliffe = state.nashSutcliffeCache[isrsCode];
+        if (nashSutcliffe) {
+          const { timestamp, nsc } = nashSutcliffe;
+          const isRecent = isBefore(now, addMinutes(timestamp, 15));
+          if (isRecent) {
+            resolve(nsc);
+            return;
+          }
+        }
+        HTTP.get(`/data/nash-sutcliffe/${isrsCode}`, {
+          headers: { "X-Gemma-Auth": localStorage.getItem("token") }
+        })
+          .then(response => {
+            nashSutcliffe = response.data;
+            commit("addNSCtoCache", {
+              isrsCode,
+              nsc: nashSutcliffe,
+              timestamp: now
+            });
+            resolve(nashSutcliffe);
+          })
+          .catch(error => {
+            reject(error);
+          });
+      });
+    },
     selectedGaugeISRS: ({ commit, dispatch, state }, isrs) => {
       if (state.selectedGaugeISRS !== isrs) {
         commit("selectedGaugeISRS", isrs);