Mercurial > gemma
annotate pkg/controllers/stretches.go @ 3984:2fa052a44300
Set schema version in update-db.sh, not in extra scripts.
Also fixed handling of empty gemma_schema_version table.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Wed, 17 Jul 2019 13:54:01 +0200 |
parents | ad93adaeb688 |
children | 95786a675d70 |
rev | line source |
---|---|
3245
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 // Copyright (C) 2099 by via donau |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package controllers |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
17 "context" |
3245
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "database/sql" |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
19 "encoding/csv" |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
20 "fmt" |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
21 "log" |
3245
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 "net/http" |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
23 "runtime" |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
24 "strings" |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
25 "sync" |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
26 "time" |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
27 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
28 "gemma.intevation.de/gemma/pkg/middleware" |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
29 "github.com/gorilla/mux" |
3245
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 ) |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
32 const ( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
33 selectSectionBottlenecks = ` |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
34 SELECT |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
35 distinct(b.objnam), |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
36 b.limiting |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
37 FROM waterway.sections s, waterway.bottlenecks b |
3666
db87f34805fb
Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents:
3645
diff
changeset
|
38 WHERE b.validity @> current_timestamp |
db87f34805fb
Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents:
3645
diff
changeset
|
39 AND ST_Intersects(b.area, s.area) |
db87f34805fb
Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents:
3645
diff
changeset
|
40 AND s.name = $1` |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
41 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
42 selectStretchBottlenecks = ` |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
43 SELECT |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
44 distinct(b.objnam), |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
45 b.limiting |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
46 FROM waterway.stretches s, waterway.bottlenecks b |
3666
db87f34805fb
Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents:
3645
diff
changeset
|
47 WHERE b.validity @> current_timestamp |
db87f34805fb
Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents:
3645
diff
changeset
|
48 AND ST_Intersects(b.area, s.area) |
db87f34805fb
Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents:
3645
diff
changeset
|
49 AND s.name = $1` |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
50 ) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
51 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
52 type ( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
53 stretchBottleneck struct { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
54 name string |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
55 limiting string |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
56 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
57 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
58 stretchBottlenecks []stretchBottleneck |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
59 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
60 fullStretchBottleneck struct { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
61 *stretchBottleneck |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
62 measurements availMeasurements |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3420
diff
changeset
|
63 ldc []float64 |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3420
diff
changeset
|
64 breaks []float64 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
65 access func(*availMeasurement) float64 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
66 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
67 ) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
68 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
69 func (bns stretchBottlenecks) contains(limiting string) bool { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
70 for i := range bns { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
71 if bns[i].limiting == limiting { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
72 return true |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
73 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
74 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
75 return false |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
76 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
77 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
78 func loadFullStretchBottleneck( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
79 ctx context.Context, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
80 conn *sql.Conn, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
81 bn *stretchBottleneck, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
82 los int, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
83 from, to time.Time, |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3420
diff
changeset
|
84 depthbreaks, widthbreaks []float64, |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
85 ) (*fullStretchBottleneck, error) { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
86 measurements, err := loadDepthValues(ctx, conn, bn.name, los, from, to) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
87 if err != nil { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
88 return nil, err |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
89 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
90 ldc, err := loadLDCReferenceValue(ctx, conn, bn.name) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
91 if err != nil { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
92 return nil, err |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
93 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
94 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
95 var access func(*availMeasurement) float64 |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3420
diff
changeset
|
96 var breaks []float64 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
97 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
98 switch bn.limiting { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
99 case "width": |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
100 access = (*availMeasurement).getWidth |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
101 breaks = widthbreaks |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
102 case "depth": |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
103 access = (*availMeasurement).getDepth |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
104 breaks = depthbreaks |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
105 default: |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
106 log.Printf( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
107 "warn: unknown limitation '%s'. default to 'depth'.\n", |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
108 bn.limiting) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
109 access = (*availMeasurement).getDepth |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
110 breaks = depthbreaks |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
111 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
112 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
113 return &fullStretchBottleneck{ |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
114 stretchBottleneck: bn, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
115 measurements: measurements, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
116 ldc: ldc, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
117 breaks: breaks, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
118 access: access, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
119 }, nil |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
120 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
121 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
122 func loadStretchBottlenecks( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
123 ctx context.Context, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
124 conn *sql.Conn, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
125 stretch bool, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
126 name string, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
127 ) (stretchBottlenecks, error) { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
128 var sql string |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
129 if stretch { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
130 sql = selectStretchBottlenecks |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
131 } else { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
132 sql = selectSectionBottlenecks |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
133 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
134 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
135 rows, err := conn.QueryContext(ctx, sql, name) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
136 if err != nil { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
137 return nil, err |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
138 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
139 defer rows.Close() |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
140 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
141 var bns stretchBottlenecks |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
142 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
143 for rows.Next() { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
144 var bn stretchBottleneck |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
145 if err := rows.Scan( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
146 &bn.name, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
147 &bn.limiting, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
148 ); err != nil { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
149 return nil, err |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
150 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
151 bns = append(bns, bn) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
152 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
153 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
154 if err := rows.Err(); err != nil { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
155 return nil, err |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
156 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
157 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
158 return bns, nil |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
159 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
160 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
161 func stretchAvailableFairwayDepth(rw http.ResponseWriter, req *http.Request) { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
162 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
163 vars := mux.Vars(req) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
164 stretch := vars["kind"] == "stretch" |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
165 name := vars["name"] |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
166 mode := intervalMode(req.FormValue("mode")) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
167 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
168 depthbreaks, widthbreaks := afdRefs, afdRefs |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
169 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
170 from, ok := parseFormTime(rw, req, "from", time.Now().AddDate(-1, 0, 0)) |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
171 if !ok { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
172 return |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
173 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
174 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
175 to, ok := parseFormTime(rw, req, "to", from.AddDate(1, 0, 0)) |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
176 if !ok { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
177 return |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
178 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
179 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
180 if to.Before(from) { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
181 to, from = from, to |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
182 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
183 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
184 los, ok := parseFormInt(rw, req, "los", 1) |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
185 if !ok { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
186 return |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
187 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
188 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
189 conn := middleware.GetDBConn(req) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
190 ctx := req.Context() |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
191 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
192 bns, err := loadStretchBottlenecks(ctx, conn, stretch, name) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
193 if err != nil { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
194 http.Error( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
195 rw, fmt.Sprintf("DB error: %v.", err), |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
196 http.StatusInternalServerError) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
197 return |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
198 } |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
199 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
200 if len(bns) == 0 { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
201 http.Error(rw, "No bottlenecks found.", http.StatusNotFound) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
202 return |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
203 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
204 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
205 if b := req.FormValue("depthbreaks"); b != "" { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
206 depthbreaks = breaksToReferenceValue(b) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
207 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
208 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
209 if b := req.FormValue("widthbreaks"); b != "" { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
210 widthbreaks = breaksToReferenceValue(b) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
211 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
212 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
213 useDepth, useWidth := bns.contains("depth"), bns.contains("width") |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
214 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
215 if useDepth && useWidth && len(widthbreaks) != len(depthbreaks) { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
216 http.Error( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
217 rw, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
218 fmt.Sprintf("class breaks lengths differ: %d != %d", |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
219 len(widthbreaks), len(depthbreaks)), |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
220 http.StatusBadRequest, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
221 ) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
222 return |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
223 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
224 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
225 log.Printf("info: time interval: (%v - %v)\n", from, to) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
226 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
227 var loaded []*fullStretchBottleneck |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
228 var errors []error |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
229 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
230 for i := range bns { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
231 l, err := loadFullStretchBottleneck( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
232 ctx, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
233 conn, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
234 &bns[i], |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
235 los, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
236 from, to, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
237 depthbreaks, widthbreaks, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
238 ) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
239 if err != nil { |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
240 log.Printf("error: %v\n", err) |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
241 errors = append(errors, err) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
242 continue |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
243 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
244 loaded = append(loaded, l) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
245 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
246 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
247 if len(loaded) == 0 { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
248 http.Error( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
249 rw, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
250 fmt.Sprintf("No bottleneck loaded: %v", joinErrors(errors)), |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
251 http.StatusInternalServerError, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
252 ) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
253 return |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
254 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
255 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
256 n := runtime.NumCPU() / 2 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
257 if n == 0 { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
258 n = 1 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
259 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
260 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
261 type result struct { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
262 label string |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
263 from time.Time |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
264 to time.Time |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
265 ldc []time.Duration |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
266 breaks []time.Duration |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
267 } |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
268 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
269 jobCh := make(chan *result) |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
270 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
271 var wg sync.WaitGroup |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
272 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
273 for i := 0; i < n; i++ { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
274 wg.Add(1) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
275 go func() { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
276 defer wg.Done() |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
277 for res := range jobCh { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
278 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
279 var ldc, breaks []time.Duration |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
280 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
281 for _, bn := range loaded { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
282 l := bn.measurements.classify( |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
283 res.from, res.to, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
284 bn.ldc, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
285 bn.access, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
286 ) |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
287 b := bn.measurements.classify( |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
288 res.from, res.to, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
289 bn.breaks, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
290 bn.access, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
291 ) |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
292 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
293 if ldc == nil { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
294 ldc, breaks = l, b |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
295 } else { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
296 for i, v := range l { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
297 ldc[i] += v |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
298 } |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
299 for i, v := range b { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
300 breaks[i] += v |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
301 } |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
302 } |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
303 } |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
304 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
305 res.ldc = ldc |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
306 res.breaks = breaks |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
307 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
308 }() |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
309 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
310 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
311 var results []*result |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
312 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
313 interval := intervals[mode](from, to) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
314 |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3420
diff
changeset
|
315 var breaks []float64 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
316 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
317 if useDepth { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
318 breaks = depthbreaks |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
319 } else { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
320 breaks = widthbreaks |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
321 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
322 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
323 for pfrom, pto, label := interval(); label != ""; pfrom, pto, label = interval() { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
324 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
325 res := &result{ |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
326 label: label, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
327 from: pfrom, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
328 to: pto, |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
329 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
330 results = append(results, res) |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
331 jobCh <- res |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
332 } |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
333 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
334 close(jobCh) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
335 wg.Wait() |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
336 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
337 rw.Header().Add("Content-Type", "text/csv") |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
338 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
339 out := csv.NewWriter(rw) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
340 |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
341 // label, lnwl, classes |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
342 record := make([]string, 1+2+len(breaks)+1) |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
343 record[0] = "# time" |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
344 record[1] = "# < LDC [h]" |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
345 record[2] = "# >= LDC [h]" |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
346 for i, v := range breaks { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
347 if useDepth && useWidth { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
348 if i == 0 { |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
349 record[3] = "# < break_1 [h]" |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
350 } |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
351 record[i+4] = fmt.Sprintf("# >= break_%d", i+1) |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
352 } else { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
353 if i == 0 { |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
354 record[3] = fmt.Sprintf("# < %.1f [h]", v) |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
355 } |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
356 record[i+4] = fmt.Sprintf("# >= %.1f [h]", v) |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
357 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
358 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
359 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
360 if err := out.Write(record); err != nil { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
361 // Too late for HTTP status message. |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
362 log.Printf("error: %v\n", err) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
363 return |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
364 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
365 |
3420
b9fc6c546610
fairway availability: Normalize results to look like as we only have one bottleneck.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3405
diff
changeset
|
366 // Normalize to look like as we have only one bottleneck. |
b9fc6c546610
fairway availability: Normalize results to look like as we only have one bottleneck.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3405
diff
changeset
|
367 scale := 1 / float64(len(loaded)) |
b9fc6c546610
fairway availability: Normalize results to look like as we only have one bottleneck.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3405
diff
changeset
|
368 |
3470
104c4f683c74
fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3467
diff
changeset
|
369 empty := fmt.Sprintf("%.3f", 0.0) |
104c4f683c74
fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3467
diff
changeset
|
370 for i := range record[1:] { |
104c4f683c74
fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3467
diff
changeset
|
371 record[i+1] = empty |
104c4f683c74
fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3467
diff
changeset
|
372 } |
104c4f683c74
fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3467
diff
changeset
|
373 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
374 for _, r := range results { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
375 record[0] = r.label |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
376 for i, v := range r.ldc { |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
377 record[1+i] = fmt.Sprintf("%.3f", v.Hours()*scale) |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
378 } |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
379 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
380 for i, d := range r.breaks { |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
381 record[3+i] = fmt.Sprintf("%.3f", d.Hours()*scale) |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
382 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
383 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
384 if err := out.Write(record); err != nil { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
385 // Too late for HTTP status message. |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
386 log.Printf("error: %v\n", err) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
387 return |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
388 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
389 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
390 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
391 out.Flush() |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
392 if err := out.Error(); err != nil { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
393 // Too late for HTTP status message. |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
394 log.Printf("error: %v\n", err) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
395 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
396 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
397 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
398 func joinErrors(errors []error) string { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
399 var b strings.Builder |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
400 for _, err := range errors { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
401 if b.Len() > 0 { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
402 b.WriteString(", ") |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
403 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
404 b.WriteString(err.Error()) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
405 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
406 return b.String() |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
407 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
408 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
409 func stretchAvailabilty(rw http.ResponseWriter, req *http.Request) { |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
410 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
411 vars := mux.Vars(req) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
412 stretch := vars["kind"] == "stretch" |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
413 name := vars["name"] |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
414 mode := intervalMode(req.FormValue("mode")) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
415 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
416 if name == "" { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
417 http.Error( |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
418 rw, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
419 fmt.Sprintf("Missing %s name", vars["kind"]), |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
420 http.StatusBadRequest, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
421 ) |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
422 return |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
423 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
424 |
3519
a6128caca3c1
stretches/sections: availability. fixed typo in 'from' parameter parsing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3473
diff
changeset
|
425 from, ok := parseFormTime(rw, req, "from", time.Now().AddDate(-1, 0, 0)) |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
426 if !ok { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
427 return |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
428 } |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
429 |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
430 to, ok := parseFormTime(rw, req, "to", from.AddDate(1, 0, 0)) |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
431 if !ok { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
432 return |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
433 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
434 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
435 if to.Before(from) { |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
436 to, from = from, to |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
437 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
438 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
439 los, ok := parseFormInt(rw, req, "los", 1) |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
440 if !ok { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
441 return |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
442 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
443 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
444 depthbreaks, widthbreaks := afdRefs, afdRefs |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
445 |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
446 if b := req.FormValue("depthbreaks"); b != "" { |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
447 depthbreaks = breaksToReferenceValue(b) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
448 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
449 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
450 if b := req.FormValue("widthbreaks"); b != "" { |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
451 widthbreaks = breaksToReferenceValue(b) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
452 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
453 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
454 conn := middleware.GetDBConn(req) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
455 ctx := req.Context() |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
456 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
457 bns, err := loadStretchBottlenecks(ctx, conn, stretch, name) |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
458 if err != nil { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
459 http.Error( |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
460 rw, fmt.Sprintf("DB error: %v.", err), |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
461 http.StatusInternalServerError) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
462 return |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
463 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
464 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
465 if len(bns) == 0 { |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
466 http.Error( |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
467 rw, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
468 "No bottlenecks found.", |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
469 http.StatusNotFound, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
470 ) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
471 return |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
472 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
473 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
474 useDepth, useWidth := bns.contains("depth"), bns.contains("width") |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
475 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
476 if useDepth && useWidth && len(widthbreaks) != len(depthbreaks) { |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
477 http.Error( |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
478 rw, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
479 fmt.Sprintf("class breaks lengths differ: %d != %d", |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
480 len(widthbreaks), len(depthbreaks)), |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
481 http.StatusBadRequest, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
482 ) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
483 return |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
484 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
485 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
486 log.Printf("info: time interval: (%v - %v)\n", from, to) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
487 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
488 var loaded []*fullStretchBottleneck |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
489 var errors []error |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
490 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
491 for i := range bns { |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
492 l, err := loadFullStretchBottleneck( |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
493 ctx, |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
494 conn, |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
495 &bns[i], |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
496 los, |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
497 from, to, |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
498 depthbreaks, widthbreaks, |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
499 ) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
500 if err != nil { |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
501 log.Printf("error: %v\n", err) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
502 errors = append(errors, err) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
503 continue |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
504 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
505 loaded = append(loaded, l) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
506 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
507 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
508 if len(loaded) == 0 { |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
509 http.Error( |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
510 rw, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
511 fmt.Sprintf("No bottleneck loaded: %v", joinErrors(errors)), |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
512 http.StatusInternalServerError, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
513 ) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
514 return |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
515 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
516 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
517 n := runtime.NumCPU() / 2 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
518 if n == 0 { |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
519 n = 1 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
520 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
521 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
522 type result struct { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
523 label string |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
524 from time.Time |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
525 to time.Time |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
526 ldc []float64 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
527 breaks []float64 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
528 } |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
529 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
530 jobCh := make(chan *result) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
531 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
532 var wg sync.WaitGroup |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
533 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
534 for i := 0; i < n; i++ { |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
535 wg.Add(1) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
536 go func() { |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
537 defer wg.Done() |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
538 for res := range jobCh { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
539 var ldc, breaks []time.Duration |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
540 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
541 for _, bn := range loaded { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
542 l := bn.measurements.classify( |
3764
ad93adaeb688
Section/Stretch avail: Fixed classification interval calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3666
diff
changeset
|
543 res.from, res.to, |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
544 bn.ldc, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
545 (*availMeasurement).getValue, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
546 ) |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
547 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
548 b := bn.measurements.classify( |
3764
ad93adaeb688
Section/Stretch avail: Fixed classification interval calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3666
diff
changeset
|
549 res.from, res.to, |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
550 bn.breaks, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
551 bn.access, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
552 ) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
553 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
554 if ldc == nil { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
555 ldc, breaks = l, b |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
556 } else { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
557 for i, v := range l { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
558 ldc[i] += v |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
559 } |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
560 for i, v := range b { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
561 breaks[i] += v |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
562 } |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
563 } |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
564 } |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
565 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
566 duration := res.to.Sub(res.from) * time.Duration(len(loaded)) |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
567 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
568 res.ldc = durationsToPercentage(duration, ldc) |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
569 res.breaks = durationsToPercentage(duration, breaks) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
570 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
571 }() |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
572 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
573 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
574 var results []*result |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
575 |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
576 interval := intervals[mode](from, to) |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
577 |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
578 var breaks []float64 |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
579 |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
580 if useDepth { |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
581 breaks = depthbreaks |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
582 } else { |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
583 breaks = widthbreaks |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
584 } |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
585 |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
586 for pfrom, pto, label := interval(); label != ""; pfrom, pto, label = interval() { |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
587 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
588 res := &result{ |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
589 label: label, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
590 from: pfrom, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
591 to: pto, |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
592 } |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
593 results = append(results, res) |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
594 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
595 jobCh <- res |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
596 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
597 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
598 close(jobCh) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
599 wg.Wait() |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
600 |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
601 rw.Header().Add("Content-Type", "text/csv") |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
602 |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
603 out := csv.NewWriter(rw) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
604 |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
605 // label, lnwl, classes |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
606 record := make([]string, 1+2+len(breaks)+1) |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
607 record[0] = "# time" |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
608 record[1] = "# < LDC [%%]" |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
609 record[2] = "# >= LDC [%%]" |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
610 for i, v := range breaks { |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
611 if useDepth && useWidth { |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
612 if i == 0 { |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
613 record[3] = "# < break_1 [%%]" |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
614 } |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
615 record[i+4] = fmt.Sprintf("# >= break_%d [%%]", i+1) |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
616 } else { |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
617 if i == 0 { |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
618 record[3] = fmt.Sprintf("# < %.3f [%%]", v) |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
619 } |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
620 record[i+4] = fmt.Sprintf("# >= %.3f [%%]", v) |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
621 } |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
622 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
623 |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
624 if err := out.Write(record); err != nil { |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
625 // Too late for HTTP status message. |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
626 log.Printf("error: %v\n", err) |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
627 return |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
628 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
629 |
3470
104c4f683c74
fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3467
diff
changeset
|
630 empty := fmt.Sprintf("%.3f", 0.0) |
104c4f683c74
fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3467
diff
changeset
|
631 for i := range record[1:] { |
104c4f683c74
fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3467
diff
changeset
|
632 record[i+1] = empty |
104c4f683c74
fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3467
diff
changeset
|
633 } |
104c4f683c74
fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3467
diff
changeset
|
634 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
635 for _, res := range results { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
636 record[0] = res.label |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
637 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
638 for i, v := range res.ldc { |
3473
d66cae5be0a1
fairway availabilty: Fixed off-by-one array access when filling LDC classification into result rows.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3470
diff
changeset
|
639 record[1+i] = fmt.Sprintf("%.3f", v) |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
640 } |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
641 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
642 for i, v := range res.breaks { |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
643 record[3+i] = fmt.Sprintf("%.3f", v) |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
644 } |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
645 |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
646 if err := out.Write(record); err != nil { |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
647 // Too late for HTTP status message. |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
648 log.Printf("error: %v\n", err) |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
649 return |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
650 } |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
651 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
652 |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
653 out.Flush() |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
654 if err := out.Error(); err != nil { |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
655 // Too late for HTTP status message. |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
656 log.Printf("error: %v\n", err) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
657 } |
3245
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
658 } |