view client/src/store/fairwayavailability.js @ 3347:cd745be63f71

available_fairway_depth: draw lower levels refactored
author Thomas Junk <thomas.junk@intevation.de>
date Tue, 21 May 2019 11:48:15 +0200
parents 8c96b7379aea
children dcae9b3e864b
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):
 * Thomas Junk <thomas.junk@intevation.de>
 */

import { HTTP } from "@/lib/http";
import { format, subYears } from "date-fns";

const FREQUENCIES = {
  MONTHLY: "monthly",
  QUARTERLY: "quarterly",
  YEARLY: "yearly"
};

/**
 * Maximum number of hours:
 * Month: approx 744/720/672 hours for 31/30/21 days
 * Quarter: aprrox 2160 per quarter (~90 days)
 * Yearly: approx 8760 per year (~365 days)
 */
const MOCKDATA = {
  [FREQUENCIES.MONTHLY]: `
#label,# >= LDC [h],# < 200.00 [h],# >= 200.00 [h],# >= 230.00 [h],# >= 250.00 [h]
05-2018,670.000,450.000,70.000,50.000,100.000
06-2018,669.000,546.000,0.000,0.000,123.000
07-2018,671.000,377.000,0.000,0.000,294.000
08-2018,668.000,168.000,0.000,0.000,500.000
09-2018,673.000,23.000,0.000,0.000,650.000
10-2018,670.000,4.000,0.000,0.000,666.000
11-2018,672.000,1.000,0.000,0.000,671.000
12-2018,675.000,5.000,0.000,0.000,670.000
01-2019,677.000,0.000,0.000,0.000,677.000
02-2019,668.000,43.000,0.000,0.000,625.000
03-2019,660.000,49.000,0.000,0.000,611.000
04-2019,620.000,20.000,0.000,0.000,600.000
05-2019,672.000,42.000,0.000,0.000,630.000`,
  [FREQUENCIES.QUARTERLY]: `
#label,# >= LDC [h],# < 200.00 [h],# >= 200.00 [h],# >= 230.00 [h],# >= 250.00 [h]
Q2-2018,989.000, 500.000,240.000,260.000,1823.000
Q3-2018,1108.000,500.000,180.000,320.000,1956.000
Q3-2018,1145.000,500.000,250.000,250.000,2001.000
Q1-2019,1155.000,500.000,400.000,100.000,2021.000
Q2-2019,1160.000,500.000,300.000,200.000,1998.000`,
  [FREQUENCIES.YEARLY]: `
#label,# >= LDC [h],# < 200.00 [h],# >= 200.00 [h],# >= 230.00 [h],# >= 250.00 [h]
2018,8360.000,0.000,0.000,0.000,7360.000
2019,8153.000,0.000,0.000,0.000,7250.000`
};

const init = () => {
  return {
    type: "bottlenecks",
    selectedFairwayAvailabilityFeature: null,
    to: format(new Date(), "YYYY-MM-DD"),
    from: format(subYears(new Date(), 1), "YYYY-MM-DD"),
    frequency: FREQUENCIES.MONTHLY,
    fwData: null,
    fwLNWLData: null,
    legend: null,
    LOS: 3
  };
};

const fairwayavailability = {
  init,
  namespaced: true,
  state: init(),
  mutations: {
    type: (state, type) => {
      state.type = type;
    },
    setLOS: (state, LOS) => {
      state.LOS = LOS;
    },
    setFrequency: (state, frequency) => {
      state.frequency = frequency;
    },
    setFrom: (state, from) => {
      state.from = from;
    },
    setTo: (state, to) => {
      state.to = to;
    },
    setSelectedFairwayAvailability: (state, feature) => {
      state.selectedFairwayAvailabilityFeature = feature;
    },
    setFwData: (state, fwData) => {
      state.fwData = fwData;
    },
    setLegend: (state, header) => {
      const headerEntries = header.split(",");
      headerEntries.shift();
      state.legend = headerEntries.map(x => {
        let entry = x.split("#")[1]; // split leading #
        entry = entry.replace("[h]", "").trim(); // omit unit
        return entry;
      });
    }
  },
  actions: {
    loadAvailableFairwayDepth: ({ commit }, options) => {
      return new Promise((resolve, reject) => {
        const { feature, from, to, frequency, LOS } = options;
        let name =
          feature.constructor.name === "Feature"
            ? feature.get("objnam")
            : feature.properties.name;
        const start = encodeURIComponent("00:00:00+00:00");
        const end = encodeURIComponent("23:59:59+00:00");
        const URL = `/data/bottleneck/fairway-depth/${encodeURIComponent(
          name
        )}?from=${from}T${start}&to=${to}T${end}&mode=${frequency}&los=${LOS}`;
        HTTP.get(URL, {
          headers: { "X-Gemma-Auth": localStorage.getItem("token") }
        })
          .then(response => {
            //const { data } = response;
            const data = MOCKDATA[frequency];
            console.log(data);
            const csv = data.split("\n").filter(x => x !== ""); //omit empty lines
            commit("setLegend", csv.shift());
            let transformed = csv.map(e => {
              const result = e.split(",");
              const label = result.shift();
              const ldc = result.shift();
              const highestLevel = result.pop();
              const sum = result.reduce((x, y) => Number(x) + Number(y));
              let last = 0;
              const levelsWithSum = result.map(x => {
                let result = {
                  height: Number(x),
                  translateY: sum - (last + Number(x))
                };
                last = last + Number(x);
                return result;
              });
              return {
                label: label,
                ldc: ldc,
                highestLevel: highestLevel,
                lowerLevels: levelsWithSum
              };
            });
            commit("setFwData", transformed);
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    }
  }
};

export { FREQUENCIES, fairwayavailability };