Mercurial > gemma
annotate pkg/controllers/stretches.go @ 4343:63c25eb9c07c
FA: be optimistic about missing data.
According to clarification, missing data has to be interpreted as the best case,
this is, because the services do not provide data for bottlenecks, which are not
considered a limitating factor on the water way at a given time.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Fri, 06 Sep 2019 17:22:42 +0200 |
parents | 0f467a839fe2 |
children | 97312d7954ba |
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 // |
4305
0f467a839fe2
Moved stretches as shape file download to separate file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4304
diff
changeset
|
7 // Copyright (C) 2019 by via donau |
3245
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 |
4304
fee5794f3b7a
Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
28 "github.com/gorilla/mux" |
fee5794f3b7a
Return stretch as a ZIPed ESRI shape file from end point. TODO: Write the actual geometries to the shape file.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4296
diff
changeset
|
29 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
30 "gemma.intevation.de/gemma/pkg/middleware" |
3245
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 ) |
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
33 const ( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
34 selectSectionBottlenecks = ` |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
35 SELECT |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
36 distinct(b.objnam), |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
37 b.limiting |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
38 FROM waterway.sections s, waterway.bottlenecks b |
3666
db87f34805fb
Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents:
3645
diff
changeset
|
39 WHERE b.validity @> current_timestamp |
db87f34805fb
Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents:
3645
diff
changeset
|
40 AND ST_Intersects(b.area, s.area) |
db87f34805fb
Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents:
3645
diff
changeset
|
41 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
|
42 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
43 selectStretchBottlenecks = ` |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
44 SELECT |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
45 distinct(b.objnam), |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
46 b.limiting |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
47 FROM waterway.stretches s, waterway.bottlenecks b |
3666
db87f34805fb
Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents:
3645
diff
changeset
|
48 WHERE b.validity @> current_timestamp |
db87f34805fb
Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents:
3645
diff
changeset
|
49 AND ST_Intersects(b.area, s.area) |
db87f34805fb
Align bottleneck validity at gauges
Tom Gottfried <tom@intevation.de>
parents:
3645
diff
changeset
|
50 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
|
51 ) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
52 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
53 type ( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
54 stretchBottleneck struct { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
55 name string |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
56 limiting string |
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
59 stretchBottlenecks []stretchBottleneck |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
60 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
61 fullStretchBottleneck struct { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
62 *stretchBottleneck |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
63 measurements availMeasurements |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3420
diff
changeset
|
64 ldc []float64 |
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3420
diff
changeset
|
65 breaks []float64 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
66 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
|
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
70 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
|
71 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
|
72 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
|
73 return true |
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 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
76 return false |
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
79 func loadFullStretchBottleneck( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
80 ctx context.Context, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
81 conn *sql.Conn, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
82 bn *stretchBottleneck, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
83 los int, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
84 from, to time.Time, |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3420
diff
changeset
|
85 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
|
86 ) (*fullStretchBottleneck, error) { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
87 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
|
88 if err != nil { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
89 return nil, err |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
90 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
91 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
|
92 if err != nil { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
93 return nil, err |
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
96 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
|
97 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
|
98 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
99 switch bn.limiting { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
100 case "width": |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
101 access = (*availMeasurement).getWidth |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
102 breaks = widthbreaks |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
103 case "depth": |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
104 access = (*availMeasurement).getDepth |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
105 breaks = depthbreaks |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
106 default: |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
107 log.Printf( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
108 "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
|
109 bn.limiting) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
110 access = (*availMeasurement).getDepth |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
111 breaks = depthbreaks |
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
114 return &fullStretchBottleneck{ |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
115 stretchBottleneck: bn, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
116 measurements: measurements, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
117 ldc: ldc, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
118 breaks: breaks, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
119 access: access, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
120 }, nil |
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
123 func loadStretchBottlenecks( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
124 ctx context.Context, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
125 conn *sql.Conn, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
126 stretch bool, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
127 name string, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
128 ) (stretchBottlenecks, error) { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
129 var sql string |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
130 if stretch { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
131 sql = selectStretchBottlenecks |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
132 } else { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
133 sql = selectSectionBottlenecks |
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
136 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
|
137 if err != nil { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
138 return nil, err |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
139 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
140 defer rows.Close() |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
141 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
142 var bns stretchBottlenecks |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
143 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
144 for rows.Next() { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
145 var bn stretchBottleneck |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
146 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
|
147 &bn.name, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
148 &bn.limiting, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
149 ); err != nil { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
150 return nil, err |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
151 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
152 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
|
153 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
154 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
155 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
|
156 return nil, err |
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
159 return bns, nil |
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
162 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
|
163 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
164 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
|
165 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
|
166 name := vars["name"] |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
167 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
|
168 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
169 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
|
170 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
171 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
|
172 if !ok { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
173 return |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
174 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
175 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
176 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
|
177 if !ok { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
178 return |
3405
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
181 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
|
182 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
|
183 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
184 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
185 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
|
186 if !ok { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
187 return |
3405
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
190 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
|
191 ctx := req.Context() |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
192 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
193 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
|
194 if err != nil { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
195 http.Error( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
196 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
|
197 http.StatusInternalServerError) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
198 return |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
199 } |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
200 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
201 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
|
202 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
|
203 return |
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
206 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
|
207 depthbreaks = breaksToReferenceValue(b) |
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
210 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
|
211 widthbreaks = breaksToReferenceValue(b) |
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
214 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
|
215 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
216 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
|
217 http.Error( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
218 rw, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
219 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
|
220 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
|
221 http.StatusBadRequest, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
222 ) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
223 return |
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
226 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
|
227 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
228 var loaded []*fullStretchBottleneck |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
229 var errors []error |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
230 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
231 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
|
232 l, err := loadFullStretchBottleneck( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
233 ctx, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
234 conn, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
235 &bns[i], |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
236 los, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
237 from, to, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
238 depthbreaks, widthbreaks, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
239 ) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
240 if err != nil { |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
241 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
|
242 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
|
243 continue |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
244 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
245 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
|
246 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
247 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
248 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
|
249 http.Error( |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
250 rw, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
251 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
|
252 http.StatusInternalServerError, |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
253 ) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
254 return |
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
257 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
|
258 if n == 0 { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
259 n = 1 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
260 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
261 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
262 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
|
263 label string |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
264 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
|
265 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
|
266 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
|
267 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
|
268 } |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
269 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
270 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
|
271 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
272 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
|
273 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
274 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
|
275 wg.Add(1) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
276 go func() { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
277 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
|
278 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
|
279 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
280 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
|
281 |
4343
63c25eb9c07c
FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents:
4305
diff
changeset
|
282 now := time.Now() |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
283 for _, bn := range loaded { |
4343
63c25eb9c07c
FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents:
4305
diff
changeset
|
284 // Don't interpolate for the future |
63c25eb9c07c
FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents:
4305
diff
changeset
|
285 if now.Sub(res.to) < 0 { |
63c25eb9c07c
FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents:
4305
diff
changeset
|
286 res.to = now |
63c25eb9c07c
FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents:
4305
diff
changeset
|
287 } |
63c25eb9c07c
FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents:
4305
diff
changeset
|
288 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
289 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
|
290 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
|
291 bn.ldc, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
292 bn.access, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
293 ) |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
294 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
|
295 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
|
296 bn.breaks, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
297 bn.access, |
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 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
300 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
|
301 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
|
302 } else { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
303 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
|
304 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
|
305 } |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
306 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
|
307 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
|
308 } |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
309 } |
3405
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 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
312 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
|
313 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
|
314 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
315 }() |
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 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
318 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
|
319 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
320 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
|
321 |
3424
0a666ba899fa
fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3420
diff
changeset
|
322 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
|
323 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
324 if useDepth { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
325 breaks = depthbreaks |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
326 } else { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
327 breaks = widthbreaks |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
328 } |
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 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
|
331 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
332 res := &result{ |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
333 label: label, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
334 from: pfrom, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
335 to: pto, |
3405
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 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
|
338 jobCh <- res |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
339 } |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
340 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
341 close(jobCh) |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
342 wg.Wait() |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
343 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
344 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
|
345 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
346 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
|
347 |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
348 // 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
|
349 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
|
350 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
|
351 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
|
352 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
|
353 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
|
354 if useDepth && useWidth { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
355 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
|
356 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
|
357 } |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
358 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
|
359 } else { |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
360 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
|
361 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
|
362 } |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
363 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
|
364 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
365 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
366 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
367 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
|
368 // 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
|
369 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
|
370 return |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
371 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
372 |
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
|
373 // 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
|
374 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
|
375 |
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
|
376 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
|
377 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
|
378 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
|
379 } |
104c4f683c74
fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3467
diff
changeset
|
380 |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
381 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
|
382 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
|
383 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
|
384 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
|
385 } |
3405
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
386 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
387 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
|
388 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
|
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 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
|
392 // 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
|
393 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
|
394 return |
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 out.Flush() |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
399 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
|
400 // 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
|
401 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
|
402 } |
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 |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
405 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
|
406 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
|
407 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
|
408 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
|
409 b.WriteString(", ") |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
410 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
411 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
|
412 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
413 return b.String() |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
diff
changeset
|
414 } |
2b5c22f6bb1f
available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3245
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 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
|
417 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
418 vars := mux.Vars(req) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
419 stretch := vars["kind"] == "stretch" |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
420 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
|
421 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
|
422 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
423 if name == "" { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
424 http.Error( |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
425 rw, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
426 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
|
427 http.StatusBadRequest, |
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 return |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
430 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
431 |
3519
a6128caca3c1
stretches/sections: availability. fixed typo in 'from' parameter parsing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3473
diff
changeset
|
432 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
|
433 if !ok { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
434 return |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
435 } |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
436 |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
437 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
|
438 if !ok { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
439 return |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
440 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
441 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
442 if to.Before(from) { |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
443 to, from = from, to |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
444 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
445 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
446 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
|
447 if !ok { |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
448 return |
3430
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 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
451 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
|
452 |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
453 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
|
454 depthbreaks = breaksToReferenceValue(b) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
455 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
456 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
457 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
|
458 widthbreaks = breaksToReferenceValue(b) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
459 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
460 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
461 conn := middleware.GetDBConn(req) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
462 ctx := req.Context() |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
463 |
3439
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
464 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
|
465 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
|
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, 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
|
468 http.StatusInternalServerError) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
469 return |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
470 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
471 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
472 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
|
473 http.Error( |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
474 rw, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
475 "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
|
476 http.StatusNotFound, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
477 ) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
478 return |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
479 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
480 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
481 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
|
482 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
483 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
|
484 http.Error( |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
485 rw, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
486 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
|
487 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
|
488 http.StatusBadRequest, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
489 ) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
490 return |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
491 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
492 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
493 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
|
494 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
495 var loaded []*fullStretchBottleneck |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
496 var errors []error |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
497 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
498 for i := range bns { |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
499 l, err := loadFullStretchBottleneck( |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
500 ctx, |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
501 conn, |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
502 &bns[i], |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
503 los, |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
504 from, to, |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
505 depthbreaks, widthbreaks, |
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 if err != nil { |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
508 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
|
509 errors = append(errors, err) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
510 continue |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
511 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
512 loaded = append(loaded, l) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
513 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
514 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
515 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
|
516 http.Error( |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
517 rw, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
518 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
|
519 http.StatusInternalServerError, |
d7ddb21f7017
Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3430
diff
changeset
|
520 ) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
521 return |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
522 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
523 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
524 n := runtime.NumCPU() / 2 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
525 if n == 0 { |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
526 n = 1 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
527 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
528 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
529 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
|
530 label string |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
531 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
|
532 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
|
533 ldc []float64 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
534 breaks []float64 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
535 } |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
536 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
537 jobCh := make(chan *result) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
538 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
539 var wg sync.WaitGroup |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
540 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
541 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
|
542 wg.Add(1) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
543 go func() { |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
544 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
|
545 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
|
546 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
|
547 |
4343
63c25eb9c07c
FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents:
4305
diff
changeset
|
548 now := time.Now() |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
549 for _, bn := range loaded { |
4343
63c25eb9c07c
FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents:
4305
diff
changeset
|
550 // Don't interpolate for the future |
63c25eb9c07c
FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents:
4305
diff
changeset
|
551 if now.Sub(res.to) < 0 { |
63c25eb9c07c
FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents:
4305
diff
changeset
|
552 res.to = now |
63c25eb9c07c
FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents:
4305
diff
changeset
|
553 } |
63c25eb9c07c
FA: be optimistic about missing data.
Sascha Wilde <wilde@intevation.de>
parents:
4305
diff
changeset
|
554 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
555 l := bn.measurements.classify( |
3764
ad93adaeb688
Section/Stretch avail: Fixed classification interval calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3666
diff
changeset
|
556 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
|
557 bn.ldc, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
558 (*availMeasurement).getValue, |
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 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
561 b := bn.measurements.classify( |
3764
ad93adaeb688
Section/Stretch avail: Fixed classification interval calculation.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3666
diff
changeset
|
562 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
|
563 bn.breaks, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
564 bn.access, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
565 ) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
566 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
567 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
|
568 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
|
569 } else { |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
570 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
|
571 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
|
572 } |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
573 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
|
574 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
|
575 } |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
576 } |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
577 } |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
578 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
579 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
|
580 |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
581 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
|
582 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
|
583 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
584 }() |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
585 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
586 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
587 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
|
588 |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
589 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
|
590 |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
591 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
|
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 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
|
594 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
|
595 } else { |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
596 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
|
597 } |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
598 |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
599 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
|
600 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
601 res := &result{ |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
602 label: label, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
603 from: pfrom, |
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
604 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
|
605 } |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
606 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
|
607 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
608 jobCh <- res |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
609 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
610 |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
611 close(jobCh) |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
612 wg.Wait() |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
613 |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
614 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
|
615 |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
616 out := csv.NewWriter(rw) |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
617 |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
618 // 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
|
619 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
|
620 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
|
621 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
|
622 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
|
623 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
|
624 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
|
625 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
|
626 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
|
627 } |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
628 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
|
629 } else { |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
630 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
|
631 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
|
632 } |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
633 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
|
634 } |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
635 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
636 |
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 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
|
638 // 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
|
639 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
|
640 return |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
641 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
642 |
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
|
643 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
|
644 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
|
645 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
|
646 } |
104c4f683c74
fairway availabilty: prefill result mit zero values if there is no data.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3467
diff
changeset
|
647 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
648 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
|
649 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
|
650 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
651 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
|
652 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
|
653 } |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
654 |
3467
7265adcc5baa
Simplified code paths of fairway availibilty/depth for streches/sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3452
diff
changeset
|
655 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
|
656 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
|
657 } |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
658 |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
659 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
|
660 // 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
|
661 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
|
662 return |
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
663 } |
3430
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
664 } |
6994602d2935
fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3424
diff
changeset
|
665 |
3452
ae6c09fbc590
fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3439
diff
changeset
|
666 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
|
667 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
|
668 // 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
|
669 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
|
670 } |
3245
60f25cbe77fb
Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
671 } |