changeset 5200:5572da077c89 new-fwa

Load LDCs.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 08 May 2020 15:29:36 +0200
parents 5001582f2ee1
children 40daecc6f552
files pkg/controllers/fwa.go
diffstat 1 files changed, 63 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/fwa.go	Fri May 08 11:43:06 2020 +0200
+++ b/pkg/controllers/fwa.go	Fri May 08 15:29:36 2020 +0200
@@ -48,13 +48,37 @@
   ST_Intersects(b.area, s.area)
   AND s.name = $1
   AND b.validity && tstzrange($2, $3)`
+
+	selectLDCsSQL = `
+SELECT
+  lower(grwl.validity),
+  upper(grwl.validity),
+  grwl.value
+FROM
+  waterway.gauges_reference_water_levels grwl
+  JOIN waterway.bottlenecks bns
+    ON grwl.location = bns.gauge_location
+WHERE
+  grwl.depth_reference like 'LDC%'
+  AND bns.bottleneck_id = $1
+  AND grwl.validity && tstzrange($2, $3)`
 )
 
 type (
+	timeRange struct {
+		lower time.Time
+		upper time.Time
+	}
+
+	ldc struct {
+		timeRange
+		value float64
+	}
+
 	limitingValidity struct {
+		timeRange
 		limiting string
-		lower    time.Time
-		upper    time.Time
+		ldcs     []*ldc
 	}
 
 	limitingValidities []limitingValidity
@@ -99,7 +123,7 @@
 
 	bottlenecks, err := extract(ctx, conn, name, from, to)
 	if err != nil {
-		log.Println("error: %v\n", err)
+		log.Printf("error: %v\n", err)
 		http.Error(rw, "cannot extract bottlenecks", http.StatusBadRequest)
 		return
 	}
@@ -111,6 +135,12 @@
 			http.Error(rw, "cannot load validities", http.StatusInternalServerError)
 			return
 		}
+		// load LCDs
+		if err := bottlenecks[i].loadLDCs(ctx, conn, from, to); err != nil {
+			log.Printf("error: %v\n", err)
+			http.Error(rw, "cannot load LDCs", http.StatusInternalServerError)
+			return
+		}
 	}
 
 	// TODO: Implement me!
@@ -153,8 +183,8 @@
 	return dusk(from), dawn(to), true
 }
 
-func (lv *limitingValidity) intersects(from, to time.Time) bool {
-	return !(to.Before(lv.lower) || from.After(lv.upper))
+func (tr *timeRange) intersects(from, to time.Time) bool {
+	return !(to.Before(tr.lower) || from.After(tr.upper))
 }
 
 func (lvs limitingValidities) find() func(from, to time.Time) *limitingValidity {
@@ -291,3 +321,31 @@
 	}
 	return err
 }
+
+func (bn *bottleneck) loadLDCs(
+	ctx context.Context,
+	conn *sql.Conn,
+	from, to time.Time,
+) error {
+	rows, err := conn.QueryContext(
+		ctx, selectLDCsSQL,
+		bn.id,
+		from, to)
+	if err != nil {
+		return err
+	}
+	defer rows.Close()
+	for rows.Next() {
+		var l ldc
+		if err := rows.Scan(&l.lower, &l.upper, &l.value); err != nil {
+			return err
+		}
+		for i := range bn.validities {
+			vs := bn.validities[i]
+			if vs.intersects(l.lower, l.upper) {
+				vs.ldcs = append(vs.ldcs, &l)
+			}
+		}
+	}
+	return nil
+}