diff pkg/controllers/fwa.go @ 5204:7ca9e6c9a203 new-fwa

Introduced type for mode (monthly, quarterly, yearly).
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 11 May 2020 11:03:19 +0200
parents 355195a90298
children c7907715309f
line wrap: on
line diff
--- a/pkg/controllers/fwa.go	Fri May 08 18:59:14 2020 +0200
+++ b/pkg/controllers/fwa.go	Mon May 11 11:03:19 2020 +0200
@@ -20,6 +20,8 @@
 	"log"
 	"net/http"
 	"sort"
+	"strconv"
+	"strings"
 	"time"
 
 	"github.com/gorilla/mux"
@@ -138,8 +140,22 @@
 		validities   limitingValidities
 		measurements availMeasurements
 	}
+
+	fwaMode int
 )
 
+const (
+	fwaMonthly fwaMode = iota
+	fwaQuarterly
+	fwaYearly
+)
+
+// afdRefs are the typical available fairway depth reference values.
+var afdRefs = []float64{
+	230,
+	250,
+}
+
 func (ls ldcs) find(from, to time.Time) *ldc {
 	for _, l := range ls {
 		if l.intersects(from, to) {
@@ -151,6 +167,12 @@
 
 func fairwayAvailability(rw http.ResponseWriter, req *http.Request) {
 
+	mode := parseFWAMode(req.FormValue("mode"))
+
+	// TODO: depending on mode do output stepping
+
+	_ = mode
+
 	from, to, ok := parseFromTo(rw, req)
 	if !ok {
 		return
@@ -300,6 +322,40 @@
 	return dusk(from), dawn(to), true
 }
 
+func parseFWAMode(mode string) fwaMode {
+	switch strings.ToLower(mode) {
+	case "monthly":
+		return fwaMonthly
+	case "quarterly":
+		return fwaQuarterly
+	case "yearly":
+		return fwaYearly
+	default:
+		return fwaMonthly
+	}
+}
+
+func breaksToReferenceValue(breaks string) []float64 {
+	parts := strings.Split(breaks, ",")
+	var values []float64
+
+	for _, part := range parts {
+		part = strings.TrimSpace(part)
+		if v, err := strconv.ParseFloat(part, 64); err == nil {
+			values = append(values, v)
+		}
+	}
+
+	return common.DedupFloat64s(values)
+}
+
+func parseBreaks(breaks string) []float64 {
+	if breaks != "" {
+		return breaksToReferenceValue(breaks)
+	}
+	return afdRefs
+}
+
 func (tr *timeRange) intersects(from, to time.Time) bool {
 	return !(to.Before(tr.lower) || from.After(tr.upper))
 }