Mercurial > gemma
comparison pkg/controllers/fwa.go @ 5204:7ca9e6c9a203 new-fwa
Introduced type for mode (monthly, quarterly, yearly).
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 11 May 2020 11:03:19 +0200 |
parents | 355195a90298 |
children | c7907715309f |
comparison
equal
deleted
inserted
replaced
5203:355195a90298 | 5204:7ca9e6c9a203 |
---|---|
18 "database/sql" | 18 "database/sql" |
19 "fmt" | 19 "fmt" |
20 "log" | 20 "log" |
21 "net/http" | 21 "net/http" |
22 "sort" | 22 "sort" |
23 "strconv" | |
24 "strings" | |
23 "time" | 25 "time" |
24 | 26 |
25 "github.com/gorilla/mux" | 27 "github.com/gorilla/mux" |
26 | 28 |
27 "gemma.intevation.de/gemma/pkg/common" | 29 "gemma.intevation.de/gemma/pkg/common" |
136 bottleneck struct { | 138 bottleneck struct { |
137 id string | 139 id string |
138 validities limitingValidities | 140 validities limitingValidities |
139 measurements availMeasurements | 141 measurements availMeasurements |
140 } | 142 } |
143 | |
144 fwaMode int | |
141 ) | 145 ) |
146 | |
147 const ( | |
148 fwaMonthly fwaMode = iota | |
149 fwaQuarterly | |
150 fwaYearly | |
151 ) | |
152 | |
153 // afdRefs are the typical available fairway depth reference values. | |
154 var afdRefs = []float64{ | |
155 230, | |
156 250, | |
157 } | |
142 | 158 |
143 func (ls ldcs) find(from, to time.Time) *ldc { | 159 func (ls ldcs) find(from, to time.Time) *ldc { |
144 for _, l := range ls { | 160 for _, l := range ls { |
145 if l.intersects(from, to) { | 161 if l.intersects(from, to) { |
146 return l | 162 return l |
148 } | 164 } |
149 return nil | 165 return nil |
150 } | 166 } |
151 | 167 |
152 func fairwayAvailability(rw http.ResponseWriter, req *http.Request) { | 168 func fairwayAvailability(rw http.ResponseWriter, req *http.Request) { |
169 | |
170 mode := parseFWAMode(req.FormValue("mode")) | |
171 | |
172 // TODO: depending on mode do output stepping | |
173 | |
174 _ = mode | |
153 | 175 |
154 from, to, ok := parseFromTo(rw, req) | 176 from, to, ok := parseFromTo(rw, req) |
155 if !ok { | 177 if !ok { |
156 return | 178 return |
157 } | 179 } |
298 from, to = common.OrderTime(from, to) | 320 from, to = common.OrderTime(from, to) |
299 // Operate on daily basis so go to full days. | 321 // Operate on daily basis so go to full days. |
300 return dusk(from), dawn(to), true | 322 return dusk(from), dawn(to), true |
301 } | 323 } |
302 | 324 |
325 func parseFWAMode(mode string) fwaMode { | |
326 switch strings.ToLower(mode) { | |
327 case "monthly": | |
328 return fwaMonthly | |
329 case "quarterly": | |
330 return fwaQuarterly | |
331 case "yearly": | |
332 return fwaYearly | |
333 default: | |
334 return fwaMonthly | |
335 } | |
336 } | |
337 | |
338 func breaksToReferenceValue(breaks string) []float64 { | |
339 parts := strings.Split(breaks, ",") | |
340 var values []float64 | |
341 | |
342 for _, part := range parts { | |
343 part = strings.TrimSpace(part) | |
344 if v, err := strconv.ParseFloat(part, 64); err == nil { | |
345 values = append(values, v) | |
346 } | |
347 } | |
348 | |
349 return common.DedupFloat64s(values) | |
350 } | |
351 | |
352 func parseBreaks(breaks string) []float64 { | |
353 if breaks != "" { | |
354 return breaksToReferenceValue(breaks) | |
355 } | |
356 return afdRefs | |
357 } | |
358 | |
303 func (tr *timeRange) intersects(from, to time.Time) bool { | 359 func (tr *timeRange) intersects(from, to time.Time) bool { |
304 return !(to.Before(tr.lower) || from.After(tr.upper)) | 360 return !(to.Before(tr.lower) || from.After(tr.upper)) |
305 } | 361 } |
306 | 362 |
307 func (tr *timeRange) toUTC() { | 363 func (tr *timeRange) toUTC() { |