annotate pkg/controllers/stretches.go @ 3420:b9fc6c546610

fairway availability: Normalize results to look like as we only have one bottleneck.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 23 May 2019 15:32:38 +0200
parents 2b5c22f6bb1f
children 0a666ba899fa
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
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
61 ldc []referenceValue
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
62 breaks []referenceValue
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,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
82 depthbreaks, widthbreaks []referenceValue,
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
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
94 var breaks []referenceValue
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 {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
263 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
264 continue
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
265 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
266 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
267 }
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 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
270 http.Error(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
271 rw,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
272 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
273 http.StatusInternalServerError,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
274 )
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
275 return
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
276 }
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 type (
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
279 result struct {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
280 label string
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
281 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
282 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
283 }
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 calculation struct {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
286 result *result
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
287 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
288 }
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 job struct {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
291 result *result
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
292 bn *fullStretchBottleneck
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
293 }
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 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
297 if n == 0 {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
298 n = 1
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
299 }
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 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
302 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
303 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
304
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
305 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
306
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
307 go func() {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
308 defer close(done)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
309 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
310 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
311 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
312 ldc[i] += v
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
313 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
314 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
315 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
316 breaks[i] += v
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
317 }
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 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
322 wg.Add(1)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
323 go func() {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
324 defer wg.Done()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
325 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
326 bn := job.bn
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
327 res := job.result
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
328 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
329 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
330 bn.ldc,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
331 bn.access,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
332 )
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
333 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
334 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
335 bn.breaks,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
336 bn.access,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
337 )
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
338 calcCh <- calculation{
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
339 result: res,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
340 breaks: breaks,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
341 ldc: ldc,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
342 }
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 var results []*result
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
348
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
349 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
350
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
351 var breaks []referenceValue
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
352
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
353 if useDepth {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
354 breaks = depthbreaks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
355 } else {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
356 breaks = widthbreaks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
357 }
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 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
360
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
361 res := &result{
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
362 label: label,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
363 from: pfrom,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
364 to: pto,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
365 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
366 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
367 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
368 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
369
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
370 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
371 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
372 }
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 close(jobCh)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
375 wg.Wait()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
376 close(calcCh)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
377 <-done
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
378
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
379 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
380
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
381 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
382
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
383 // label, classes, lnwl
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
384 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
385 record[0] = "#label"
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
386 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
387 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
388 if useDepth && useWidth {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
389 if i == 0 {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
390 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
391 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
392 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
393 } else {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
394 if i == 0 {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
395 record[2] = fmt.Sprintf("# < %.2f [h]", v.value)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
396 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
397 record[i+3] = fmt.Sprintf("# >= %.2f [h]", v.value)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
398 }
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 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
402 // 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
403 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
404 return
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
405 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
406
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
407 // 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
408 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
409
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
410 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
411 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
412 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
413
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
414 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
415 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
416 }
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 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
419 // 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
420 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
421 return
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
422 }
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 out.Flush()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
426 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
427 // 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
428 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
429 }
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 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
433 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
434 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
435 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
436 b.WriteString(", ")
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
437 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
438 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
439 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
440 return b.String()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
441 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
442
3245
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
443 func stretchAvailabilty(
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
444 _ interface{},
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
445 req *http.Request,
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
446 conn *sql.Conn,
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
447 ) (jr JSONResult, err error) {
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
448 // TODO: Implement me!
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
449 return
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
450 }