annotate pkg/controllers/stretches.go @ 3430:6994602d2935

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