view client/src/store/fairwayavailability.js @ 5560:f2204f91d286

Join the log lines of imports to the log exports to recover data from them. Used in SR export to extract information that where in the meta json but now are only found in the log.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 09 Feb 2022 18:34:40 +0100
parents 791a372553a0
children
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, 2019 by via donau
 *   – Österreichische Wasserstraßen-Gesellschaft mbH
 * Software engineering by Intevation GmbH
 *
 * Author(s):
 * Thomas Junk <thomas.junk@intevation.de>
 */

/*eslint no-unused-vars: ["error", { "varsIgnorePattern": "_" }]*/

import {
  endOfMonth,
  endOfQuarter,
  endOfYear,
  format,
  startOfMonth,
  startOfQuarter,
  startOfYear
} from "date-fns";

import { HTTP } from "@/lib/http";

const LIMITINGFACTORS = {
  WIDTH: "width",
  DEPTH: "depth"
};

const TYPES = {
  BOTTLENECK: "bottleneck",
  SECTION: "section",
  STRETCH: "stretch"
};

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

const getIntervallBorders = (start, end, frequency) => {
  switch (frequency) {
    case FREQUENCIES.MONTHLY:
      return [isoFormat(startOfMonth(start)), isoFormat(endOfMonth(end))];
    case FREQUENCIES.YEARLY:
      return [isoFormat(startOfYear(start)), isoFormat(endOfYear(end))];
    case FREQUENCIES.QUARTERLY:
      return [isoFormat(startOfQuarter(start)), isoFormat(endOfQuarter(end))];
    default:
      throw new Error("Boom!");
  }
};

const isoFormat = date => {
  return format(date, "YYYY-MM-DD");
};

const init = () => {
  return {
    type: TYPES.BOTTLENECK,
    selectedFairwayAvailabilityFeature: null,
    to: null,
    from: null,
    frequency: FREQUENCIES.MONTHLY,
    limitingFactor: null,
    depthlimit1: 230,
    depthlimit2: 250,
    widthlimit1: 80,
    widthlimit2: 150,
    csv: null,
    fwData: null,
    fwLNWLData: null,
    fwLNWLOverviewData: [],
    legendLNWL: null,
    legend: null,
    LOS: 3
  };
};

/**
 * transformAFD
 * @param {string} csv
 *
 * takes the afd csv and transforms it to an intermediary format
 * for display of diagrams
 *
 * Incoming csv Format
 * #label,# <LDC ,# >= LDC [h],# < 230.00 [h],# >= 230.00 [h],# >= 250.00 [h]
 * 05-2019,215.500,0.000,0.000,215.500
 *
 * Format:
 * $LABEL, $LDC, $BELOWLIMIT1, $BETWEENLIMIT12, $ABOVELIMIT2^
 *
 * This format is assumed to be fix
 *
 */
const transformAFD = csv => {
  return csv.map(e => {
    const result = e.split(",");
    let label, _, ldc, lower, middle, highestLevel;
    let levelsWithSum;
    if (result.length == 6) {
      [label, _, ldc, lower, middle, highestLevel] = result;
      levelsWithSum = [
        {
          height: Number(lower),
          translateY: Number(middle)
        },
        {
          height: Number(middle),
          translateY: 0
        }
      ];
    } else {
      {
        [label, _, ldc, middle, highestLevel] = result;
        levelsWithSum = [
          {
            height: Number(middle),
            translateY: 0
          }
        ];
      }
    }
    return {
      label: label,
      ldc: ldc,
      highestLevel: highestLevel,
      lowerLevels: levelsWithSum
    };
  });
};

