annotate pkg/imports/wg.go @ 3286:e640f51b5a4e

Fix reporting of removed reference water levels Since more than one reference water level might be removed, do not discard all but the first returned row.
author Tom Gottfried <tom@intevation.de>
date Thu, 16 May 2019 11:53:44 +0200
parents 4acbee65275d
children ec6163c6687d
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 (
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
68 deleteReferenceWaterLevelsSQL = `
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
69 DELETE FROM waterway.gauges_reference_water_levels
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
70 WHERE gauge_id = ($1::char(2), $2::char(3), $3::char(5), $4::char(5), $5::int)
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
71 AND depth_reference <> ALL($6)
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
72 RETURNING depth_reference
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
73 `
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
74
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
75 insertGaugeSQL = `
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
76 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
77 location,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
78 objname,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
79 geom,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
80 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
81 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
82 validity,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
83 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
84 geodref,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
85 date_info,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
86 source_organization
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
87 ) VALUES (
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
88 ($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
89 $6,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
90 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
91 $9,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
92 $10,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
93 $11,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
94 $12,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
95 $13,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
96 $14,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
97 $15
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
98 ) ON CONFLICT (location) DO UPDATE SET
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
99 objname = EXCLUDED.objname,
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
100 geom = EXCLUDED.geom,
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
101 applicability_from_km = EXCLUDED.applicability_from_km,
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
102 applicability_to_km = EXCLUDED.applicability_to_km,
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
103 validity = EXCLUDED.validity,
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
104 zero_point = EXCLUDED.zero_point,
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
105 geodref = EXCLUDED.geodref,
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
106 date_info = EXCLUDED.date_info,
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
107 source_organization = EXCLUDED.source_organization
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
108 `
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
109
2318
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
110 isNtSDepthRefSQL = `
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
111 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
112
1837
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
113 insertReferenceWaterLevelsSQL = `
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
114 INSERT INTO waterway.gauges_reference_water_levels (
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
115 gauge_id,
2317
8a8680e70d2e Cleanup schema for reference water levels
Tom Gottfried <tom@intevation.de>
parents: 2187
diff changeset
116 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
117 value
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
118 ) VALUES (
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
119 ($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
120 $6,
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
121 $7
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
122 ) ON CONFLICT (gauge_id, depth_reference) DO UPDATE SET
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
123 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
124 `
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
125 )
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
126
1808
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127 func (wg *WaterwayGauge) Do(
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 ctx context.Context,
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129 importID int64,
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 conn *sql.Conn,
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131 feedback Feedback,
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132 ) (interface{}, error) {
1810
7ee9bdaac336 Waterway gauge import: Added support for username/password.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1808
diff changeset
133
1837
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
134 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
135
2633
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
136 responseData, err := getRisData(
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
137 ctx,
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
138 conn,
2633
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
139 feedback,
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
140 wg.Username,
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
141 wg.Password,
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
142 wg.URL,
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
143 wg.Insecure,
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
144 "wtwgag")
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
145 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
146 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
147 }
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
148
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
149 var ignored int
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
150
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
151 type idxCode struct {
2633
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
152 jdx int
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
153 idx int
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
154 code *models.Isrs
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
155 }
1810
7ee9bdaac336 Waterway gauge import: Added support for username/password.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1808
diff changeset
156
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
157 var gauges []idxCode
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
158
2633
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
159 for j, data := range responseData {
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
160 for i, dr := range data.RisdataReturn {
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
161 if dr.RisidxCode == nil {
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
162 ignored++
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
163 continue
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
164 }
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
165 code, err := models.IsrsFromString(string(*dr.RisidxCode))
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
166 if err != nil {
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
167 feedback.Warn("invalid ISRS code %v", err)
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
168 ignored++
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
169 continue
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
170 }
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
171
2633
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
172 if dr.Objname.Loc == nil {
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
173 feedback.Warn("missing objname: %s", code)
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
174 ignored++
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
175 continue
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
176 }
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
177
2633
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
178 if dr.Lat == nil || dr.Lon == nil {
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
179 feedback.Warn("missing lat/lon: %s", code)
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
180 ignored++
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
181 continue
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
182 }
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
183
2633
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
184 if dr.Zeropoint == nil {
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
185 feedback.Warn("missing zeropoint: %s", code)
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
186 ignored++
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
187 continue
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
188 }
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
189
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
190 gauges = append(gauges, idxCode{jdx: j, idx: i, code: code})
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
191 }
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
192 }
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
193 feedback.Info("ignored gauges: %d", ignored)
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
194 feedback.Info("insert/update gauges: %d", len(gauges))
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
195
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
196 if len(gauges) == 0 {
1975
d966f03ea819 Imports: Added the new state 'unchanged' which can be issued by the imports to indicate that the database is not modified by the particular imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1918
diff changeset
197 return nil, UnchangedError("nothing to do")
1829
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
198 }
b4b9089c2d79 Waterway gauges: Started with deleting old gauges to be overwritten.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1812
diff changeset
199
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
200 // insert/update the gauges
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
201 var insertStmt, deleteReferenceWaterLevelsStmt,
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
202 isNtSDepthRefStmt, insertWaterLevelStmt *sql.Stmt
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
203 for _, x := range []struct {
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
204 sql string
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
205 stmt **sql.Stmt
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
206 }{
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
207 {insertGaugeSQL, &insertStmt},
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
208 {deleteReferenceWaterLevelsSQL, &deleteReferenceWaterLevelsStmt},
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
209 {isNtSDepthRefSQL, &isNtSDepthRefStmt},
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
210 {insertReferenceWaterLevelsSQL, &insertWaterLevelStmt},
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
211 } {
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
212 var err error
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
213 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
214 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
215 }
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
216 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
217 }
1810
7ee9bdaac336 Waterway gauge import: Added support for username/password.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1808
diff changeset
218
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
219 for i := range gauges {
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
220 ic := &gauges[i]
2633
eb1d119f253f Fetch data from ERDMS for all allowed countries
Tom Gottfried <tom@intevation.de>
parents: 2518
diff changeset
221 dr := responseData[ic.jdx].RisdataReturn[ic.idx]
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
222
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
223 feedback.Info("insert/update %s", ic.code)
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
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 var from, to sql.NullInt64
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
226
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
227 if dr.Applicabilityfromkm != nil {
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
228 from = sql.NullInt64{
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
229 Int64: int64(*dr.Applicabilityfromkm),
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
230 Valid: true,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
231 }
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
232 }
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
233 if dr.Applicabilitytokm != nil {
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
234 to = sql.NullInt64{
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
235 Int64: int64(*dr.Applicabilitytokm),
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
236 Valid: true,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
237 }
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
238 }
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
239
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
240 var tfrom, tto, dateInfo pgtype.Timestamptz
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
241
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
242 if dr.Startdate != nil {
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
243 tfrom = pgtype.Timestamptz{
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
244 Time: time.Time(*dr.Startdate),
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
245 Status: pgtype.Present,
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
246 }
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
247 } else {
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 tfrom = pgtype.Timestamptz{
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
249 Status: pgtype.Null,
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
250 }
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
251 }
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
252
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
253 if dr.Enddate != nil {
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
254 tto = pgtype.Timestamptz{
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
255 Time: time.Time(*dr.Enddate),
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 Status: pgtype.Present,
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
257 }
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
258 } else {
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
259 tto = pgtype.Timestamptz{
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
260 Status: pgtype.Null,
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
261 }
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
262 }
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
263
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
264 validity := pgtype.Tstzrange{
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
265 Lower: tfrom,
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
266 Upper: tto,
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
267 LowerType: pgtype.Inclusive,
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
268 UpperType: pgtype.Inclusive,
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
269 Status: pgtype.Present,
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
270 }
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
271
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
272 if dr.Infodate != nil {
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
273 dateInfo = pgtype.Timestamptz{
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
274 Time: time.Time(*dr.Infodate),
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
275 Status: pgtype.Present,
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
276 }
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
277 } else {
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
278 dateInfo = pgtype.Timestamptz{
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
279 Status: pgtype.Null,
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
280 }
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
281 }
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
282
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
283 var geodref sql.NullString
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
284 if dr.Geodref != nil {
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
285 geodref = sql.NullString{
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
286 String: string(*dr.Geodref),
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
287 Valid: true,
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
288 }
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
289 }
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
290
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
291 var source sql.NullString
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
292 if dr.Source != nil {
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
293 source = sql.NullString{
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
294 String: string(*dr.Source),
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
295 Valid: true,
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
296 }
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
297 }
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
298
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
299 tx, err := conn.BeginTx(ctx, nil)
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
300 if err != nil {
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
301 return nil, err
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
302 }
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
303 defer tx.Rollback()
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
304
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
305 if _, err := tx.StmtContext(ctx, insertStmt).ExecContext(ctx,
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
306 ic.code.CountryCode,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
307 ic.code.LoCode,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
308 ic.code.FairwaySection,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
309 ic.code.Orc,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
310 ic.code.Hectometre,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
311 string(*dr.Objname.Loc),
2518
e199655809c1 Dont cast lat/lon values from ERDMS service to ints.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 2318
diff changeset
312 float64(*dr.Lon), float64(*dr.Lat),
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
313 from,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
314 to,
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
315 &validity,
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
316 float64(*dr.Zeropoint),
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
317 geodref,
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
318 &dateInfo,
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
319 source,
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
320 ); err != nil {
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
321 feedback.Warn(handleError(err).Error())
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
322 tx.Rollback()
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
323 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
324 }
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
325
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
326 // Remove obsolete reference water levels
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
327 var currLevels pgtype.VarcharArray
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
328 currLevels.Set([]string{
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
329 string(*dr.Reflevel1code),
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
330 string(*dr.Reflevel2code),
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
331 string(*dr.Reflevel3code),
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
332 })
3286
e640f51b5a4e Fix reporting of removed reference water levels
Tom Gottfried <tom@intevation.de>
parents: 3219
diff changeset
333 rwls, err := tx.StmtContext(
e640f51b5a4e Fix reporting of removed reference water levels
Tom Gottfried <tom@intevation.de>
parents: 3219
diff changeset
334 ctx, deleteReferenceWaterLevelsStmt).QueryContext(ctx,
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
335 ic.code.CountryCode,
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
336 ic.code.LoCode,
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
337 ic.code.FairwaySection,
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
338 ic.code.Orc,
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
339 ic.code.Hectometre,
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
340 &currLevels,
3286
e640f51b5a4e Fix reporting of removed reference water levels
Tom Gottfried <tom@intevation.de>
parents: 3219
diff changeset
341 )
e640f51b5a4e Fix reporting of removed reference water levels
Tom Gottfried <tom@intevation.de>
parents: 3219
diff changeset
342 if err != nil {
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
343 return nil, err
3286
e640f51b5a4e Fix reporting of removed reference water levels
Tom Gottfried <tom@intevation.de>
parents: 3219
diff changeset
344 }
e640f51b5a4e Fix reporting of removed reference water levels
Tom Gottfried <tom@intevation.de>
parents: 3219
diff changeset
345 defer rwls.Close()
e640f51b5a4e Fix reporting of removed reference water levels
Tom Gottfried <tom@intevation.de>
parents: 3219
diff changeset
346 for rwls.Next() {
e640f51b5a4e Fix reporting of removed reference water levels
Tom Gottfried <tom@intevation.de>
parents: 3219
diff changeset
347 var delRef string
e640f51b5a4e Fix reporting of removed reference water levels
Tom Gottfried <tom@intevation.de>
parents: 3219
diff changeset
348 if err = rwls.Scan(&delRef); err != nil {
e640f51b5a4e Fix reporting of removed reference water levels
Tom Gottfried <tom@intevation.de>
parents: 3219
diff changeset
349 return nil, err
e640f51b5a4e Fix reporting of removed reference water levels
Tom Gottfried <tom@intevation.de>
parents: 3219
diff changeset
350 }
e640f51b5a4e Fix reporting of removed reference water levels
Tom Gottfried <tom@intevation.de>
parents: 3219
diff changeset
351 feedback.Warn("Removed reference water level %s from %s",
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
352 delRef, ic.code)
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
353 }
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
354
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
355 // Insert/update reference water levels
1837
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
356 for _, wl := range []struct {
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
357 level **erdms.RisreflevelcodeType
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
358 value **erdms.RisreflevelvalueType
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
359 }{
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
360 {&dr.Reflevel1code, &dr.Reflevel1value},
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
361 {&dr.Reflevel2code, &dr.Reflevel2value},
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
362 {&dr.Reflevel3code, &dr.Reflevel3value},
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
363 } {
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
364 if *wl.level == nil || *wl.value == nil {
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
365 continue
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
366 }
2318
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
367
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
368 var isNtSDepthRef bool
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
369 if err := tx.StmtContext(ctx, isNtSDepthRefStmt).QueryRowContext(
2318
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
370 ctx,
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
371 string(**wl.level),
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
372 ).Scan(
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
373 &isNtSDepthRef,
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
374 ); err != nil {
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
375 return nil, err
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
376 }
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
377 if !isNtSDepthRef {
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
378 feedback.Warn(
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
379 "Reference level code '%s' is not in line "+
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
380 "with the NtS reference_code table",
2318
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
381 string(**wl.level))
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
382 }
06c4e57435f1 Warn on import of unknown reference level codes
Tom Gottfried <tom@intevation.de>
parents: 2317
diff changeset
383
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
384 if _, err := tx.StmtContext(ctx, insertWaterLevelStmt).ExecContext(
1837
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
385 ctx,
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
386 ic.code.CountryCode,
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
387 ic.code.LoCode,
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
388 ic.code.FairwaySection,
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
389 ic.code.Orc,
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
390 ic.code.Hectometre,
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
391 string(**wl.level),
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
392 int64(**wl.value),
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
393 ); err != nil {
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
394 feedback.Warn(handleError(err).Error())
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
395 tx.Rollback()
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
396 continue
1837
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
397 }
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
398 }
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
399
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
400 if err = tx.Commit(); err != nil {
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
401 return nil, err
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
402 }
1835
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
403 }
f7b926440449 Waterway gauge import: More stuggling with inserting gauges. Not working, yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1829
diff changeset
404
3163
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
405 feedback.Info("Refreshing gauges took %s.",
d9903cb34842 Handle failing INSERTs gracefully during gauges import
Tom Gottfried <tom@intevation.de>
parents: 2633
diff changeset
406 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
407
00d63eb9306a Waterway gauges import: Finalized with re-inserting the reference water levels.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1836
diff changeset
408 return nil, err
1808
77582da3adb0 Waterway gauges import: Added stub.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
409 }