view client/src/lib/classifications.js @ 3678:8f58851927c0

client: make layer factory only return new layer config for individual maps instead of each time it is invoked. The purpose of the factory was to support multiple maps with individual layers. But returning a new config each time it is invoked leads to bugs that rely on the layer's state. Now this factory reuses the same objects it created before, per map.
author Markus Kottlaender <markus@intevation.de>
date Mon, 17 Jun 2019 17:31:35 +0200
parents bcf9713ee359
children 2596a028dc3a
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):
 * Raimund Renkert <raimund.renkert@intevation.de>
 * Markus Kottländer <markus.kottlaender@intevation.de>
 */
import store from "@/store/index";

const getGauge = f => {
  if (f.getId().indexOf("bottlenecks") > -1) {
    return f.get("gauge_obj");
  }
  return f;
};

export default {
  surveyCurrency(bottleneck) {
    if (
      bottleneck.get("revisiting_time") === null ||
      bottleneck.get("revisiting_time") === 0
    ) {
      return "white";
    }
    if (bottleneck.get("date_max") === null) {
      return "red";
    }
    let revTime = bottleneck.get("revisiting_time") * 30.5;
    let latest = Date.parse(bottleneck.get("date_max").replace("Z", ""));
    var diff = Math.floor((Date.now() - latest) / 86400000);
    let revTimeMultiplier =
      store.state.application.config.bn_revtime_multiplier;
    if (diff <= revTime) {
      return "lime";
    } else if (revTime < diff && diff <= revTime * revTimeMultiplier) {
      return "yellow";
    } else if (revTime * revTimeMultiplier < diff) {
      return "red";
    }
  },
  gmAvailability(feature) {
    let gauge = getGauge(feature);
    let gmDate = gauge.get("gm_measuredate");
    let gmN = gauge.get("gm_n_14d");
    if (
      gmDate !== undefined &&
      gmDate !== null &&
      // latest measurement within configured hours
      Date.parse(gmDate) >
        Date.now() -
          store.state.application.config.gm_latest_hours * 60 * 60 * 1000
    ) {
      // at least configured amount of measurements in last 14 days
      const valuesAtLeast = store.state.application.config.gm_min_values_14d;
      if (gmN !== undefined && gmN !== null && gmN >= valuesAtLeast) {
        return "lime";
      }
      return "yellow";
    }
    return "red";
  },
  forecastAccuracy(feature) {
    let gauge = getGauge(feature);
    let fa3d = gauge.get("forecast_accuracy_3d");
    let fa1d = gauge.get("forecast_accuracy_1d");
    if (typeof fa3d == "number" && typeof fa1d == "number") {
      if (fa1d > store.state.application.config.gm_forecast_offset_24h) {
        return "red";
      } else if (fa3d > store.state.application.config.gm_forecast_offset_72h) {
        return "yellow";
      } else {
        return "lime";
      }
    }
    return "white";
  },
  forecastVsReality(feature) {
    let gauge = getGauge(feature);
    let nsc = gauge.get("nsc_data");
    if (nsc && nsc.coeffs.reduce((sum, coeff) => sum + coeff.samples, 0)) {
      // 24h < configured value
      if (
        nsc.coeffs[0].samples &&
        nsc.coeffs[0].value <
          store.state.application.config.gm_forecast_vs_reality_nsc_24h
      )
        return "red";
      // 72h < configured value
      if (
        nsc.coeffs[2].samples &&
        nsc.coeffs[2].value <
          store.state.application.config.gm_forecast_vs_reality_nsc_72h
      )
        return "yellow";
      // both > configured value
      return "lime";
    }
    // no data available
    return "white";
  }
};