changeset 1531:24445a618513

Added stub for bottleneck importer.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 07 Dec 2018 13:08:17 +0100
parents e7830ab6bacc
children 44e094330272
files pkg/common/json.go pkg/controllers/srimports.go pkg/imports/bn.go pkg/imports/sr.go
diffstat 4 files changed, 140 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/common/json.go	Fri Dec 07 13:08:17 2018 +0100
@@ -0,0 +1,34 @@
+// This is Free Software under GNU Affero General Public License v >= 3.0
+// without warranty, see README.md and license for details.
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// License-Filename: LICENSES/AGPL-3.0.txt
+//
+// Copyright (C) 2018 by via donau
+//   – Österreichische Wasserstraßen-Gesellschaft mbH
+// Software engineering by Intevation GmbH
+//
+// Author(s):
+//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+package common
+
+import (
+	"encoding/json"
+	"strings"
+)
+
+// FromJSONString revives data from a JSON string.
+func FromJSONString(data string, dst interface{}) error {
+	return json.NewDecoder(strings.NewReader(data)).Decode(dst)
+}
+
+// ToJSONString serializes src into a string to
+// be revived by FromJSONString.
+func ToJSONString(src interface{}) (string, error) {
+	var b strings.Builder
+	if err := json.NewEncoder(&b).Encode(src); err != nil {
+		return "", err
+	}
+	return b.String(), nil
+}
--- a/pkg/controllers/srimports.go	Fri Dec 07 12:31:37 2018 +0100
+++ b/pkg/controllers/srimports.go	Fri Dec 07 13:08:17 2018 +0100
@@ -152,7 +152,7 @@
 	}
 	sr.Dir = dir
 
-	serialized, err := sr.ToString()
+	serialized, err := common.ToJSONString(sr)
 	if err != nil {
 		log.Printf("error: %v\n", err)
 		http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/imports/bn.go	Fri Dec 07 13:08:17 2018 +0100
@@ -0,0 +1,104 @@
+// This is Free Software under GNU Affero General Public License v >= 3.0
+// without warranty, see README.md and license for details.
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// License-Filename: LICENSES/AGPL-3.0.txt
+//
+// Copyright (C) 2018 by via donau
+//   – Österreichische Wasserstraßen-Gesellschaft mbH
+// Software engineering by Intevation GmbH
+//
+// Author(s):
+//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+package imports
+
+import (
+	"context"
+	"database/sql"
+	"errors"
+
+	"gemma.intevation.de/gemma/pkg/common"
+	"gemma.intevation.de/gemma/pkg/soap/ifbn"
+)
+
+type Bottleneck struct {
+	URL      string `json:"url"`
+	Insecure bool   `json:"insecure"`
+}
+
+const BNJobKind JobKind = "bn"
+
+type bnJobCreator struct{}
+
+func init() {
+	RegisterJobCreator(BNJobKind, bnJobCreator{})
+}
+
+func (bnJobCreator) Create(_ JobKind, data string) (Job, error) {
+	bn := new(Bottleneck)
+	if err := common.FromJSONString(data, bn); err != nil {
+		return nil, err
+	}
+	return bn, nil
+}
+
+func (bnJobCreator) Depends() []string {
+	return []string{
+		"waterway.gauges",
+		"waterway.bottlenecks",
+	}
+}
+
+const (
+	bnStageDoneSQL = `
+UPDATE waterway.sounding_results SET staging_done = true
+WHERE id = (
+  SELECT key from waterway.track_imports
+  WHERE import_id = $1 AND
+        relation = 'waterway.sounding_results'::regclass)`
+)
+
+// StageDone moves the imported bottleneck out of the staging area.
+func (bnJobCreator) StageDone(
+	ctx context.Context,
+	tx *sql.Tx,
+	id int64,
+) error {
+	_, err := tx.ExecContext(ctx, srStageDoneSQL, id)
+	return err
+}
+
+// CleanUp of a bottleneck import is a NOP.
+func (bn *Bottleneck) CleanUp() error { return nil }
+
+// Do executes the actual bottleneck import.
+func (bn *Bottleneck) Do(
+	ctx context.Context,
+	importID int64,
+	conn *sql.Conn,
+	feedback Feedback,
+) (interface{}, error) {
+	client := ifbn.NewIBottleneckService(bn.URL, bn.Insecure, nil)
+
+	req := &ifbn.Export_bn_by_isrs{}
+
+	resp, err := client.Export_bn_by_isrs(req)
+	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
+	}
+
+	// TODO: Implement me!
+	bns := resp.Export_bn_by_isrsResult.BottleNeckType
+
+	_ = bns
+
+	return nil, nil
+}
--- a/pkg/imports/sr.go	Fri Dec 07 12:31:37 2018 +0100
+++ b/pkg/imports/sr.go	Fri Dec 07 13:08:17 2018 +0100
@@ -21,7 +21,6 @@
 	"crypto/sha1"
 	"database/sql"
 	"encoding/hex"
-	"encoding/json"
 	"errors"
 	"fmt"
 	"io"
@@ -76,7 +75,7 @@
 
 func (srJobCreator) Create(_ JobKind, data string) (Job, error) {
 	sr := new(SoundingResult)
-	if err := sr.FromString(data); err != nil {
+	if err := common.FromJSONString(data, sr); err != nil {
 		return nil, err
 	}
 	return sr, nil
@@ -169,21 +168,6 @@
 `
 )
 
-// FromString revives a SoundingResult import from a string.
-func (sr *SoundingResult) FromString(data string) error {
-	return json.NewDecoder(strings.NewReader(data)).Decode(sr)
-}
-
-// ToString serializes a SoundingResult import into a string to
-// be revived by FromString.
-func (sr *SoundingResult) ToString() (string, error) {
-	var b strings.Builder
-	if err := json.NewEncoder(&b).Encode(sr); err != nil {
-		return "", err
-	}
-	return b.String(), nil
-}
-
 // Do executes the actual sounding result import.
 func (sr *SoundingResult) Do(
 	ctx context.Context,