changeset 5251:1fce0fd81f46 new-fwa

Code simplification.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 13 May 2020 09:13:42 +0200
parents 13e1767b63a1
children aebe1a12e8b9
files pkg/controllers/fwa.go
diffstat 1 files changed, 18 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/fwa.go	Tue May 12 23:03:21 2020 +0200
+++ b/pkg/controllers/fwa.go	Wed May 13 09:13:42 2020 +0200
@@ -202,22 +202,21 @@
 	ctx := req.Context()
 	conn := middleware.GetDBConn(req)
 
-	// Function to extract the bottleneck_id's from the query.
-	var extract func(context.Context, *sql.Conn, string, time.Time, time.Time) (bottlenecks, error)
+	var bns bottlenecks
+	var err error
 
 	switch vars["kind"] {
 	case "bottleneck":
-		extract = extractBottleneck
+		bns = bottlenecks{{id: name}}
 	case "stretch":
-		extract = extractStretch
+		bns, err = loadSymbolBottlenecks(ctx, conn, "users.stretches", name, from, to)
 	case "section":
-		extract = extractSection
+		bns, err = loadSymbolBottlenecks(ctx, conn, "waterway.sections", name, from, to)
 	default:
 		http.Error(rw, "Invalid kind type.", http.StatusBadRequest)
 		return
 	}
 
-	bottlenecks, err := extract(ctx, conn, name, from, to)
 	if err != nil {
 		log.Printf("error: %v\n", err)
 		http.Error(rw, "cannot extract bottlenecks", http.StatusBadRequest)
@@ -225,26 +224,26 @@
 	}
 
 	// If there are no bottlenecks there is nothing to do.
-	if len(bottlenecks) == 0 {
+	if len(bns) == 0 {
 		http.Error(rw, "No bottlenecks found.", http.StatusNotFound)
 		return
 	}
 
 	// load validities and limiting factors
-	for i := range bottlenecks {
-		if err := bottlenecks[i].loadLimitingValidities(ctx, conn, from, to); err != nil {
+	for i := range bns {
+		if err := bns[i].loadLimitingValidities(ctx, conn, from, to); err != nil {
 			log.Printf("error: %v\n", err)
 			http.Error(rw, "cannot load validities", http.StatusInternalServerError)
 			return
 		}
 		// load LCDs
-		if err := bottlenecks[i].loadLDCs(ctx, conn, from, to); err != nil {
+		if err := bns[i].loadLDCs(ctx, conn, from, to); err != nil {
 			log.Printf("error: %v\n", err)
 			http.Error(rw, "cannot load LDCs", http.StatusInternalServerError)
 			return
 		}
 		// load values
-		if err := bottlenecks[i].loadValues(ctx, conn, from, to, los); err != nil {
+		if err := bns[i].loadValues(ctx, conn, from, to, los); err != nil {
 			log.Printf("error: %v\n", err)
 			http.Error(rw, "cannot load values", http.StatusInternalServerError)
 			return
@@ -261,8 +260,8 @@
 			limitingWidth: widthBreaks,
 		}
 
-		useDepth = bottlenecks.hasLimiting(limitingDepth, from, to)
-		useWidth = bottlenecks.hasLimiting(limitingWidth, from, to)
+		useDepth = bns.hasLimiting(limitingDepth, from, to)
+		useWidth = bns.hasLimiting(limitingWidth, from, to)
 	)
 
 	if useDepth && useWidth && len(widthBreaks) != len(depthBreaks) {
@@ -301,9 +300,9 @@
 	}
 
 	// For every day on every bottleneck we need to find out if this day is valid.
-	validities := make([]func(time.Time, time.Time) *limitingValidity, len(bottlenecks))
-	for i := range bottlenecks {
-		validities[i] = bottlenecks[i].validities.find()
+	validities := make([]func(time.Time, time.Time) *limitingValidity, len(bns))
+	for i := range bns {
+		validities[i] = bns[i].validities.find()
 	}
 
 	// Mode reflects if we use monthly, quarterly od yearly intervals.
@@ -371,7 +370,7 @@
 			hasValid = true
 
 			if overLDC { // If its already not shipable we need no further tests.
-				result := bottlenecks[i].measurements.classify(
+				result := bns[i].measurements.classify(
 					current, next,
 					ldc.value,
 					(*availMeasurement).getValue)
@@ -381,7 +380,7 @@
 				}
 			}
 
-			if min := minClass(bottlenecks[i].measurements.classify(
+			if min := minClass(bns[i].measurements.classify(
 				current, next,
 				chooseBreaks[vs.limiting],
 				limitingAccess[vs.limiting]),
@@ -624,7 +623,7 @@
 	return lvs, rows.Err()
 }
 
-func loadSymbolBottlenecksFromTo(
+func loadSymbolBottlenecks(
 	ctx context.Context,
 	conn *sql.Conn,
 	what, name string,
@@ -654,41 +653,6 @@
 	return bns, rows.Err()
 }
 
-func extractBottleneck(
-	_ context.Context,
-	_ *sql.Conn,
-	name string,
-	_, _ time.Time,
-) (bottlenecks, error) {
-	return bottlenecks{{id: name}}, nil
-}
-
-func extractStretch(
-	ctx context.Context,
-	conn *sql.Conn,
-	name string,
-	from, to time.Time,
-) (bottlenecks, error) {
-	return loadSymbolBottlenecksFromTo(
-		ctx,
-		conn,
-		"users.stretches", name,
-		from, to)
-}
-
-func extractSection(
-	ctx context.Context,
-	conn *sql.Conn,
-	name string,
-	from, to time.Time,
-) (bottlenecks, error) {
-	return loadSymbolBottlenecksFromTo(
-		ctx,
-		conn,
-		"waterway.sections", name,
-		from, to)
-}
-
 func (bn *bottleneck) loadLimitingValidities(
 	ctx context.Context,
 	conn *sql.Conn,