const fairwayavailability = {
  init,
  namespaced: true,
  state: init(),
  getters: {
    fwLNWLOverviewData: state => feature => {
      return state.fwLNWLOverviewData.find(
        d => d.feature.get("id") === feature.get("id")
      );
    }
  },
  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;
    },
    setDepthlimit1: (state, depthlimit1) => {
      state.depthlimit1 = depthlimit1;
    },
    setDepthlimit2: (state, depthlimit2) => {
      state.depthlimit2 = depthlimit2;
    },
    setWidthlimit1: (state, widthlimit1) => {
      state.widthlimit1 = widthlimit1;
    },
    setWidthlimit2: (state, widthlimit2) => {
      state.widthlimit2 = widthlimit2;
    },
    setSelectedFairwayAvailability: (state, feature) => {
      state.selectedFairwayAvailabilityFeature = feature;
    },
    addFwLNWLOverviewData: (state, data) => {
      let existingIndex = state.fwLNWLOverviewData.findIndex(
        d => d.feature.get("id") === data.feature.get("id")
      );
      if (existingIndex !== -1)
        state.fwLNWLOverviewData.splice(existingIndex, 1);
      state.fwLNWLOverviewData.push(data);
    },
    setLegendLNWL: (state, headerLNWL) => {
      state.headerLNWL = headerLNWL;
    },
    // See docs/developers.md for an example how to directly
    // call this method for testing.
    setAvailableFairwayDepthData: (state, data) => {
      const lines = data.split("\n");
      let [header, rest] = [lines.shift(), lines];
      // convert values to meter in the csv header
      state.csv = [
        header.replace(/\d+\.?\d*/g, x => `${x / 100}m`),
        ...rest
      ].join("\n");
      const csv = [header, ...rest].filter(x => x !== ""); //omit empty lines

      // setLegend
      const headerEntries = csv.shift().split(",");
      headerEntries.shift();
      headerEntries.shift();
      state.legend = headerEntries.map(x => {
        let entry = x.split("#")[1]; // split leading #
        entry = entry.replace("[h]", "").trim(); // omit unit
        return entry;
      });

      state.fwData = transformAFD(csv);
    },
    // See docs/developers.md for an example how to directly
    // call this method for testing.
    setAvailableFairwayDepthLNWLData: (state, data) => {
      state.csv = data;

      data = data.split("\n").filter(d => d);
      data.shift(); // remove header line
      data = data.map(d => {
        let columns = d.split(",");
        let result;
        if (columns.length === 6) {
          result = {
            date: columns[0],
            ldc: Number(columns[2]),
            below: Number(columns[3]),
            between: Number(columns[4]),
            above: Number(columns[5])
          };
        } else {
          result = {
            date: columns[0],
            ldc: Number(columns[2]),
            below: Number(columns[3]),
            between: null,
            above: Number(columns[4])
          };
        }
        return result;
      });
      state.fwLNWLData = data;
    }
  },
  actions: {
    loadAvailableFairwayDepth: ({ commit }, options) => {
      return new Promise((resolve, reject) => {
        const {
          feature,
          frequency,
          LOS,
          depthLimit1,
          depthLimit2,
          widthLimit1,
          widthLimit2,
          limitingFactor,
          type
        } = options;
        let { from, to } = options;
        let name = "";
        if (type === TYPES.BOTTLENECK) {
          name = feature.hasOwnProperty("properties")
            ? feature.properties.bottleneck_id
            : feature.get("bottleneck_id");
        } else {
          name = feature.hasOwnProperty("properties")
            ? feature.properties.name
            : feature.get("objnam");
        }
        [from, to] = getIntervallBorders(from, to, frequency);
        let additionalParams = "";
        let endpoint = type;
        if (type === TYPES.BOTTLENECK) {
          if (limitingFactor === LIMITINGFACTORS.DEPTH)
            additionalParams = `&breaks=${depthLimit1},${depthLimit2}`;
          if (limitingFactor === LIMITINGFACTORS.WIDTH)
            additionalParams = `&breaks=${widthLimit1},${widthLimit2}`;
        } else if (type == TYPES.SECTION || type == TYPES.STRETCH) {
          additionalParams = `&depthbreaks=${depthLimit1},${depthLimit2}&widthbreaks=${widthLimit1},${widthLimit2}`;
        }
        const start = encodeURIComponent("00:00:00+00:00");
        const end = encodeURIComponent("23:59:59+00:00");
        const URL = `data/fairway/${endpoint}/${encodeURIComponent(
          name
        )}?from=${from}T${start}&to=${to}T${end}&mode=${frequency}&los=${LOS}${additionalParams}`;
        HTTP.get(URL, {
          headers: { "X-Gemma-Auth": localStorage.getItem("token") }
        })
          .then(response => {
            commit("setAvailableFairwayDepthData", response.data);
            resolve(response);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    loadAvailableFairwayDepthLNWL: (context, options) => {
      return new Promise((resolve, reject) => {
        const {
          feature,
          frequency,
          LOS,
          depthLimit1,
          depthLimit2,
          widthLimit1,
          widthLimit2,
          limitingFactor,
          type
        } = options;
        let { from, to } = options;
        let name = "";
        if (type === TYPES.BOTTLENECK) {
          name = feature.hasOwnProperty("properties")
            ? feature.properties.bottleneck_id
            : feature.get("bottleneck_id");
        } else {
          name = feature.hasOwnProperty("properties")
            ? feature.properties.name
            : feature.get("objnam");
        }
        [from, to] = getIntervallBorders(from, to, frequency);
        const start = encodeURIComponent("00:00:00+00:00");
        const end = encodeURIComponent("23:59:59+00:00");
        let additionalParams = "";
        let endpoint = type || TYPES.BOTTLENECK;
        if (type === TYPES.BOTTLENECK) {
          if (limitingFactor === LIMITINGFACTORS.DEPTH)
            additionalParams = `&breaks=${depthLimit1},${depthLimit2}`;
          if (limitingFactor === LIMITINGFACTORS.WIDTH)
            additionalParams = `&breaks=${widthLimit1},${widthLimit2}`;
        } else if (type == TYPES.SECTION || type == TYPES.STRETCH) {
          additionalParams = `&depthbreaks=${depthLimit1},${depthLimit2}&widthbreaks=${widthLimit1},${widthLimit2}`;
        }
        const URL = `data/availability/${endpoint}/${encodeURIComponent(
          name
        )}?from=${from}T${start}&to=${to}T${end}&mode=${frequency}&los=${LOS}${additionalParams}`;
        HTTP.get(URL, {
          headers: { "X-Gemma-Auth": localStorage.getItem("token") }
        })
          .then(response => {
            const { data } = response;
            resolve(data);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    loadAvailableFairwayDepthLNWLDiagram: ({ commit, dispatch }, options) => {
      dispatch("loadAvailableFairwayDepthLNWL", options).then(data => {
        commit("setAvailableFairwayDepthLNWLData", data);
      });
    },
    loadAvailableFairwayDepthLNWLForMap: ({ dispatch }, options) => {
      return dispatch("loadAvailableFairwayDepthLNWL", options).then(
        response => {
          let data = response.split("\n").filter(d => d);
          data.shift(); // remove header line
          data = data.map(d => {
            let columns = d.split(",");
            return {
              ldc: Number(columns[2]),
              below: Number(columns[3]),
              between: Number(columns[4]),
              above: Number(columns[5])
            };
          });
          return data[0];
        }
      );
    }
  }
};

export { LIMITINGFACTORS, FREQUENCIES, TYPES, fairwayavailability };