changeset 2201:cae0b597aefc

Fairway availabilty import: There is no need to fetch the responsibility country from the database. Only the bottleneck_ids are needed.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 12 Feb 2019 18:06:32 +0100
parents 64147a137e0a
children 0aee7d4954ae
files pkg/imports/fa.go
diffstat 1 files changed, 23 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/fa.go	Tue Feb 12 17:12:12 2019 +0100
+++ b/pkg/imports/fa.go	Tue Feb 12 18:06:32 2019 +0100
@@ -18,6 +18,7 @@
 	"database/sql"
 	"errors"
 	"fmt"
+	"sort"
 	"time"
 
 	"github.com/jackc/pgx/pgtype"
@@ -46,12 +47,13 @@
 const (
 	listBottlenecksSQL = `
 SELECT
-  bottleneck_id,
-  responsible_country
+  bottleneck_id
 FROM waterway.bottlenecks
 WHERE responsible_country = users.current_user_country()
   AND staging_done = true
+ORDER BY bottleneck_id
 `
+
 	latestMeasureDateSQL = `
 SELECT
 	measure_date
@@ -180,12 +182,14 @@
 // CleanUp of a fairway availablities import is a NOP.
 func (*FairwayAvailability) CleanUp() error { return nil }
 
-type bottleneckCountry struct {
-	ID                 string
-	ResponsibleCountry string
+type bottlenecks []string
+
+func (bns bottlenecks) contains(bn string) bool {
+	idx := sort.SearchStrings(bns, bn)
+	return idx < len(bns) && bns[idx] == bn
 }
 
-func loadBottleneckCountries(ctx context.Context, tx *sql.Tx) ([]bottleneckCountry, error) {
+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)
@@ -194,22 +198,20 @@
 	}
 	defer rows.Close()
 
-	var bottlenecks []bottleneckCountry
+	var bns bottlenecks
 
 	for rows.Next() {
-		var bn bottleneckCountry
-		if err = rows.Scan(
-			&bn.ID,
-			&bn.ResponsibleCountry,
-		); err != nil {
+		var bn string
+		if err = rows.Scan(&bn); err != nil {
 			return nil, err
 		}
-		bottlenecks = append(bottlenecks, bn)
+		bns = append(bns, bn)
 	}
 	if err = rows.Err(); err != nil {
 		return nil, err
 	}
-	return bottlenecks, nil
+
+	return bns, nil
 }
 
 func loadFairwayAvailabilities(ctx context.Context, tx *sql.Tx) (map[uniqueFairwayAvailability]int64, error) {
@@ -273,7 +275,7 @@
 	}
 	defer tx.Rollback()
 
-	bottlenecks, err := loadBottleneckCountries(ctx, tx)
+	bns, err := loadBottleneckCountries(ctx, tx)
 	if err != nil {
 		return nil, err
 	}
@@ -288,7 +290,7 @@
 		return nil, err
 	}
 
-	faids, err := fa.doForFAs(ctx, bottlenecks, fairwayAvailabilities, latest, tx, feedback)
+	faids, err := fa.doForFAs(ctx, bns, fairwayAvailabilities, latest, tx, feedback)
 	if err != nil {
 		return nil, fmt.Errorf("Error processing data: %v", err)
 	}
@@ -320,7 +322,7 @@
 
 func (fa *FairwayAvailability) doForFAs(
 	ctx context.Context,
-	bottlenecks []bottleneckCountry,
+	bnIds bottlenecks,
 	fairwayAvailabilities map[uniqueFairwayAvailability]int64,
 	latestDate pgtype.Timestamp,
 	tx *sql.Tx,
@@ -329,11 +331,6 @@
 
 	client := ifaf.NewFairwayAvailabilityService(fa.URL, fa.Insecure, nil)
 
-	bnIds := make([]string, len(bottlenecks))
-	for i := range bottlenecks {
-		bnIds[i] = bottlenecks[i].ID
-	}
-
 	var period ifaf.RequestedPeriod
 	period.Date_start = latestDate.Time
 	period.Date_end = time.Now()
@@ -382,6 +379,10 @@
 	var faID int64
 	feedback.Info("Found %d fairway availabilities", len(result.FairwayAvailability))
 	for _, faRes := range result.FairwayAvailability {
+		if !bnIds.contains(faRes.Bottleneck_id) {
+			feedback.Warn("Bottleneck %s not found in database.", faRes.Bottleneck_id)
+			continue
+		}
 		uniqueFa := uniqueFairwayAvailability{
 			BottleneckId: faRes.Bottleneck_id,
 			Surdat:       faRes.SURDAT,