view client/src/store/gauges.js @ 2751:5da81634bdc4

client: waterlevel diagram: implemented nash-sutcliffe Since there is no real data available currently, I left a commented block of demo data in client/src/store/gauges.js:L139. Uncomment that block to see the coefficients in the diagram.
author Markus Kottlaender <markus@intevation.de>
date Thu, 21 Mar 2019 12:33:43 +0100
parents 7dcca9649a61
children 71e7237110ba
line wrap: on
line source

/* This is Free Software under GNU Affero General Public License v >= 3.0
 * without warranty, see README.md and license for details.
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 * License-Filename: LICENSES/AGPL-3.0.txt
 *
 * Copyright (C) 2018 by via donau
 *   – Österreichische Wasserstraßen-Gesellschaft mbH
 * Software engineering by Intevation GmbH
 *
 * Author(s):
 * Markus Kottländer <markus@intevation.de>
 */
import { HTTP } from "@/lib/http";
import { WFS } from "ol/format.js";

let dateFrom = new Date();
dateFrom.setDate(dateFrom.getDate() - 30);

const init = () => {
  return {
    gauges: [],
    selectedGaugeISRS: null,
    waterlevels: [],
    nashSutcliffe: null,
    dateFrom: dateFrom,
    dateTo: new Date()
  };
};

export default {
  init,
  namespaced: true,
  state: init(),
  getters: {
    selectedGauge: state => {
      return state.gauges.find(
        g => g.properties.isrs_code === state.selectedGaugeISRS
      );
    }
  },
  mutations: {
    gauges: (state, gauges) => {
      state.gauges = gauges;
    },
    selectedGaugeISRS: (state, isrs) => {
      state.selectedGaugeISRS = isrs;
    },
    waterlevels: (state, data) => {
      state.waterlevels = data;
    },
    nashSutcliffe: (state, data) => {
      state.nashSutcliffe = data;
    },
    dateFrom: (state, dateFrom) => {
      state.dateFrom = dateFrom;
    },
    dateTo: (state, dateTo) => {
      state.dateTo = dateTo;
    }
  },
  actions: {
    selectedGaugeISRS: ({ commit }, isrs) => {
      commit("selectedGaugeISRS", isrs);
      commit("application/showGauges", true, { root: true });
    },
    loadGauges: ({ commit }) => {
      return new Promise((resolve, reject) => {
        var gaugesFeatureCollectionRequest = new WFS().writeGetFeature({
          srsName: "EPSG:4326",
          featureNS: "gemma",
          featurePrefix: "gemma",
          featureTypes: ["gauges_geoserver"],
          outputFormat: "application/json"
        });
        HTTP.post(
          "/internal/wfs",
          new XMLSerializer().serializeToString(gaugesFeatureCollectionRequest),
          {
            headers: {
              "X-Gemma-Auth": localStorage.getItem("token"),
              "Content-type": "text/xml; charset=UTF-8"
            }
          }
        )
          .then(response => {
            commit("gauges", response.data.features);
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    loadWaterlevels({ state, commit }) {
      // include the last day
      let dateTo = new Date(state.dateTo.getTime() + 86400);

      return new Promise((resolve, reject) => {
        HTTP.get(
          `/data/waterlevels/${
            state.selectedGaugeISRS
          }?from=${state.dateFrom
            .toISOString()
            .substr(0, 23)}&to=${dateTo.toISOString().substr(0, 23)}`,
          {
            headers: { "X-Gemma-Auth": localStorage.getItem("token") }
          }
        )
          .then(response => {
            let data = response.data
              .split("\n")
              .filter(wl => wl)
              .map(wl => {
                wl = wl.split(",");
                return {
                  date: new Date(wl[0]),
                  waterlevel: Number(wl[1]),
                  predicted: wl[2] === "f" ? false : true
                };
              });
            data = data.sort((a, b) => a.date - b.date);
            commit("waterlevels", data);
            resolve(data);
          })
          .catch(error => {
            commit("waterlevels", []);
            reject(error);
          });
      });
    },
    loadNashSutcliffe({ state, commit }) {
      return new Promise((resolve, reject) => {
        HTTP.get(`/data/nash-sutcliffe/${state.selectedGaugeISRS}`, {
          headers: { "X-Gemma-Auth": localStorage.getItem("token") }
        })
          .then(response => {
            commit("nashSutcliffe", response.data);
            // dest data
            // commit("nashSutcliffe", {
            //   when: "2019-03-20T10:38:05.687",
            //   coeffs: [
            //     {
            //       value: 0.814,
            //       samples: 18,
            //       hours: 24
            //     },
            //     {
            //       value: 0.319,
            //       samples: 36,
            //       hours: 48
            //     },
            //     {
            //       value: -0.20546757,
            //       samples: 54,
            //       hours: 72
            //     }
            //   ]
            // });
            resolve(response.data);
          })
          .catch(error => {
            commit("nashSutcliffe", null);
            reject(error);
          });
      });
    }
  }
};