Mercurial > gemma
view client/src/store/gauges.js @ 2787:2ce5ee6d65b5
client:translations: merge translations from weblate
author | Fadi Abbud <fadi.abbud@intevation.de> |
---|---|
date | Mon, 25 Mar 2019 10:34:33 +0100 |
parents | 2feb9f8f6c66 |
children | 2b79c0871138 |
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"; import { isPast } from "date-fns"; let dateFrom = new Date(); dateFrom.setDate(dateFrom.getDate() - 30); let dateTo = new Date(); dateTo.setDate(dateTo.getDate() + 3); const init = () => { return { gauges: [], selectedGaugeISRS: null, waterlevels: [], meanWaterlevels: [], nashSutcliffe: null, dateFrom: dateFrom, dateTo: dateTo }; }; 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; }, meanWaterlevels: (state, data) => { state.meanWaterlevels = 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 }; }) .filter(wl => !(wl.predicted && isPast(wl.date))); data = data.sort((a, b) => a.date - b.date); commit("waterlevels", data); resolve(data); }) .catch(error => { commit("waterlevels", []); reject(error); }); }); }, loadMeanWaterlevels({ /*state,*/ commit }) { return new Promise(resolve => { setTimeout(() => { commit("meanWaterlevels", [1]); resolve(); }, 2000); }); // return new Promise((resolve, reject) => { // HTTP.get(`/data/mean-waterlevels/${state.selectedGaugeISRS}`, { // headers: { "X-Gemma-Auth": localStorage.getItem("token") } // }) // .then(response => { // commit("meanWaterlevels", response.data); // resolve(response.data); // }) // .catch(error => { // commit("meanWaterlevels", []); // 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); resolve(response.data); }) .catch(error => { commit("nashSutcliffe", null); reject(error); }); }); } } };