changeset 2533:de4dc3d16647

AGM import: Check if gauge exists for a meassurement.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 07 Mar 2019 11:57:05 +0100
parents 452bc714bfd9
children bb5286acfee2
files pkg/imports/agm.go
diffstat 1 files changed, 36 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/agm.go	Thu Mar 07 10:46:34 2019 +0100
+++ b/pkg/imports/agm.go	Thu Mar 07 11:57:05 2019 +0100
@@ -215,6 +215,12 @@
   false
 )
 RETURNING id`
+
+	agmGaugeCheckSQL = `
+SELECT EXISTS(
+  SELECT 1 FROM waterway.gauges
+  WHERE location = ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int))
+`
 )
 
 // Do executes the actual approved gauge measurements import.
@@ -326,6 +332,11 @@
 	}
 	defer tx.Rollback()
 
+	gaugeCheckStmt, err := tx.PrepareContext(ctx, agmGaugeCheckSQL)
+	if err != nil {
+		return nil, err
+	}
+	defer gaugeCheckStmt.Close()
 	selectStmt, err := tx.PrepareContext(ctx, agmSelectSQL)
 	if err != nil {
 		return nil, err
@@ -344,6 +355,8 @@
 
 	entries := []*agmSummaryEntry{}
 
+	checkedGauges := map[models.Isrs]bool{}
+
 lines:
 	for line := 1; ; line++ {
 
@@ -365,6 +378,29 @@
 			return nil, fmt.Errorf("Invalid ISRS code line %d: %v", line, err)
 		}
 
+		if exists, found := checkedGauges[*gid]; found {
+			if !exists {
+				feedback.Warn("Ignoring data for unknown gauge %s", gid.String())
+				continue lines
+			}
+		} else { // not found in gauge cache
+			if err := gaugeCheckStmt.QueryRowContext(
+				ctx,
+				gid.CountryCode,
+				gid.LoCode,
+				gid.FairwaySection,
+				gid.Orc,
+				gid.Hectometre,
+			).Scan(&exists); err != nil {
+				return nil, err
+			}
+			checkedGauges[*gid] = exists
+			if !exists {
+				feedback.Warn("Ignoring data for unknown gauge %s", gid.String())
+				continue lines
+			}
+		}
+
 		md, err := guessDate(row[measureDateIdx])
 		if err != nil {
 			return nil, fmt.Errorf("Invalid 'measure_date' line %d: %v", line, err)