changeset 1569:cf0ca4a9812a

Bottleneck import: Implemented backend to store bottlenecks in database.
author Raimund Renkert <raimund.renkert@intevation.de>
date Wed, 12 Dec 2018 15:48:33 +0100
parents ce59e73a0cef
children 424793472f28
files pkg/imports/bn.go
diffstat 1 files changed, 105 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/bn.go	Wed Dec 12 14:50:28 2018 +0100
+++ b/pkg/imports/bn.go	Wed Dec 12 15:48:33 2018 +0100
@@ -17,6 +17,9 @@
 	"context"
 	"database/sql"
 	"errors"
+	"regexp"
+	"strconv"
+	"time"
 
 	"gemma.intevation.de/gemma/pkg/common"
 	"gemma.intevation.de/gemma/pkg/soap/ifbn"
@@ -29,6 +32,36 @@
 
 const BNJobKind JobKind = "bn"
 
+const insertSQL = `INSERT INTO waterway.bottlenecks (
+  bottleneck_id,
+  fk_g_fid,
+  objnam,
+  nobjnm,
+  stretch,
+  area,
+  rb,
+  lb,
+  responsible_country,
+  revisiting_time,
+  limiting,
+  date_info,
+  source_organization
+) VALUES(
+  $1,
+  isrs_fromText($2),
+  $3,
+  $4,
+  isrsrange(isrs_fromText($5), isrs_fromText($6)),
+  ST_MakePolygon(ST_ExteriorRing(ST_Buffer(ST_SetSRID(ST_Makepoint(13.05501, 47.80949), 4326), 0.01)))::Geography,
+  $7,
+  $8,
+  $9,
+  $10,
+  $11,
+  $12,
+  $13
+) ON CONFLICT (bottleneck_id) DO NOTHING`
+
 type bnJobCreator struct{}
 
 func init() {
@@ -73,6 +106,24 @@
 // CleanUp of a bottleneck import is a NOP.
 func (bn *Bottleneck) CleanUp() error { return nil }
 
+var rblbRe = regexp.MustCompile(`(..)_(..)`)
+
+func splitRBLB(s string) (string, string) {
+	m := rblbRe.FindStringSubmatch(s)
+	if len(m) == 0 {
+		return "", ""
+	}
+	return m[1], m[2]
+}
+
+func revisitingTime(s string) int {
+	v, err := strconv.Atoi(s)
+	if err != nil {
+		v = 0
+	}
+	return v
+}
+
 // Do executes the actual bottleneck import.
 func (bn *Bottleneck) Do(
 	ctx context.Context,
@@ -96,10 +147,61 @@
 		return nil, err
 	}
 
-	// TODO: Implement me!
 	bns := resp.Export_bn_by_isrsResult.BottleNeckType
+	feedback.Info("Found %d bottlenecks for import", len(bns))
+
+	tx, err := conn.BeginTx(ctx, nil)
+	if err != nil {
+		return nil, err
+	}
+	defer tx.Rollback()
+
+	start := time.Now()
+	for i := range bns {
+		bn := bns[i]
+		rb, lb := splitRBLB(bn.Rb_lb)
+
+		var limiting, country string
+
+		if bn.Limiting_factor != nil {
+			limiting = string(*bn.Limiting_factor)
+		}
+
+		if bn.Responsible_country != nil {
+			country = string(*bn.Responsible_country)
+		}
 
-	_ = bns
+		tx.Exec(insertSQL,
+			bn.Bottleneck_id,
+			bn.Fk_g_fid,
+			bn.OBJNAM,
+			bn.NOBJNM,
+			bn.From_ISRS, bn.To_ISRS,
+			rb,
+			lb,
+			country,
+			revisitingTime(bn.Revisiting_time),
+			limiting,
+			bn.Date_Info,
+			bn.Source,
+		)
+		feedback.Info("Insert \"%s\" into database", bn.OBJNAM)
+		//TODO: Track for potential later removal? Bottlenecks have an string PK, track wants int64.
+		// if err = track(ctx, tx, importID, "waterway.bottlenecks", bn.Bottleneck_id); err != nil {
+		// 	return nil, err
+		// }
+	}
 
-	return nil, nil
+	feedback.Info("Storing %d bottlenecks took %s", len(bns), time.Since(start))
+	if err = tx.Commit(); err == nil {
+		feedback.Info("Import of bottlenecks was successful")
+	}
+
+	// TODO: needs to be filled.
+	summary := struct {
+		BottleneckCount int `json:"bottleneckCount"`
+	}{
+		BottleneckCount: len(bns),
+	}
+	return &summary, err
 }