annotate pkg/models/sr.go @ 3645:02951a62e8c6

'Historicise' bottlenecks on import
author Tom Gottfried <tom@intevation.de>
date Wed, 12 Jun 2019 17:11:15 +0200
parents 479da494bc09
children 2a079d0a71c1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2018 by via donau
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 // * Bernhard E. Reiter <bernhard.reiter@intevation.de>
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 package models
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 import (
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "context"
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "database/sql"
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "encoding/json"
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 "errors"
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 "fmt"
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 "io"
2345
df6383831ad3 Sounding result import: Be more verbose if sounding result already exists for a bottleneck/date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
24
df6383831ad3 Sounding result import: Be more verbose if sounding result already exists for a bottleneck/date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
25 "gemma.intevation.de/gemma/pkg/common"
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 )
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 type (
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 SoundingResultMeta struct {
1900
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1572
diff changeset
30 Date Date `json:"date"`
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1572
diff changeset
31 Bottleneck string `json:"bottleneck"`
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1572
diff changeset
32 EPSG uint `json:"epsg"`
6a67cd819e93 To prepare stretch import made some model data types re-usable.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1572
diff changeset
33 DepthReference string `json:"depth-reference"`
3585
479da494bc09 SR import: access beam-type parameter supplied by client. TODO: Implement single beam code path.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3302
diff changeset
34 SingleBeam bool `json:"single-beam"`
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 }
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 )
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 const (
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 checkDepthReferenceSQL = `
2961
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
40 SELECT EXISTS(SELECT 1
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
41 FROM waterway.bottlenecks bn
3302
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 2961
diff changeset
42 JOIN waterway.gauges g
ec6163c6687d 'Historicise' gauges on import
Tom Gottfried <tom@intevation.de>
parents: 2961
diff changeset
43 ON bn.gauge_location = g.location AND bn.gauge_validity = g.validity
3645
02951a62e8c6 'Historicise' bottlenecks on import
Tom Gottfried <tom@intevation.de>
parents: 3585
diff changeset
44 JOIN waterway.gauges_reference_water_levels rl
02951a62e8c6 'Historicise' bottlenecks on import
Tom Gottfried <tom@intevation.de>
parents: 3585
diff changeset
45 ON g.location = rl.location AND g.validity = rl.validity
2961
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
46 WHERE bn.objnam = $1
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
47 AND rl.depth_reference = $2)`
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 checkBottleneckSQL = `
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 SELECT true FROM waterway.bottlenecks WHERE objnam = $1`
1297
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
51
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
52 checkBottleneckDateUniqueSQL = `
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
53 SELECT true FROM waterway.sounding_results sr JOIN
1572
056a86b24be2 Made bottleneck primary key an int. Attention: This may break something!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1373
diff changeset
54 waterway.bottlenecks bn ON sr.bottleneck_id = bn.id
1297
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
55 WHERE bn.objnam = $1 AND sr.date_info = $2`
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 )
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 func (m *SoundingResultMeta) Decode(r io.Reader) error {
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 err := json.NewDecoder(r).Decode(m)
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 if err == nil && m.EPSG == 0 {
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 m.EPSG = WGS84
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 }
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 return err
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 }
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65
1328
d753ce6cf588 To make golint happier made context.Context to be the first argument in all calls.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1297
diff changeset
66 func (m *SoundingResultMeta) Validate(ctx context.Context, conn *sql.Conn) []error {
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 var errs []error
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 var b bool
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 err := conn.QueryRowContext(ctx,
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 checkBottleneckSQL,
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 m.Bottleneck).Scan(&b)
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 switch {
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 case err == sql.ErrNoRows:
1330
785f14264426 Follow the rules for error messages to make golint happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1328
diff changeset
76 errs = append(errs, fmt.Errorf("unknown bottleneck '%s'", m.Bottleneck))
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 case err != nil:
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 errs = append(errs, err)
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 case !b:
1331
275ead19b9fd More on following the rules for error messages to make golint happy.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1330
diff changeset
80 errs = append(errs, errors.New("unexpected bottleneck"))
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 }
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82
1297
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
83 err = conn.QueryRowContext(ctx,
2961
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
84 checkDepthReferenceSQL,
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
85 m.Bottleneck,
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
86 m.DepthReference).Scan(&b)
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
87 switch {
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
88 case !b:
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
89 errs = append(errs,
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
90 fmt.Errorf("unknown depth reference '%s'", m.DepthReference))
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
91 case err != nil:
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
92 errs = append(errs, err)
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
93 }
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
94
5bc941d9ec43 Fix checking of depth reference for sounding result
Tom Gottfried <tom@intevation.de>
parents: 2345
diff changeset
95 err = conn.QueryRowContext(ctx,
1297
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
96 checkBottleneckDateUniqueSQL,
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
97 m.Bottleneck, m.Date.Time).Scan(&b)
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
98 switch {
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
99 case err == sql.ErrNoRows: // good! -> unique.
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
100 case err != nil:
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
101 errs = append(errs, err)
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
102 case b:
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
103 errs = append(errs,
2345
df6383831ad3 Sounding result import: Be more verbose if sounding result already exists for a bottleneck/date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
104 fmt.Errorf("sounding result for date %s already exists at bottleneck '%s'",
df6383831ad3 Sounding result import: Be more verbose if sounding result already exists for a bottleneck/date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
105 m.Date.Time.Format(common.DateFormat),
df6383831ad3 Sounding result import: Be more verbose if sounding result already exists for a bottleneck/date.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1900
diff changeset
106 m.Bottleneck))
1297
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
107 }
1c0c9190fcf2 Import sounding result: If uploading a zip with a meta.json which has
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 1224
diff changeset
108
1224
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 return errs
bc4b642c8d04 Started with an upload sounding result to temp upload area.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 }