changeset 2195:3437ceee1529

Uploaded bottleneck import: Implemented in terms of normal bottleneck import.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 12 Feb 2019 11:54:12 +0100
parents 4d6979dedb11
children e57ba9585aaa
files pkg/imports/bn.go pkg/imports/ubn.go
diffstat 2 files changed, 56 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/bn.go	Tue Feb 12 11:09:55 2019 +0100
+++ b/pkg/imports/bn.go	Tue Feb 12 11:54:12 2019 +0100
@@ -143,23 +143,41 @@
 	conn *sql.Conn,
 	feedback Feedback,
 ) (interface{}, error) {
-	client := ifbn.NewIBottleneckService(bn.URL, bn.Insecure, nil)
+
+	fetch := func(feedback Feedback) ([]*ifbn.BottleNeckType, error) {
+		client := ifbn.NewIBottleneckService(bn.URL, bn.Insecure, nil)
+
+		req := &ifbn.Export_bn_by_isrs{}
 
-	req := &ifbn.Export_bn_by_isrs{}
+		resp, err := client.Export_bn_by_isrs(req)
+		if err != nil {
+			return nil, err
+		}
+
+		if resp.Export_bn_by_isrsResult == nil {
+			return nil, errors.New("no Bottlenecks found")
+		}
 
-	resp, err := client.Export_bn_by_isrs(req)
+		return resp.Export_bn_by_isrsResult.BottleNeckType, nil
+	}
+
+	return storeBottlenecks(ctx, fetch, importID, conn, feedback)
+}
+
+func storeBottlenecks(
+	ctx context.Context,
+	fetch func(Feedback) ([]*ifbn.BottleNeckType, error),
+	importID int64,
+	conn *sql.Conn,
+	feedback Feedback,
+) (interface{}, error) {
+	start := time.Now()
+
+	bns, err := fetch(feedback)
 	if err != nil {
-		feedback.Error("%v", err)
 		return nil, err
 	}
 
-	if resp.Export_bn_by_isrsResult == nil {
-		err := errors.New("no Bottlenecks found")
-		feedback.Error("%v", err)
-		return nil, err
-	}
-
-	bns := resp.Export_bn_by_isrsResult.BottleNeckType
 	feedback.Info("Found %d bottlenecks for import", len(bns))
 
 	tx, err := conn.BeginTx(ctx, nil)
@@ -187,8 +205,6 @@
 
 	var nids []string
 
-	start := time.Now()
-
 nextBN:
 	for _, bn := range bns {
 
--- a/pkg/imports/ubn.go	Tue Feb 12 11:09:55 2019 +0100
+++ b/pkg/imports/ubn.go	Tue Feb 12 11:54:12 2019 +0100
@@ -18,6 +18,10 @@
 	"database/sql"
 	"errors"
 	"os"
+	"path/filepath"
+
+	"gemma.intevation.de/gemma/pkg/soap"
+	"gemma.intevation.de/gemma/pkg/soap/ifbn"
 )
 
 type UploadedBottleneck struct {
@@ -40,10 +44,8 @@
 func (ubnJobCreator) Create() Job { return new(UploadedBottleneck) }
 
 func (ubnJobCreator) Depends() []string {
-	return []string{
-		"gauges",
-		"bottlenecks",
-	}
+	// Same as normal bottleneck import.
+	return bnJobCreator{}.Depends()
 }
 
 // StageDone moves the imported bottleneck out of the staging area.
@@ -52,8 +54,8 @@
 	tx *sql.Tx,
 	id int64,
 ) error {
-	// TODO: Implement me!
-	return nil
+	// Same as normal bottleneck import.
+	return bnJobCreator{}.StageDone(ctx, tx, id)
 }
 
 // CleanUp of a uploaded bottleneck import removes the temp dir.
@@ -68,6 +70,23 @@
 	conn *sql.Conn,
 	feedback Feedback,
 ) (interface{}, error) {
-	// TODO: Implement me!
-	return nil, errors.New("Not implemented, yet!")
+
+	fetch := func(feedback Feedback) ([]*ifbn.BottleNeckType, error) {
+		var dst ifbn.Export_bn_by_isrsResponse
+		if err := soap.ValidateFile(
+			filepath.Join(ubn.Dir, "data.xml"),
+			"IFBN.xsd",
+			&dst,
+		); err != nil {
+			return nil, err
+		}
+
+		if dst.Export_bn_by_isrsResult == nil {
+			return nil, errors.New("No bottlenecks found")
+		}
+
+		return dst.Export_bn_by_isrsResult.BottleNeckType, nil
+	}
+
+	return storeBottlenecks(ctx, fetch, importID, conn, feedback)
 }