annotate pkg/imports/wg.go @ 3310:e0dabe7b2fcf

Simplify gauges import Instead of checking some NOT NULL constraints in an extra loop, check all of them in the database.
author Tom Gottfried <tom@intevation.de>
date Fri, 17 May 2019 12:49:28 +0200
parents 5932f9574493
children 0f6b156cff55
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1808
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
2318
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
7 // Copyright (C) 2018, 2019 by via donau
1808
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
2318
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
13 // * Tom Gottfried <tom.gottfried@intevation.de>
1808
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 package imports
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 import (
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "context"
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "database/sql"
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
20 "time"
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
21
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
22 "github.com/jackc/pgx/pgtype"
1808
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
24 "gemma.intevation.de/gemma/pkg/models"
1810
7ee9bdaac336 Waterway gauge import: Added support for username/password.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1808
diff changeset
25 "gemma.intevation.de/gemma/pkg/soap/erdms"
1808
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 )
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 type WaterwayGauge struct {
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 // URL is the URL of the SOAP service.
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 URL string `json:"url"`
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 // Username is the username used to authenticate.
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 Username string `json:"username"`
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 // Passwort is the password to authenticate.
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 Password string `json:"password"`
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 // Insecure indicates if HTTPS traffic
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 // should validate certificates or not.
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 Insecure bool `json:"insecure"`
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 }
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 const WGJobKind JobKind = "wg"
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 type wgJobCreator struct{}
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 func init() {
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 RegisterJobCreator(WGJobKind, wgJobCreator{})
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 }
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 func (wgJobCreator) Description() string { return "waterway gauges" }
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
50 func (wgJobCreator) AutoAccept() bool { return true }
1808
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51
2187
7c83b5277c1c Import queue: Removed boilerplate code to deserialize jobs from JSON by making it part of the import queue.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2168
diff changeset
52 func (wgJobCreator) Create() Job { return new(WaterwayGauge) }
1808
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53
3219
4acbee65275d Import queue: Split locked dependencies in exclusively and multiple uses.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3163
diff changeset
54 func (wgJobCreator) Depends() [2][]string {
4acbee65275d Import queue: Split locked dependencies in exclusively and multiple uses.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3163
diff changeset
55 return [2][]string{
4acbee65275d Import queue: Split locked dependencies in exclusively and multiple uses.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3163
diff changeset
56 {"gauges_reference_water_levels", "gauges"},
4acbee65275d Import queue: Split locked dependencies in exclusively and multiple uses.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3163
diff changeset
57 {"depth_references"},
1808
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 }
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 }
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
61 // StageDone does nothing as there is no staging for gauges.
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
62 func (wgJobCreator) StageDone(context.Context, *sql.Tx, int64) error { return nil }
1808
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
64 // CleanUp does nothing as there is nothing to cleanup with gauges.
1808
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 func (*WaterwayGauge) CleanUp() error { return nil }
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66
1812
4910bcfab319 Waterway gauge import: Work around the expected (but nonetheless silly) namespace issues with the SOAP interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1810
diff changeset
67 const (
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
68 eraseGaugeSQL = `
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
69 UPDATE waterway.gauges SET
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
70 erased = true,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
71 -- Set enddate of old entry to new startdate in case of overlap:
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
72 validity = validity - $2
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
73 WHERE isrs_astext(location) = $1
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
74 AND NOT erased
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
75 -- Don't touch old entry if validity did not change: will be updated
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
76 AND validity <> $2
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
77 `
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
78
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
79 insertGaugeSQL = `
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
80 INSERT INTO waterway.gauges (
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
81 location,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
82 objname,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
83 geom,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
84 applicability_from_km,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
85 applicability_to_km,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
86 validity,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
87 zero_point,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
88 geodref,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
89 date_info,
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
90 source_organization,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
91 lastupdate
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
92 ) VALUES (
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
93 ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int),
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
94 $6,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
95 ST_SetSRID(ST_MakePoint($7, $8), 4326)::geography,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
96 $9,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
97 $10,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
98 $11,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
99 $12,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
100 $13,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
101 $14,
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
102 $15,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
103 $16
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
104 -- Exclusion constraints are not supported as arbiters.
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
105 -- Thus we need to DO NOTHING here and use an extra UPDATE statement
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
106 ) ON CONFLICT DO NOTHING
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
107 RETURNING 1
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
108 `
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
109 updateGaugeSQL = `
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
110 UPDATE waterway.gauges SET
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
111 objname = $6,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
112 geom = ST_SetSRID(ST_MakePoint($7, $8), 4326),
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
113 applicability_from_km = $9,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
114 applicability_to_km = $10,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
115 zero_point = $11,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
116 geodref = $12,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
117 date_info = $13,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
118 source_organization = $14,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
119 lastupdate = $15
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
120 WHERE location = ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int)
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
121 AND NOT erased
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
122 AND $15 > lastupdate
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
123 RETURNING 1
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
124 `
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
125
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
126 deleteReferenceWaterLevelsSQL = `
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
127 DELETE FROM waterway.gauges_reference_water_levels
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
128 WHERE isrs_astext(location) = $1
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
129 AND validity = $2
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
130 AND depth_reference <> ALL($3)
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
131 RETURNING depth_reference
1836
4dcdd8891770 Waterway gauges import: Fixed insert/update of gauges. TODO: Re-insert reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1835
diff changeset
132 `
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
133
2318
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
134 isNtSDepthRefSQL = `
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
135 SELECT EXISTS(SELECT 1 FROM depth_references WHERE depth_reference = $1)`
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
136
1837
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
137 insertReferenceWaterLevelsSQL = `
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
138 INSERT INTO waterway.gauges_reference_water_levels (
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
139 location,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
140 validity,
2317
8a8680e70d2e Cleanup schema for reference water levels
Tom Gottfried <tom@intevation.de>
parents: 2187
diff changeset
141 depth_reference,
1837
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
142 value
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
143 ) VALUES (
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
144 ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int),
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
145 $6,
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
146 $7,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
147 $8
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
148 ) ON CONFLICT (location, validity, depth_reference) DO UPDATE SET
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
149 value = EXCLUDED.value
1837
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
150 `
1812
4910bcfab319 Waterway gauge import: Work around the expected (but nonetheless silly) namespace issues with the SOAP interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1810
diff changeset
151 )
4910bcfab319 Waterway gauge import: Work around the expected (but nonetheless silly) namespace issues with the SOAP interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1810
diff changeset
152
1808
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
153 func (wg *WaterwayGauge) Do(
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
154 ctx context.Context,
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
155 importID int64,
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
156 conn *sql.Conn,
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157 feedback Feedback,
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
158 ) (interface{}, error) {
1810
7ee9bdaac336 Waterway gauge import: Added support for username/password.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1808
diff changeset
159
1837
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
160 start := time.Now()
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
161
2633
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
162 responseData, err := getRisData(
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
163 ctx,
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
164 conn,
2633
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
165 feedback,
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
166 wg.Username,
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
167 wg.Password,
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
168 wg.URL,
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
169 wg.Insecure,
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
170 "wtwgag")
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
171 if err != nil {
1812
4910bcfab319 Waterway gauge import: Work around the expected (but nonetheless silly) namespace issues with the SOAP interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1810
diff changeset
172 return nil, err
4910bcfab319 Waterway gauge import: Work around the expected (but nonetheless silly) namespace issues with the SOAP interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1810
diff changeset
173 }
4910bcfab319 Waterway gauge import: Work around the expected (but nonetheless silly) namespace issues with the SOAP interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1810
diff changeset
174
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
175 var eraseGaugeStmt, insertStmt, updateStmt,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
176 deleteReferenceWaterLevelsStmt,
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
177 isNtSDepthRefStmt, insertWaterLevelStmt *sql.Stmt
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
178 for _, x := range []struct {
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
179 sql string
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
180 stmt **sql.Stmt
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
181 }{
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
182 {eraseGaugeSQL, &eraseGaugeStmt},
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
183 {insertGaugeSQL, &insertStmt},
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
184 {updateGaugeSQL, &updateStmt},
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
185 {deleteReferenceWaterLevelsSQL, &deleteReferenceWaterLevelsStmt},
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
186 {isNtSDepthRefSQL, &isNtSDepthRefStmt},
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
187 {insertReferenceWaterLevelsSQL, &insertWaterLevelStmt},
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
188 } {
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
189 var err error
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
190 if *x.stmt, err = conn.PrepareContext(ctx, x.sql); err != nil {
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
191 return nil, err
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
192 }
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
193 defer (*x.stmt).Close()
1812
4910bcfab319 Waterway gauge import: Work around the expected (but nonetheless silly) namespace issues with the SOAP interface.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1810
diff changeset
194 }
1810
7ee9bdaac336 Waterway gauge import: Added support for username/password.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1808
diff changeset
195
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
196 var gauges []string
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
197 var unchanged int
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
198
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
199 for _, data := range responseData {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
200 for _, dr := range data.RisdataReturn {
1836
4dcdd8891770 Waterway gauges import: Fixed insert/update of gauges. TODO: Re-insert reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1835
diff changeset
201
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
202 isrs := string(*dr.RisidxCode)
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
203 code, err := models.IsrsFromString(isrs)
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
204 if err != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
205 feedback.Warn("Invalid ISRS code '%s': %v", isrs, err)
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
206 continue
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
207 }
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
208 gauges = append(gauges, isrs)
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
209 feedback.Info("Processing %s", code)
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
210
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
211 var from, to sql.NullInt64
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
212
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
213 if dr.Applicabilityfromkm != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
214 from = sql.NullInt64{
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
215 Int64: int64(*dr.Applicabilityfromkm),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
216 Valid: true,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
217 }
1836
4dcdd8891770 Waterway gauges import: Fixed insert/update of gauges. TODO: Re-insert reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1835
diff changeset
218 }
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
219 if dr.Applicabilitytokm != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
220 to = sql.NullInt64{
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
221 Int64: int64(*dr.Applicabilitytokm),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
222 Valid: true,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
223 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
224 }
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
225
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
226 var tfrom, tto, dateInfo pgtype.Timestamptz
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
227
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
228 if dr.Startdate != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
229 tfrom = pgtype.Timestamptz{
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
230 Time: time.Time(*dr.Startdate),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
231 Status: pgtype.Present,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
232 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
233 } else {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
234 tfrom = pgtype.Timestamptz{
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
235 Status: pgtype.Null,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
236 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
237 }
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
238
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
239 if dr.Enddate != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
240 tto = pgtype.Timestamptz{
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
241 Time: time.Time(*dr.Enddate),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
242 Status: pgtype.Present,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
243 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
244 } else {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
245 tto = pgtype.Timestamptz{
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
246 Status: pgtype.Null,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
247 }
1836
4dcdd8891770 Waterway gauges import: Fixed insert/update of gauges. TODO: Re-insert reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1835
diff changeset
248 }
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
249
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
250 validity := pgtype.Tstzrange{
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
251 Lower: tfrom,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
252 Upper: tto,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
253 LowerType: pgtype.Inclusive,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
254 UpperType: pgtype.Exclusive,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
255 Status: pgtype.Present,
1836
4dcdd8891770 Waterway gauges import: Fixed insert/update of gauges. TODO: Re-insert reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1835
diff changeset
256 }
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
257
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
258 if dr.Infodate != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
259 dateInfo = pgtype.Timestamptz{
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
260 Time: time.Time(*dr.Infodate),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
261 Status: pgtype.Present,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
262 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
263 } else {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
264 dateInfo = pgtype.Timestamptz{
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
265 Status: pgtype.Null,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
266 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
267 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
268
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
269 var geodref sql.NullString
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
270 if dr.Geodref != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
271 geodref = sql.NullString{
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
272 String: string(*dr.Geodref),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
273 Valid: true,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
274 }
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
275 }
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
276
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
277 var source sql.NullString
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
278 if dr.Source != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
279 source = sql.NullString{
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
280 String: string(*dr.Source),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
281 Valid: true,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
282 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
283 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
284
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
285 tx, err := conn.BeginTx(ctx, nil)
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
286 if err != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
287 return nil, err
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
288 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
289 defer tx.Rollback()
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
290
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
291 // Mark old entries of gauge as erased, if applicable
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
292 if _, err := tx.StmtContext(ctx, eraseGaugeStmt).ExecContext(ctx,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
293 code.String(),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
294 validity,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
295 ); err != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
296 feedback.Warn(handleError(err).Error())
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
297 if err2 := tx.Rollback(); err2 != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
298 return nil, err2
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
299 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
300 unchanged++
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
301 continue
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
302 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
303
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
304 // Try to insert gauge entry
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
305 var dummy int
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
306 err = tx.StmtContext(ctx, insertStmt).QueryRowContext(ctx,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
307 code.CountryCode,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
308 code.LoCode,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
309 code.FairwaySection,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
310 code.Orc,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
311 code.Hectometre,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
312 dr.Objname.Loc,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
313 dr.Lon, dr.Lat,
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
314 from,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
315 to,
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
316 &validity,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
317 dr.Zeropoint,
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
318 geodref,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
319 &dateInfo,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
320 source,
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
321 time.Time(*dr.Lastupdate),
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
322 ).Scan(&dummy)
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
323 switch {
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
324 case err == sql.ErrNoRows:
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
325 // Assume constraint conflict, try to update
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
326 err2 := tx.StmtContext(ctx, updateStmt).QueryRowContext(ctx,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
327 code.CountryCode,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
328 code.LoCode,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
329 code.FairwaySection,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
330 code.Orc,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
331 code.Hectometre,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
332 dr.Objname.Loc,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
333 dr.Lon, dr.Lat,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
334 from,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
335 to,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
336 dr.Zeropoint,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
337 geodref,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
338 &dateInfo,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
339 source,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
340 time.Time(*dr.Lastupdate),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
341 ).Scan(&dummy)
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
342 switch {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
343 case err2 == sql.ErrNoRows:
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
344 feedback.Info("unchanged")
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
345 if err3 := tx.Rollback(); err3 != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
346 return nil, err3
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
347 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
348 unchanged++
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
349 continue
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
350 case err2 != nil:
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
351 feedback.Warn(handleError(err2).Error())
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
352 if err3 := tx.Rollback(); err3 != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
353 return nil, err3
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
354 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
355 unchanged++
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
356 continue
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
357 default:
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
358 feedback.Info("update")
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
359 }
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
360
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
361 // Remove obsolete reference water levels
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
362 var currLevels pgtype.VarcharArray
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
363 currLevels.Set([]string{
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
364 string(*dr.Reflevel1code),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
365 string(*dr.Reflevel2code),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
366 string(*dr.Reflevel3code),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
367 })
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
368 rwls, err := tx.StmtContext(ctx,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
369 deleteReferenceWaterLevelsStmt).QueryContext(ctx,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
370 code.String(),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
371 &validity,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
372 &currLevels,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
373 )
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
374 if err != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
375 return nil, err
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
376 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
377 defer rwls.Close()
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
378 for rwls.Next() {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
379 var delRef string
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
380 if err := rwls.Scan(&delRef); err != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
381 return nil, err
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
382 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
383 feedback.Warn("Removed reference water level %s from %s",
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
384 delRef, code)
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
385 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
386 if err := rwls.Err(); err != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
387 return nil, err
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
388 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
389 case err != nil:
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
390 feedback.Warn(handleError(err).Error())
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
391 if err2 := tx.Rollback(); err2 != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
392 return nil, err2
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
393 }
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
394 unchanged++
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
395 continue
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
396 default:
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
397 feedback.Info("insert new version")
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
398 }
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
399
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
400 // "Upsert" reference water levels
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
401 for _, wl := range []struct {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
402 level **erdms.RisreflevelcodeType
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
403 value **erdms.RisreflevelvalueType
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
404 }{
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
405 {&dr.Reflevel1code, &dr.Reflevel1value},
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
406 {&dr.Reflevel2code, &dr.Reflevel2value},
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
407 {&dr.Reflevel3code, &dr.Reflevel3value},
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
408 } {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
409 if *wl.level == nil || *wl.value == nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
410 continue
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
411 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
412
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
413 var isNtSDepthRef bool
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
414 if err := tx.StmtContext(
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
415 ctx, isNtSDepthRefStmt).QueryRowContext(ctx,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
416 string(**wl.level),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
417 ).Scan(
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
418 &isNtSDepthRef,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
419 ); err != nil {
3305
5932f9574493 Follow-up for 3350:e640f51b5a4e (Fix reporting of removed reference water levels).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3302
diff changeset
420 return nil, err
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
421 }
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
422 if !isNtSDepthRef {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
423 feedback.Warn(
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
424 "Reference level code '%s' is not in line "+
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
425 "with the NtS reference_code table",
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
426 string(**wl.level))
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
427 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
428
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
429 if _, err := tx.StmtContext(
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
430 ctx, insertWaterLevelStmt).ExecContext(ctx,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
431 code.CountryCode,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
432 code.LoCode,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
433 code.FairwaySection,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
434 code.Orc,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
435 code.Hectometre,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
436 &validity,
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
437 string(**wl.level),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
438 int64(**wl.value),
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
439 ); err != nil {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
440 feedback.Warn(handleError(err).Error())
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
441 tx.Rollback()
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
442 continue
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
443 }
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
444 }
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
445
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
446 if err = tx.Commit(); err != nil {
3305
5932f9574493 Follow-up for 3350:e640f51b5a4e (Fix reporting of removed reference water levels).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3302
diff changeset
447 return nil, err
5932f9574493 Follow-up for 3350:e640f51b5a4e (Fix reporting of removed reference water levels).
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3302
diff changeset
448 }
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
449 }
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
450 }
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
451
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
452 feedback.Info("Importing gauges took %s",
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
453 time.Since(start))
1837
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
454
3310
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
455 if len(gauges) == 0 {
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
456 return nil, UnchangedError("No gauges returned from ERDMS")
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
457 }
e0dabe7b2fcf Simplify gauges import
Tom Gottfried <tom@intevation.de>
parents: 3305
diff changeset
458
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
459 if unchanged == len(gauges) {
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
460 return nil, UnchangedError("All gauges unchanged")
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
461 }
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 3286
diff changeset
462
1837
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
463 return nil, err
1808
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
464 }