changeset 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
files pkg/controllers/bottlenecks.go pkg/controllers/fwa.go pkg/controllers/stretches.go
diffstat 3 files changed, 65 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/bottlenecks.go	Fri May 08 18:59:14 2020 +0200
+++ b/pkg/controllers/bottlenecks.go	Mon May 11 11:03:19 2020 +0200
@@ -21,8 +21,6 @@
 	"fmt"
 	"log"
 	"net/http"
-	"strconv"
-	"strings"
 	"time"
 
 	"github.com/gorilla/mux"
@@ -91,12 +89,6 @@
 `
 )
 
-// afdRefs are the typical available fairway depth reference values.
-var afdRefs = []float64{
-	230,
-	250,
-}
-
 // According to clarification, it has to be assumed, that at times
 // with no data, the best case (which by convention is the highest
 // class created by classify()) should be assumed.  That is due to the
@@ -140,19 +132,6 @@
 	return percents
 }
 
-func intervalMode(mode string) int {
-	switch strings.ToLower(mode) {
-	case "monthly":
-		return 0
-	case "quarterly":
-		return 1
-	case "yearly":
-		return 2
-	default:
-		return 0
-	}
-}
-
 func loadDepthValues(
 	ctx context.Context,
 	conn *sql.Conn,
@@ -207,23 +186,9 @@
 	return []float64{value}, nil
 }
 
-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 bottleneckAvailabilty(rw http.ResponseWriter, req *http.Request) {
 
-	mode := intervalMode(req.FormValue("mode"))
+	mode := parseFWAMode(req.FormValue("mode"))
 	bn := mux.Vars(req)["objnam"]
 
 	if bn == "" {
@@ -284,12 +249,7 @@
 		return
 	}
 
-	var breaks []float64
-	if b := req.FormValue("breaks"); b != "" {
-		breaks = breaksToReferenceValue(b)
-	} else {
-		breaks = afdRefs
-	}
+	breaks := parseBreaks(req.FormValue("breaks"))
 
 	log.Printf("info: time interval: (%v - %v)\n", from, to)
 
@@ -377,7 +337,7 @@
 
 func bottleneckAvailableFairwayDepth(rw http.ResponseWriter, req *http.Request) {
 
-	mode := intervalMode(req.FormValue("mode"))
+	mode := parseFWAMode(req.FormValue("mode"))
 
 	bn := mux.Vars(req)["objnam"]
 	if bn == "" {
@@ -449,12 +409,7 @@
 		return
 	}
 
-	var breaks []float64
-	if b := req.FormValue("breaks"); b != "" {
-		breaks = breaksToReferenceValue(b)
-	} else {
-		breaks = afdRefs
-	}
+	breaks := parseBreaks(req.FormValue("breaks"))
 
 	rw.Header().Add("Content-Type", "text/csv")
 
@@ -539,9 +494,9 @@
 }
 
 var intervals = []func(time.Time, time.Time) func() (time.Time, time.Time, string){
-	monthly,
-	quarterly,
-	yearly,
+	fwaMonthly:   monthly,
+	fwaQuarterly: quarterly,
+	fwaYearly:    yearly,
 }
 
 func monthly(from, to time.Time) func() (time.Time, time.Time, string) {
--- 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))
 }
--- a/pkg/controllers/stretches.go	Fri May 08 18:59:14 2020 +0200
+++ b/pkg/controllers/stretches.go	Mon May 11 11:03:19 2020 +0200
@@ -209,7 +209,7 @@
 	vars := mux.Vars(req)
 	stretch := vars["kind"] == "stretch"
 	name := vars["name"]
-	mode := intervalMode(req.FormValue("mode"))
+	mode := parseFWAMode(req.FormValue("mode"))
 
 	depthbreaks, widthbreaks := afdRefs, afdRefs
 
@@ -460,7 +460,7 @@
 	vars := mux.Vars(req)
 	stretch := vars["kind"] == "stretch"
 	name := vars["name"]
-	mode := intervalMode(req.FormValue("mode"))
+	mode := parseFWAMode(req.FormValue("mode"))
 
 	if name == "" {
 		http.Error(