Mercurial > gemma
comparison pkg/controllers/fwa.go @ 5251:1fce0fd81f46 new-fwa
Code simplification.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 13 May 2020 09:13:42 +0200 |
parents | 13e1767b63a1 |
children | aebe1a12e8b9 |
comparison
equal
deleted
inserted
replaced
5250:13e1767b63a1 | 5251:1fce0fd81f46 |
---|---|
200 } | 200 } |
201 | 201 |
202 ctx := req.Context() | 202 ctx := req.Context() |
203 conn := middleware.GetDBConn(req) | 203 conn := middleware.GetDBConn(req) |
204 | 204 |
205 // Function to extract the bottleneck_id's from the query. | 205 var bns bottlenecks |
206 var extract func(context.Context, *sql.Conn, string, time.Time, time.Time) (bottlenecks, error) | 206 var err error |
207 | 207 |
208 switch vars["kind"] { | 208 switch vars["kind"] { |
209 case "bottleneck": | 209 case "bottleneck": |
210 extract = extractBottleneck | 210 bns = bottlenecks{{id: name}} |
211 case "stretch": | 211 case "stretch": |
212 extract = extractStretch | 212 bns, err = loadSymbolBottlenecks(ctx, conn, "users.stretches", name, from, to) |
213 case "section": | 213 case "section": |
214 extract = extractSection | 214 bns, err = loadSymbolBottlenecks(ctx, conn, "waterway.sections", name, from, to) |
215 default: | 215 default: |
216 http.Error(rw, "Invalid kind type.", http.StatusBadRequest) | 216 http.Error(rw, "Invalid kind type.", http.StatusBadRequest) |
217 return | 217 return |
218 } | 218 } |
219 | 219 |
220 bottlenecks, err := extract(ctx, conn, name, from, to) | |
221 if err != nil { | 220 if err != nil { |
222 log.Printf("error: %v\n", err) | 221 log.Printf("error: %v\n", err) |
223 http.Error(rw, "cannot extract bottlenecks", http.StatusBadRequest) | 222 http.Error(rw, "cannot extract bottlenecks", http.StatusBadRequest) |
224 return | 223 return |
225 } | 224 } |
226 | 225 |
227 // If there are no bottlenecks there is nothing to do. | 226 // If there are no bottlenecks there is nothing to do. |
228 if len(bottlenecks) == 0 { | 227 if len(bns) == 0 { |
229 http.Error(rw, "No bottlenecks found.", http.StatusNotFound) | 228 http.Error(rw, "No bottlenecks found.", http.StatusNotFound) |
230 return | 229 return |
231 } | 230 } |
232 | 231 |
233 // load validities and limiting factors | 232 // load validities and limiting factors |
234 for i := range bottlenecks { | 233 for i := range bns { |
235 if err := bottlenecks[i].loadLimitingValidities(ctx, conn, from, to); err != nil { | 234 if err := bns[i].loadLimitingValidities(ctx, conn, from, to); err != nil { |
236 log.Printf("error: %v\n", err) | 235 log.Printf("error: %v\n", err) |
237 http.Error(rw, "cannot load validities", http.StatusInternalServerError) | 236 http.Error(rw, "cannot load validities", http.StatusInternalServerError) |
238 return | 237 return |
239 } | 238 } |
240 // load LCDs | 239 // load LCDs |
241 if err := bottlenecks[i].loadLDCs(ctx, conn, from, to); err != nil { | 240 if err := bns[i].loadLDCs(ctx, conn, from, to); err != nil { |
242 log.Printf("error: %v\n", err) | 241 log.Printf("error: %v\n", err) |
243 http.Error(rw, "cannot load LDCs", http.StatusInternalServerError) | 242 http.Error(rw, "cannot load LDCs", http.StatusInternalServerError) |
244 return | 243 return |
245 } | 244 } |
246 // load values | 245 // load values |
247 if err := bottlenecks[i].loadValues(ctx, conn, from, to, los); err != nil { | 246 if err := bns[i].loadValues(ctx, conn, from, to, los); err != nil { |
248 log.Printf("error: %v\n", err) | 247 log.Printf("error: %v\n", err) |
249 http.Error(rw, "cannot load values", http.StatusInternalServerError) | 248 http.Error(rw, "cannot load values", http.StatusInternalServerError) |
250 return | 249 return |
251 } | 250 } |
252 } | 251 } |
259 chooseBreaks = [...][]float64{ | 258 chooseBreaks = [...][]float64{ |
260 limitingDepth: depthBreaks, | 259 limitingDepth: depthBreaks, |
261 limitingWidth: widthBreaks, | 260 limitingWidth: widthBreaks, |
262 } | 261 } |
263 | 262 |
264 useDepth = bottlenecks.hasLimiting(limitingDepth, from, to) | 263 useDepth = bns.hasLimiting(limitingDepth, from, to) |
265 useWidth = bottlenecks.hasLimiting(limitingWidth, from, to) | 264 useWidth = bns.hasLimiting(limitingWidth, from, to) |
266 ) | 265 ) |
267 | 266 |
268 if useDepth && useWidth && len(widthBreaks) != len(depthBreaks) { | 267 if useDepth && useWidth && len(widthBreaks) != len(depthBreaks) { |
269 http.Error( | 268 http.Error( |
270 rw, | 269 rw, |
299 for i := range record[1:] { | 298 for i := range record[1:] { |
300 record[i+1] = "0" | 299 record[i+1] = "0" |
301 } | 300 } |
302 | 301 |
303 // For every day on every bottleneck we need to find out if this day is valid. | 302 // For every day on every bottleneck we need to find out if this day is valid. |
304 validities := make([]func(time.Time, time.Time) *limitingValidity, len(bottlenecks)) | 303 validities := make([]func(time.Time, time.Time) *limitingValidity, len(bns)) |
305 for i := range bottlenecks { | 304 for i := range bns { |
306 validities[i] = bottlenecks[i].validities.find() | 305 validities[i] = bns[i].validities.find() |
307 } | 306 } |
308 | 307 |
309 // Mode reflects if we use monthly, quarterly od yearly intervals. | 308 // Mode reflects if we use monthly, quarterly od yearly intervals. |
310 mode := parseFWAMode(req.FormValue("mode")) | 309 mode := parseFWAMode(req.FormValue("mode")) |
311 | 310 |
369 } | 368 } |
370 | 369 |
371 hasValid = true | 370 hasValid = true |
372 | 371 |
373 if overLDC { // If its already not shipable we need no further tests. | 372 if overLDC { // If its already not shipable we need no further tests. |
374 result := bottlenecks[i].measurements.classify( | 373 result := bns[i].measurements.classify( |
375 current, next, | 374 current, next, |
376 ldc.value, | 375 ldc.value, |
377 (*availMeasurement).getValue) | 376 (*availMeasurement).getValue) |
378 | 377 |
379 if result[1] < 12*time.Hour { | 378 if result[1] < 12*time.Hour { |
380 overLDC = false | 379 overLDC = false |
381 } | 380 } |
382 } | 381 } |
383 | 382 |
384 if min := minClass(bottlenecks[i].measurements.classify( | 383 if min := minClass(bns[i].measurements.classify( |
385 current, next, | 384 current, next, |
386 chooseBreaks[vs.limiting], | 385 chooseBreaks[vs.limiting], |
387 limitingAccess[vs.limiting]), | 386 limitingAccess[vs.limiting]), |
388 12*time.Hour, | 387 12*time.Hour, |
389 ); min > highest { | 388 ); min > highest { |
622 } | 621 } |
623 | 622 |
624 return lvs, rows.Err() | 623 return lvs, rows.Err() |
625 } | 624 } |
626 | 625 |
627 func loadSymbolBottlenecksFromTo( | 626 func loadSymbolBottlenecks( |
628 ctx context.Context, | 627 ctx context.Context, |
629 conn *sql.Conn, | 628 conn *sql.Conn, |
630 what, name string, | 629 what, name string, |
631 from, to time.Time, | 630 from, to time.Time, |
632 ) (bottlenecks, error) { | 631 ) (bottlenecks, error) { |
650 } | 649 } |
651 bns = append(bns, b) | 650 bns = append(bns, b) |
652 } | 651 } |
653 | 652 |
654 return bns, rows.Err() | 653 return bns, rows.Err() |
655 } | |
656 | |
657 func extractBottleneck( | |
658 _ context.Context, | |
659 _ *sql.Conn, | |
660 name string, | |
661 _, _ time.Time, | |
662 ) (bottlenecks, error) { | |
663 return bottlenecks{{id: name}}, nil | |
664 } | |
665 | |
666 func extractStretch( | |
667 ctx context.Context, | |
668 conn *sql.Conn, | |
669 name string, | |
670 from, to time.Time, | |
671 ) (bottlenecks, error) { | |
672 return loadSymbolBottlenecksFromTo( | |
673 ctx, | |
674 conn, | |
675 "users.stretches", name, | |
676 from, to) | |
677 } | |
678 | |
679 func extractSection( | |
680 ctx context.Context, | |
681 conn *sql.Conn, | |
682 name string, | |
683 from, to time.Time, | |
684 ) (bottlenecks, error) { | |
685 return loadSymbolBottlenecksFromTo( | |
686 ctx, | |
687 conn, | |
688 "waterway.sections", name, | |
689 from, to) | |
690 } | 654 } |
691 | 655 |
692 func (bn *bottleneck) loadLimitingValidities( | 656 func (bn *bottleneck) loadLimitingValidities( |
693 ctx context.Context, | 657 ctx context.Context, |
694 conn *sql.Conn, | 658 conn *sql.Conn, |