# HG changeset patch # User Sascha L. Teichmann # Date 1544184497 -3600 # Node ID 24445a618513b3bc18d8f583f13364aa1269b2f9 # Parent e7830ab6bacc7ed708b343ae7ed2e09835c24559 Added stub for bottleneck importer. diff -r e7830ab6bacc -r 24445a618513 pkg/common/json.go --- /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 + +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 +} diff -r e7830ab6bacc -r 24445a618513 pkg/controllers/srimports.go --- 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) diff -r e7830ab6bacc -r 24445a618513 pkg/imports/bn.go --- /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 + +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 +} diff -r e7830ab6bacc -r 24445a618513 pkg/imports/sr.go --- 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,