changeset 5208:defca5418446 new-fwa

Define how modes work.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 11 May 2020 15:02:15 +0200
parents c4e295b2d6a3
children 32eb35699bd3
files pkg/controllers/fwa.go
diffstat 1 files changed, 73 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/fwa.go	Mon May 11 11:57:48 2020 +0200
+++ b/pkg/controllers/fwa.go	Mon May 11 15:02:15 2020 +0200
@@ -167,12 +167,6 @@
 
 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
@@ -243,6 +237,10 @@
 		validities[i] = bottlenecks[i].validities.find()
 	}
 
+	mode := parseFWAMode(req.FormValue("mode"))
+
+	label, finish := interval(mode, from)
+
 	var shipableDays int
 
 	// We step through the time in steps of one day.
@@ -278,7 +276,15 @@
 		if shipable {
 			shipableDays++
 		}
-		// TODO: depending on mode write out results.
+
+		if finish(next) {
+			// TODO: depending on mode write out results.
+			l := label(current)
+			_ = l
+
+			// Reset counters
+			shipableDays = 0
+		}
 
 		current = next
 	}
@@ -681,3 +687,63 @@
 
 	return result
 }
+
+func interval(mode fwaMode, t time.Time) (
+	label func(time.Time) string,
+	finish func(time.Time) bool,
+) {
+	switch mode {
+	case fwaMonthly:
+		label, finish = monthLabel, otherMonth(t)
+	case fwaQuarterly:
+		label, finish = quarterLabel, otherQuarter(t)
+	case fwaYearly:
+		label, finish = yearLabel, otherYear(t)
+	default:
+		panic("Unknown mode")
+	}
+	return
+}
+
+func monthLabel(t time.Time) string {
+	return fmt.Sprintf("%02d-%d", t.Month(), t.Year())
+}
+
+func quarterLabel(t time.Time) string {
+	return fmt.Sprintf("Q%d-%d", (int(t.Month())-1)/3+1, t.Year())
+}
+
+func yearLabel(t time.Time) string {
+	return strconv.Itoa(t.Year())
+}
+
+func otherMonth(t time.Time) func(time.Time) bool {
+	return func(x time.Time) bool {
+		flag := t.Day() == x.Day()
+		if flag {
+			t = x
+		}
+		return flag
+	}
+}
+
+func otherQuarter(t time.Time) func(time.Time) bool {
+	quarter := (t.Month() - 1) / 3
+	return func(x time.Time) bool {
+		flag := quarter != (x.Month()-1)/3
+		if flag {
+			t = x
+		}
+		return flag
+	}
+}
+
+func otherYear(t time.Time) func(time.Time) bool {
+	return func(x time.Time) bool {
+		flag := t.Year() != x.Year()
+		if flag {
+			t = x
+		}
+		return flag
+	}
+}