changeset 4659:92a7551d626f stree-experiment

Merged default into stree-experiment branch.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 14 Oct 2019 14:59:31 +0200
parents 4bbfe3dd2ab5 (current diff) a89e4db7980b (diff)
children 6eab3ac0e849
files
diffstat 2 files changed, 43 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/components/fairway/Fairwayprofile.vue	Mon Oct 14 14:58:04 2019 +0200
+++ b/client/src/components/fairway/Fairwayprofile.vue	Mon Oct 14 14:59:31 2019 +0200
@@ -644,9 +644,7 @@
             return xScale(d.x);
           })
           .y(d =>
-            yScaleRight(
-              d.y + Math.abs(this.waterlevel - this.refWaterlevel) / 100
-            )
+            yScaleRight(d.y + this.waterlevel - this.refWaterlevel / 100)
           );
         let profileArea = d3
           .area()
@@ -655,9 +653,7 @@
           })
           .y0(height)
           .y1(d =>
-            yScaleRight(
-              d.y + Math.abs(this.waterlevel - this.refWaterlevel) / 100
-            )
+            yScaleRight(d.y + (this.waterlevel - this.refWaterlevel) / 100)
           );
         graph
           .append("path")
--- a/pkg/imports/fa.go	Mon Oct 14 14:58:04 2019 +0200
+++ b/pkg/imports/fa.go	Mon Oct 14 14:59:31 2019 +0200
@@ -48,6 +48,7 @@
 	maxHistoryDays = 7
 )
 
+// Find bottlenecks of the current users country for a given time range
 const (
 	listBottlenecksSQL = `
 SELECT DISTINCT
@@ -56,14 +57,21 @@
 WHERE responsible_country = (
     SELECT country FROM users.list_users WHERE username = current_user)
   AND staging_done = true
+  AND validity && $1
 ORDER BY bottleneck_id
 `
 
 	latestMeasureDateSQL = `
-SELECT
-	measure_date
-FROM waterway.effective_fairway_availability
-ORDER BY measure_date DESC LIMIT 1
+SELECT CASE WHEN (SELECT array_agg(DISTINCT bottleneck_id) @> $1::varchar[]
+                    FROM waterway.fairway_availability)
+       THEN (SELECT min(x.m) FROM
+              (SELECT max(efa.measure_date) AS m
+                FROM waterway.fairway_availability fa,
+                     waterway.effective_fairway_availability efa
+                WHERE fa.bottleneck_id = ANY($1)
+                  AND efa.fairway_availability_id = fa.id
+                GROUP BY fa.bottleneck_id) AS x)
+       END
 `
 
 	insertFASQL = `
@@ -193,7 +201,18 @@
 func loadBottleneckCountries(ctx context.Context, tx *sql.Tx) (bottlenecks, error) {
 
 	// Get available bottlenecks from database for use as filter in SOAP request
-	rows, err := tx.QueryContext(ctx, listBottlenecksSQL)
+	// We are only interested in bottlenecks which were valid in the last
+	// maxHistoryDays
+	var tfrom pgtype.Timestamptz
+	tfrom.Set(time.Now().AddDate(0, 0, -maxHistoryDays))
+	trange := pgtype.Tstzrange{
+		Lower:     tfrom,
+		LowerType: pgtype.Inclusive,
+		UpperType: pgtype.Unbounded,
+		Status:    pgtype.Present,
+	}
+
+	rows, err := tx.QueryContext(ctx, listBottlenecksSQL, trange)
 	if err != nil {
 		return nil, err
 	}
@@ -215,9 +234,20 @@
 	return bns, nil
 }
 
-func latestDate(ctx context.Context, tx *sql.Tx) (pgtype.Timestamp, error) {
-	var date pgtype.Timestamp
-	err := tx.QueryRowContext(ctx, latestMeasureDateSQL).Scan(&date)
+// Get the earliest of all the latest measure_dates for a list of bottlenecks.
+// We do not pick the latest of all dates, so that we don't miss data if one of
+// the bottlenecks we are taking into account was not active for some time...
+func latestDate(
+	ctx context.Context,
+	tx *sql.Tx,
+	bns bottlenecks,
+) (pgtype.Timestamp, error) {
+	var (
+		date  pgtype.Timestamp
+		pgbns pgtype.TextArray
+	)
+	pgbns.Set(bns)
+	err := tx.QueryRowContext(ctx, latestMeasureDateSQL, &pgbns).Scan(&date)
 	switch {
 	case err == sql.ErrNoRows:
 		date = pgtype.Timestamp{
@@ -452,11 +482,13 @@
 		ctx context.Context,
 		tx *sql.Tx, bns bottlenecks,
 	) ([]*ifaf.FairwayAvailability, error) {
+		feedback.Info("Requesting data for: %v", bns)
 
-		latest, err := latestDate(ctx, tx)
+		latest, err := latestDate(ctx, tx, bns)
 		if err != nil {
 			return nil, err
 		}
+		feedback.Info("Requesting data starting from %s", latest.Time)
 
 		client := ifaf.NewFairwayAvailabilityService(fa.URL, fa.Insecure, nil)