Mercurial > gemma
annotate pkg/controllers/bottlenecks.go @ 3160:94935895e6d7
client: add diagram-element to template (waterlevel)
* add the diagram as an element to template and add the ability to definewidth, height and position of the exported diagram to pdf
author | Fadi Abbud <fadi.abbud@intevation.de> |
---|---|
date | Mon, 06 May 2019 13:05:49 +0200 |
parents | 7b4092b6b51a |
children | d5294f1a4ad4 |
rev | line source |
---|---|
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 // |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 // |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 // Copyright (C) 2019 by via donau |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 // |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de> |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 package controllers |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 import ( |
3116
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
17 "context" |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 "database/sql" |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
19 "encoding/csv" |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 "fmt" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
21 "log" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
22 "net/http" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
23 "sort" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
24 "strconv" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
25 "strings" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 "time" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 |
3117
6b5132fd385e
Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3116
diff
changeset
|
28 "github.com/gorilla/mux" |
6b5132fd385e
Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3116
diff
changeset
|
29 |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 "gemma.intevation.de/gemma/pkg/common" |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
31 "gemma.intevation.de/gemma/pkg/middleware" |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 ) |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 const ( |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 selectAvailableDepthSQL = ` |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 WITH data AS ( |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 SELECT |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 efa.measure_date, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 efa.available_depth_value, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 efa.water_level_value |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 FROM waterway.effective_fairway_availability efa |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 JOIN waterway.fairway_availability fa |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 ON efa.fairway_availability_id = fa.id |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 JOIN waterway.bottlenecks bn |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 ON fa.bottleneck_id = bn.id |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 WHERE |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 bn.objnam = $1 AND |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 efa.level_of_service = $2 AND |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 efa.measure_type = 'Measured' AND |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 efa.available_depth_value IS NOT NULL AND |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 efa.water_level_value IS NOT NULL |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 ), |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 before AS ( |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 SELECT * FROM data WHERE measure_date < $3 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 ORDER BY measure_date DESC LIMIT 1 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 ), |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 inside AS ( |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 SELECT * FROM data WHERE measure_date BETWEEN $3 AND $4 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
59 ), |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
60 after AS ( |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
61 SELECT * FROM data WHERE measure_date > $4 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 ORDER BY measure_date LIMIT 1 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 ) |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 SELECT * FROM before |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 UNION ALL |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 SELECT * FROM inside |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 UNION ALL |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 SELECT * FROM after |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 ORDER BY measure_date |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 ` |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
71 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
72 selectGaugeLevelsSQL = ` |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
73 SELECT |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
74 grwl.depth_reference, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 grwl.value |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 FROM waterway.gauges_reference_water_levels grwl JOIN |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 waterway.bottlenecks bns |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
78 ON bns.fk_g_fid = grwl.gauge_id |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
79 WHERE bns.objnam = $1 AND ( |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
80 grwl.depth_reference like 'HDC%' OR |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 grwl.depth_reference like 'LDC%' OR |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 grwl.depth_reference like 'MW%' |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 ) |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
84 ` |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
85 selectGaugeLDCSQL = ` |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
86 SELECT |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
87 grwl.value |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
88 FROM waterway.gauges_reference_water_levels grwl JOIN |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
89 waterway.bottlenecks bns |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
90 ON bns.fk_g_fid = grwl.gauge_id |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
91 WHERE bns.objnam = $1 AND grwl.depth_reference like 'LDC%' |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
92 ` |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 ) |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
94 |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
95 type ( |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
96 referenceValue struct { |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
97 level int |
3040
a661e9b8f3b6
Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3038
diff
changeset
|
98 value float64 |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
99 } |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
100 |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
101 availMeasurement struct { |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
102 when time.Time |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
103 depth int |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
104 value int |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
105 } |
3114
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
106 |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
107 availMeasurements []availMeasurement |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
108 ) |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
109 |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
110 // afdRefs are the typical available fairway depth reference values. |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
111 var afdRefs = []referenceValue{ |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
112 {0, 200}, |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
113 {1, 230}, |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
114 {2, 250}, |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
115 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
116 |
3114
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
117 func (measurement *availMeasurement) getDepth() float64 { |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
118 return float64(measurement.depth) |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
119 } |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
120 |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
121 func (measurement *availMeasurement) getValue() float64 { |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
122 return float64(measurement.value) |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
123 } |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
124 |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
125 func (measurements availMeasurements) classifyAvailMeasurements( |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
126 from, to time.Time, |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
127 classes []referenceValue, |
3040
a661e9b8f3b6
Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3038
diff
changeset
|
128 access func(*availMeasurement) float64, |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
129 ) []time.Duration { |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
130 |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
131 type classValues struct { |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
132 when time.Time |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
133 kind common.ValueRangeKind |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
134 } |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
135 |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
136 //var invalid time.Duration |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
137 result := make([]time.Duration, len(classes)+1) |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
138 |
3114
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
139 if len(measurements) == 0 || |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
140 to.Before(measurements[0].when) || |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
141 from.After(measurements[len(measurements)-1].when) { |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
142 return result |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
143 } |
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
144 |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
145 cvs := make([]classValues, len(classes)) |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
146 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
147 classify := func(v func(float64) (time.Time, common.ValueRangeKind)) { |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
148 for i := range classes { |
3040
a661e9b8f3b6
Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3038
diff
changeset
|
149 cvs[i].when, cvs[i].kind = v(classes[i].value) |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
150 } |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
151 } |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
152 |
3036
7f12a87c56ff
Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3027
diff
changeset
|
153 vbt := func(p1, p2 *availMeasurement) func(time.Time) (float64, common.ValueRangeKind) { |
3040
a661e9b8f3b6
Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3038
diff
changeset
|
154 return common.InterpolateValueByTime(p1.when, access(p1), p2.when, access(p2)) |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
155 } |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
156 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
157 pairs: |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
158 for i := 0; i < len(measurements)-1; i++ { |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
159 p1 := &measurements[i] |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
160 p2 := &measurements[i+1] |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
161 |
3036
7f12a87c56ff
Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3027
diff
changeset
|
162 var start, end time.Time |
7f12a87c56ff
Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3027
diff
changeset
|
163 |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
164 switch { |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
165 case !p2.when.After(p2.when): |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
166 // Segment invalid |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
167 continue pairs |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
168 |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
169 case p1.when.After(to) || p2.when.Before(from): |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
170 // Segment complete outside. |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
171 continue pairs |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
172 |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
173 case p1.when.After(from) && p2.when.Before(to): |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
174 // (from-to) is complete inside segment. |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
175 // invalid += p1.when.Sub(from) |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
176 // invalid += to.Sub(p2.when) |
3036
7f12a87c56ff
Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3027
diff
changeset
|
177 v := vbt(p1, p2) |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
178 f, _ := v(from) |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
179 t, _ := v(to) |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
180 classify(common.InterpolateTimeByValue(from, f, to, t)) |
3036
7f12a87c56ff
Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3027
diff
changeset
|
181 start, end = from, to |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
182 |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
183 case p1.when.After(from): |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
184 // from is inside segment |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
185 // invalid += p1.when.Sub(from) |
3036
7f12a87c56ff
Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3027
diff
changeset
|
186 f, _ := vbt(p1, p2)(from) |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
187 classify(common.InterpolateTimeByValue( |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
188 from, f, |
3040
a661e9b8f3b6
Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3038
diff
changeset
|
189 p2.when, access(p2), |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
190 )) |
3036
7f12a87c56ff
Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3027
diff
changeset
|
191 start, end = from, p2.when |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
192 |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
193 case p2.when.Before(to): |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
194 // to is inside segment |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
195 // invalid += to.Sub(p2.when) |
3036
7f12a87c56ff
Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3027
diff
changeset
|
196 t, _ := vbt(p1, p2)(to) |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
197 classify(common.InterpolateTimeByValue( |
3040
a661e9b8f3b6
Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3038
diff
changeset
|
198 p1.when, access(p1), |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
199 to, t, |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
200 )) |
3036
7f12a87c56ff
Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3027
diff
changeset
|
201 start, end = p1.when, to |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
202 |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
203 case !p1.when.Before(from) && !to.After(p2.when): |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
204 // Segment complete inside. |
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
205 classify(common.InterpolateTimeByValue( |
3040
a661e9b8f3b6
Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3038
diff
changeset
|
206 p1.when, access(p1), |
a661e9b8f3b6
Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3038
diff
changeset
|
207 p2.when, access(p2), |
3027
84e6577a474b
Fairway availability: More robust time and value interpolations including corner cases. Still TODO: Distribute to classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2967
diff
changeset
|
208 )) |
3036
7f12a87c56ff
Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3027
diff
changeset
|
209 start, end = p1.when, p2.when |
7f12a87c56ff
Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3027
diff
changeset
|
210 default: |
7f12a87c56ff
Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3027
diff
changeset
|
211 log.Println("warn: unexpected case. That should not happen.") |
7f12a87c56ff
Fairway availability: Set boundaries of segments correctly.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3027
diff
changeset
|
212 continue pairs |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
213 } |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
214 |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
215 for i := len(cvs) - 1; i >= 0; i-- { |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
216 switch cvs[i].kind { |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
217 case common.ValueAbove: |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
218 result[i+1] += end.Sub(start) |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
219 continue pairs |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
220 |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
221 case common.ValueInside: |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
222 // -> split |
3040
a661e9b8f3b6
Fairway availability: 'virtualized' the field to classify.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3038
diff
changeset
|
223 if access(p1) < classes[i].value { |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
224 // started below -> second part above |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
225 result[i+1] = end.Sub(cvs[i].when) |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
226 end = cvs[i].when |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
227 } else { |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
228 // started above -> first part above |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
229 result[i+1] = cvs[i].when.Sub(start) |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
230 start = cvs[i].when |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
231 } |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
232 } |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
233 } |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
234 result[0] += end.Sub(start) |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
235 } |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
236 |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
237 return result |
2967
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
238 } |
7c301ff449bc
Fairway availibility: Started with class building.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
2962
diff
changeset
|
239 |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
240 func durationsToPercentage(from, to time.Time, classes []time.Duration) []float64 { |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
241 percents := make([]float64, len(classes)) |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
242 total := 100 / to.Sub(from).Seconds() |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
243 for i, v := range classes { |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
244 percents[i] = v.Seconds() * total |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
245 } |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
246 return percents |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
247 } |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
248 |
3115
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
249 func parseTime(s, what string) (time.Time, error) { |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
250 var t time.Time |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
251 var err error |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
252 if t, err = time.Parse(common.TimeFormat, s); err != nil { |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
253 return time.Time{}, JSONError{ |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
254 Code: http.StatusBadRequest, |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
255 Message: fmt.Sprintf( |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
256 "Invalid time format for '%s' field: %v", what, err), |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
257 } |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
258 } |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
259 return t.UTC(), nil |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
260 } |
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
261 |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
262 func parseInt(s, what string) (int, error) { |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
263 i, err := strconv.Atoi(s) |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
264 if err != nil { |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
265 return 0, JSONError{ |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
266 Code: http.StatusBadRequest, |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
267 Message: fmt.Sprintf( |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
268 "Invalid value for field '%s': %v", what, err), |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
269 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
270 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
271 return i, nil |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
272 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
273 |
3116
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
274 func loadDepthValues( |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
275 ctx context.Context, |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
276 conn *sql.Conn, |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
277 bottleneck string, |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
278 los int, |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
279 from, to time.Time, |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
280 ) (availMeasurements, error) { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
281 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
282 rows, err := conn.QueryContext( |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
283 ctx, selectAvailableDepthSQL, bottleneck, los, from, to) |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
284 if err != nil { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
285 return nil, err |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
286 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
287 defer rows.Close() |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
288 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
289 var ms availMeasurements |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
290 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
291 for rows.Next() { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
292 var m availMeasurement |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
293 if err := rows.Scan(&m.when, &m.depth, &m.value); err != nil { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
294 return nil, err |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
295 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
296 m.when = m.when.UTC() |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
297 ms = append(ms, m) |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
298 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
299 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
300 if err := rows.Err(); err != nil { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
301 return nil, err |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
302 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
303 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
304 return ms, nil |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
305 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
306 |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
307 func loadLDCReferenceValue( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
308 ctx context.Context, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
309 conn *sql.Conn, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
310 bottleneck string, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
311 ) ([]referenceValue, error) { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
312 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
313 var value float64 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
314 err := conn.QueryRowContext(ctx, selectGaugeLDCSQL, bottleneck).Scan(&value) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
315 switch { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
316 case err == sql.ErrNoRows: |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
317 return nil, nil |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
318 case err != nil: |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
319 return nil, err |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
320 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
321 return []referenceValue{{0, value}}, nil |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
322 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
323 |
3116
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
324 func loadLNWLReferenceValues( |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
325 ctx context.Context, |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
326 conn *sql.Conn, |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
327 bottleneck string, |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
328 ) ([]referenceValue, error) { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
329 rows, err := conn.QueryContext(ctx, selectGaugeLevelsSQL, bottleneck) |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
330 if err != nil { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
331 return nil, err |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
332 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
333 defer rows.Close() |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
334 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
335 var levels []referenceValue |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
336 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
337 loop: |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
338 for rows.Next() { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
339 var what string |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
340 var value int |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
341 if err := rows.Scan(&what, &value); err != nil { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
342 return nil, err |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
343 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
344 var level int |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
345 switch { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
346 case strings.HasPrefix(what, "LDC"): |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
347 level = 0 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
348 case strings.HasPrefix(what, "MW"): |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
349 level = 1 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
350 case strings.HasPrefix(what, "HDC"): |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
351 level = 2 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
352 default: |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
353 return nil, fmt.Errorf("Unexpected reference level type '%s'", what) |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
354 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
355 for i := range levels { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
356 if levels[i].level == level { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
357 levels[i].value = float64(value) |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
358 continue loop |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
359 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
360 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
361 levels = append(levels, referenceValue{ |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
362 level: level, |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
363 value: float64(value), |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
364 }) |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
365 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
366 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
367 if err := rows.Err(); err != nil { |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
368 return nil, err |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
369 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
370 |
3117
6b5132fd385e
Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3116
diff
changeset
|
371 sort.Slice(levels, func(i, j int) bool { |
6b5132fd385e
Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3116
diff
changeset
|
372 return levels[i].level < levels[j].level |
6b5132fd385e
Cosmetics.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3116
diff
changeset
|
373 }) |
3116
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
374 |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
375 return levels, nil |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
376 } |
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
377 |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
378 func bottleneckAvailabilty( |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
379 _ interface{}, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
380 req *http.Request, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
381 conn *sql.Conn, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
382 ) (jr JSONResult, err error) { |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
383 bn := mux.Vars(req)["objnam"] |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
384 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
385 if bn == "" { |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
386 err = JSONError{ |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
387 Code: http.StatusBadRequest, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
388 Message: "Missing objnam of bottleneck", |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
389 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
390 return |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
391 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
392 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
393 var from, to time.Time |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
394 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
395 if f := req.FormValue("from"); f != "" { |
3115
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
396 if from, err = parseTime(f, "from"); err != nil { |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
397 return |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
398 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
399 } else { |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
400 from = time.Now().AddDate(-1, 0, 0).UTC() |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
401 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
402 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
403 if t := req.FormValue("to"); t != "" { |
3115
524bc6545b20
Simplified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3114
diff
changeset
|
404 if to, err = parseTime(t, "to"); err != nil { |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
405 return |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
406 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
407 } else { |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
408 to = from.AddDate(1, 0, 0).UTC() |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
409 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
410 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
411 if to.Before(from) { |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
412 to, from = from, to |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
413 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
414 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
415 log.Printf("info: time interval: (%v - %v)\n", from, to) |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
416 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
417 var los int |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
418 if l := req.FormValue("los"); l != "" { |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
419 if los, err = parseInt(l, "los"); err != nil { |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
420 return |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
421 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
422 } else { |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
423 los = 1 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
424 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
425 |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
426 ctx := req.Context() |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
427 |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
428 var lnwlRefs []referenceValue |
3116
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
429 if lnwlRefs, err = loadLNWLReferenceValues(ctx, conn, bn); err != nil { |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
430 return |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
431 } |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
432 |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
433 if len(lnwlRefs) == 0 { |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
434 err = JSONError{ |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
435 Code: http.StatusNotFound, |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
436 Message: "No gauge reference values found for bottleneck", |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
437 } |
3116
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
438 return |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
439 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
440 |
3114
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
441 var ms availMeasurements |
3116
ff0b9a94e0e4
Simpified code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3115
diff
changeset
|
442 if ms, err = loadDepthValues(ctx, conn, bn, los, from, to); err != nil { |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
443 return |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
444 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
445 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
446 if len(ms) == 0 { |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
447 err = JSONError{ |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
448 Code: http.StatusNotFound, |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
449 Message: "No available fairway depth values found", |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
450 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
451 return |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
452 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
453 |
3114
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
454 lnwl := ms.classifyAvailMeasurements( |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
455 from, to, |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
456 lnwlRefs, |
3114
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
457 (*availMeasurement).getValue, |
3038
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
458 ) |
d47d289d6e68
Fairway availability: Fill classes.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3036
diff
changeset
|
459 |
3114
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
460 afd := ms.classifyAvailMeasurements( |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
461 from, to, |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
462 afdRefs, |
3114
c68cef0346b7
Display Available Fairway Depths: Introduced type to re-use code from Available Fairway Depths vs. LNWL.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3076
diff
changeset
|
463 (*availMeasurement).getDepth, |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
464 ) |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
465 |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
466 lnwlPercents := durationsToPercentage(from, to, lnwl) |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
467 afdPercents := durationsToPercentage(from, to, afd) |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
468 |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
469 type lnwlOutput struct { |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
470 Level string `json:"level"` |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
471 Value float64 `json:"value"` |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
472 Percent float64 `json:"percent"` |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
473 } |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
474 |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
475 type afdOutput struct { |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
476 Value float64 `json:"value"` |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
477 Percent float64 `json:"percent"` |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
478 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
479 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
480 type output struct { |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
481 LNWL []lnwlOutput `json:"lnwl"` |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
482 AFD []afdOutput `json:"afd"` |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
483 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
484 |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
485 out := output{} |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
486 |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
487 for i := range lnwlRefs { |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
488 var level string |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
489 switch lnwlRefs[i].level { |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
490 case 0: |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
491 level = "LDC" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
492 case 1: |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
493 level = "MW" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
494 case 2: |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
495 level = "HDC" |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
496 } |
3076
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
497 out.LNWL = append(out.LNWL, lnwlOutput{ |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
498 Level: level, |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
499 Value: lnwlRefs[i].value, |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
500 Percent: lnwlPercents[i], |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
501 }) |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
502 } |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
503 |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
504 for i := range afdRefs { |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
505 out.AFD = append(out.AFD, afdOutput{ |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
506 Value: afdRefs[i].value, |
e0daeb05bf50
Display Available Fairway Depths vs. LNWL: Completed backend (untested).
Sascha L. Teichmann <teichmann@intevation.de>
parents:
3040
diff
changeset
|
507 Percent: afdPercents[i], |
2962
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
508 }) |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
509 } |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
510 |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
511 jr = JSONResult{Result: &out} |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
512 return |
8d825551bc72
Fairway availibity for bottlenecks: Started with fetchting the relevant data. TODO: Range calculations. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
513 } |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
514 |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
515 func bottleneckAvailableFairwayDepth(rw http.ResponseWriter, req *http.Request) { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
516 |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
517 bn := mux.Vars(req)["objnam"] |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
518 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
519 if bn == "" { |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
520 http.Error( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
521 rw, "Missing objnam of bottleneck", |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
522 http.StatusBadRequest) |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
523 return |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
524 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
525 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
526 var mode int |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
527 if m := req.FormValue("mode"); m != "" { |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
528 switch strings.ToLower(m) { |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
529 case "monthly": |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
530 mode = 0 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
531 case "quarterly": |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
532 mode = 1 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
533 case "yearly": |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
534 mode = 2 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
535 default: |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
536 http.Error( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
537 rw, fmt.Sprintf("Unknown 'mode' value %s.", m), |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
538 http.StatusBadRequest) |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
539 return |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
540 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
541 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
542 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
543 var from, to time.Time |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
544 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
545 if f := req.FormValue("from"); f != "" { |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
546 var err error |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
547 if from, err = time.Parse(common.TimeFormat, f); err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
548 http.Error( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
549 rw, fmt.Sprintf("Invalid format for 'from': %v.", err), |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
550 http.StatusBadRequest) |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
551 return |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
552 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
553 } else { |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
554 from = time.Now().AddDate(-1, 0, 0).UTC() |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
555 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
556 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
557 if t := req.FormValue("to"); t != "" { |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
558 var err error |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
559 if to, err = time.Parse(common.TimeFormat, t); err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
560 http.Error( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
561 rw, fmt.Sprintf("Invalid format for 'to': %v.", err), |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
562 http.StatusBadRequest) |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
563 return |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
564 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
565 } else { |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
566 to = from.AddDate(1, 0, 0).UTC() |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
567 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
568 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
569 if to.Before(from) { |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
570 to, from = from, to |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
571 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
572 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
573 log.Printf("info: time interval: (%v - %v)\n", from, to) |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
574 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
575 var los int |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
576 if l := req.FormValue("los"); l != "" { |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
577 var err error |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
578 if los, err = strconv.Atoi(l); err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
579 http.Error( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
580 rw, fmt.Sprintf("Invalid format for 'los': %v.", err), |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
581 http.StatusBadRequest) |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
582 return |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
583 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
584 } else { |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
585 los = 1 |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
586 } |
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
587 |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
588 conn := middleware.GetDBConn(req) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
589 ctx := req.Context() |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
590 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
591 // load the measurements |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
592 ms, err := loadDepthValues(ctx, conn, bn, los, from, to) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
593 if err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
594 http.Error( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
595 rw, fmt.Sprintf("Loading measurements failed: %v.", err), |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
596 http.StatusInternalServerError) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
597 return |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
598 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
599 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
600 ldcRefs, err := loadLDCReferenceValue(ctx, conn, bn) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
601 if err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
602 http.Error( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
603 rw, fmt.Sprintf("Loading LDC failed: %v.", err), |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
604 http.StatusInternalServerError) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
605 return |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
606 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
607 if len(ldcRefs) == 0 { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
608 http.Error(rw, "No LDC found", http.StatusNotFound) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
609 return |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
610 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
611 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
612 rw.Header().Add("Content-Type", "text/csv") |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
613 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
614 out := csv.NewWriter(rw) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
615 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
616 // label, classes, lnwl |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
617 record := make([]string, 1+1+len(afdRefs)+1) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
618 record[0] = "#label" |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
619 record[1] = "# >= LDC [h]" |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
620 for i, v := range afdRefs { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
621 if i == 0 { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
622 record[2] = fmt.Sprintf("# < %.2f [h]", v.value) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
623 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
624 record[i+3] = fmt.Sprintf("# >= %.2f [h]", v.value) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
625 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
626 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
627 if err := out.Write(record); err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
628 // Too late for HTTP status message. |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
629 log.Printf("error: %v\n", err) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
630 return |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
631 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
632 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
633 interval := intervals[mode](from, to) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
634 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
635 for pfrom, pto, label := interval(); label != ""; pfrom, pto, label = interval() { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
636 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
637 // Find good starting point |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
638 idx := sort.Search(len(ms), func(i int) bool { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
639 return !ms[i].when.After(pfrom) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
640 }) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
641 if idx > 0 { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
642 idx-- |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
643 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
644 samples := ms[idx:] |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
645 |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
646 ranges := samples.classifyAvailMeasurements( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
647 pfrom, pto, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
648 afdRefs, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
649 (*availMeasurement).getDepth, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
650 ) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
651 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
652 ldc := samples.classifyAvailMeasurements( |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
653 pfrom, pto, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
654 ldcRefs, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
655 (*availMeasurement).getDepth, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
656 ) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
657 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
658 record[0] = label |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
659 record[1] = fmt.Sprintf("%.3f", ldc[1].Hours()/24) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
660 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
661 for i, d := range ranges { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
662 record[2+i] = fmt.Sprintf("%.3f", d.Hours()/24) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
663 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
664 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
665 if err := out.Write(record); err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
666 // Too late for HTTP status message. |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
667 log.Printf("error: %v\n", err) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
668 return |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
669 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
670 } |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
671 |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
672 out.Flush() |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
673 if err := out.Error(); err != nil { |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
674 // Too late for HTTP status message. |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
675 log.Printf("error: %v\n", err) |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
676 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
677 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
678 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
679 var intervals = []func(time.Time, time.Time) func() (time.Time, time.Time, string){ |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
680 monthly, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
681 quarterly, |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
682 yearly, |
3118
4dcbf9e9013c
Added stub for GET /api/data/fairway-depth/{gauge}
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3117
diff
changeset
|
683 } |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
684 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
685 func monthly(from, to time.Time) func() (time.Time, time.Time, string) { |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
686 pfrom := from |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
687 return func() (time.Time, time.Time, string) { |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
688 if pfrom.After(to) { |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
689 return time.Time{}, time.Time{}, "" |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
690 } |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
691 f := pfrom |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
692 pfrom = pfrom.AddDate(0, 1, 0) |
3133
7b4092b6b51a
Fairway availability depth: Fixed SQL statement.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3122
diff
changeset
|
693 label := fmt.Sprintf("%02d-%d", f.Month(), f.Year()) |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
694 return f, f.AddDate(0, 1, 0).Add(-time.Nanosecond), label |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
695 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
696 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
697 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
698 func quarterly(from, to time.Time) func() (time.Time, time.Time, string) { |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
699 pfrom := from |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
700 return func() (time.Time, time.Time, string) { |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
701 if pfrom.After(to) { |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
702 return time.Time{}, time.Time{}, "" |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
703 } |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
704 f := pfrom |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
705 pfrom = pfrom.AddDate(0, 3, 0) |
3133
7b4092b6b51a
Fairway availability depth: Fixed SQL statement.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3122
diff
changeset
|
706 label := fmt.Sprintf("Q%d-%d", int(f.Month())/4+1, f.Year()) |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
707 return f, f.AddDate(0, 3, 0).Add(-time.Nanosecond), label |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
708 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
709 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
710 |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
711 func yearly(from, to time.Time) func() (time.Time, time.Time, string) { |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
712 pfrom := from |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
713 return func() (time.Time, time.Time, string) { |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
714 if pfrom.After(to) { |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
715 return time.Time{}, time.Time{}, "" |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
716 } |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
717 f := pfrom |
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
718 pfrom = pfrom.AddDate(1, 0, 0) |
3133
7b4092b6b51a
Fairway availability depth: Fixed SQL statement.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3122
diff
changeset
|
719 label := fmt.Sprintf("%d", f.Year()) |
3122
3dffeb6df4a3
Display Available Fairway Depths: Implemented monthly, quarterly and yearly stepping through the measurements.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3119
diff
changeset
|
720 return f, f.AddDate(1, 0, 0).Add(-time.Nanosecond), label |
3119
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
721 } |
ad5a00ccd276
Display Available Fairway Depths: More controller code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
3118
diff
changeset
|
722 } |