view client/src/lib/classifications.js @ 4357:e8af2ed8666e

refactor classification of Nash sutcliffe for better readability
author Thomas Junk <thomas.junk@intevation.de>
date Mon, 09 Sep 2019 16:12:43 +0200
parents 767a6500a666
children a66275cf4490
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>
 */

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

import store from "@/store/index";

const getGauge = f => {
  if (f.getId().indexOf("bottlenecks") > -1) {
    const GaugeProxy = (function(feature) {
      const gauge = feature.get("gauge_obj");
      const get = function(propName) {
        if (gauge) return gauge.get(propName);
        return feature.get(propName);
      };
      return {
        get: get
      };
    })(f);
    return GaugeProxy;
  }
  return f;
};

const calcForecastVsRealityForNSC = nsc => {
  const hasSamples =
    nsc && nsc.coeffs.reduce((sum, coeff) => sum + coeff.samples, 0);
  if (hasSamples) {
    const [nsc24h, _, nsc72h] = nsc.coeffs;
    // 24h < configured value
    if (
      nsc24h.samples &&
      nsc24h.value <
        store.state.application.config.gm_forecast_vs_reality_nsc_24h
    )
      return "DANGER";
    // 72h < configured value
    if (
      nsc72h.samples &&
      nsc72h.value <
        store.state.application.config.gm_forecast_vs_reality_nsc_72h
    )
      return "WARNING";
    // both > configured value
    return "OK";
  }
  // no data available
  return "NEUTRAL";
};

export default {
  surveyRecency(bottleneck) {
    if (
      bottleneck.get("revisiting_time") === null ||
      bottleneck.get("revisiting_time") === 0
    ) {
      return "NEUTRAL";
    }
    if (bottleneck.get("date_max") === null) {
      return "DANGER";
    }
    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 "OK";
    } else if (revTime < diff && diff <= revTime * revTimeMultiplier) {
      return "WARNING";
    } else if (revTime * revTimeMultiplier < diff) {
      return "DANGER";
    }
  },
  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 "OK";
      }
      return "WARNING";
    }
    return "DANGER";
  },
  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 "DANGER";
      } else if (fa3d > store.state.application.config.gm_forecast_offset_72h) {
        return "WARNING";
      } else {
        return "OK";
      }
    }
    return "NEUTRAL";
  },
  forecastVsReality(feature) {
    let gauge = getGauge(feature);
    let nsc = gauge.get("nsc_data");
    return calcForecastVsRealityForNSC(nsc);
  },
  calcForecastVsRealityForNSC: calcForecastVsRealityForNSC
};