changeset 5236:b47a041e03c4 new-fwa

Fill classification columns in CSV export.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 12 May 2020 15:04:12 +0200
parents 987a56071402
children 6ba7e6ac0882
files pkg/controllers/fwa.go
diffstat 1 files changed, 31 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/fwa.go	Tue May 12 14:42:26 2020 +0200
+++ b/pkg/controllers/fwa.go	Tue May 12 15:04:12 2020 +0200
@@ -311,11 +311,11 @@
 
 	label, finish := interval(mode, from)
 
-	var totalDays, overLDCDays int
-
-	allClasses := make([][]time.Duration, len(validities))
-	minClasses := make([]time.Duration, len(validities))
-	missingLDCs := make([]int, len(validities))
+	var (
+		totalDays, overLDCDays int
+		missingLDCs            = make([]int, len(validities))
+		counters               = make([]int, len(validities))
+	)
 
 	// We step through the time in steps of one day.
 	for current := from; current.Before(to); {
@@ -324,13 +324,11 @@
 
 		// Assume that a bottleneck is over LDC.
 		overLDC := true
+		highest := -1
 
 		// check all bottlenecks
 		for i, validity := range validities {
 
-			// Assume that this bottleneck cannot be evaluated for this day.
-			allClasses[i] = nil
-
 			// Check if bottleneck is available for this day.
 			vs := validity(current, next)
 			if vs == nil {
@@ -355,37 +353,48 @@
 				}
 			}
 
-			allClasses[i] = bottlenecks[i].measurements.classify(
+			if min := minClass(bottlenecks[i].measurements.classify(
 				current, next,
 				chooseBreaks[vs.limiting],
-				limitingAccess[vs.limiting])
+				limitingAccess[vs.limiting]),
+				12*time.Hour,
+			); min > highest {
+				highest = min
+			}
 		}
 
 		if overLDC {
 			overLDCDays++
 		}
+		if highest > -1 {
+			counters[highest]++
+		}
 		totalDays++
 
-		minOfAll(allClasses, minClasses)
-
-		// TODO: Accumulate daily statistics into time segment.
-
 		if finish(next) {
 			record[0] = label(current)
 
 			if availability {
 				record[1] = strconv.Itoa(totalDays - overLDCDays)
 				record[2] = strconv.Itoa(overLDCDays)
+				for i, c := range counters {
+					record[3+i] = strconv.Itoa(c)
+				}
 			} else {
 				overPerc := float64(overLDCDays) * 100 / float64(totalDays)
 				record[1] = fmt.Sprintf("%.3f", 100-overPerc)
 				record[2] = fmt.Sprintf("%.3f", overPerc)
+				for i, c := range counters {
+					perc := float64(c) * 100 / float64(totalDays)
+					record[3+i] = fmt.Sprintf("%.3f", perc)
+				}
 			}
 
-			// TODO: depending on mode write out results.
-
 			// Reset counters
 			overLDCDays, totalDays = 0, 0
+			for i := range counters {
+				counters[i] = 0
+			}
 		}
 
 		current = next
@@ -400,19 +409,14 @@
 	}
 }
 
-func minOfAll(classes [][]time.Duration, min []time.Duration) {
-	for i := range min {
-		var m time.Duration
-		for _, cl := range classes {
-			if cl == nil {
-				continue
-			}
-			if cl[i] < m {
-				m = cl[i]
-			}
+func minClass(classes []time.Duration, threshold time.Duration) int {
+	var sum time.Duration
+	for i, v := range classes {
+		if sum += v; sum >= threshold {
+			return i
 		}
-		min[i] = m
 	}
+	return -1
 }
 
 func dusk(t time.Time) time.Time {