annotate pkg/controllers/stretches.go @ 3452:ae6c09fbc590

fairway depth/availability: bring unified CVS to streches and sections, too.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 24 May 2019 13:09:08 +0200
parents d7ddb21f7017
children 7265adcc5baa
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 "strings"
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
25 "sync"
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
26 "time"
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
27
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
28 "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
29 "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
30 )
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
32 const (
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
33 selectSectionBottlenecks = `
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
34 SELECT
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
35 distinct(b.objnam),
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
36 b.limiting
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
37 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
38 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
39
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
40 selectStretchBottlenecks = `
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
41 SELECT
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
42 distinct(b.objnam),
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
43 b.limiting
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
44 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
45 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
46 )
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
47
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
48 type (
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
49 stretchBottleneck struct {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
50 name string
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
51 limiting string
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
52 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
53
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
54 stretchBottlenecks []stretchBottleneck
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 fullStretchBottleneck struct {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
57 *stretchBottleneck
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
58 measurements availMeasurements
3424
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3420
diff changeset
59 ldc []float64
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3420
diff changeset
60 breaks []float64
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
61 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
62 }
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
63
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
64 availResult struct {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
65 label string
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
66 from, to time.Time
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
67 ldc, breaks []time.Duration
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
68 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
69
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
70 availCalculation struct {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
71 result *availResult
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
72 ldc, breaks []time.Duration
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
73 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
74
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
75 availJob struct {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
76 result *availResult
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
77 bn *fullStretchBottleneck
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
78 }
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
79 )
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
80
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
81 func (r *availResult) add(c availCalculation) {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
82 for i, v := range c.ldc {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
83 r.ldc[i] += v
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
84 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
85 for i, v := range c.breaks {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
86 r.breaks[i] += v
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
87 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
88 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
89
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
90 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
91 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
92 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
93 return true
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
94 }
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 return false
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
97 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
98
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
99 func loadFullStretchBottleneck(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
100 ctx context.Context,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
101 conn *sql.Conn,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
102 bn *stretchBottleneck,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
103 los int,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
104 from, to time.Time,
3424
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3420
diff changeset
105 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
106 ) (*fullStretchBottleneck, error) {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
107 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
108 if err != nil {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
109 return nil, err
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 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
112 if err != nil {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
113 return nil, err
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
114 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
115
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
116 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
117 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
118
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
119 switch bn.limiting {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
120 case "width":
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
121 access = (*availMeasurement).getWidth
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
122 breaks = widthbreaks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
123 case "depth":
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
124 access = (*availMeasurement).getDepth
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
125 breaks = depthbreaks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
126 default:
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
127 log.Printf(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
128 "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
129 bn.limiting)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
130 access = (*availMeasurement).getDepth
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
131 breaks = depthbreaks
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
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
134 return &fullStretchBottleneck{
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
135 stretchBottleneck: bn,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
136 measurements: measurements,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
137 ldc: ldc,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
138 breaks: breaks,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
139 access: access,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
140 }, nil
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
141 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
142
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
143 func loadStretchBottlenecks(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
144 ctx context.Context,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
145 conn *sql.Conn,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
146 stretch bool,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
147 name string,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
148 ) (stretchBottlenecks, error) {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
149 var sql string
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
150 if stretch {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
151 sql = selectStretchBottlenecks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
152 } else {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
153 sql = selectSectionBottlenecks
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 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
157 if err != nil {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
158 return nil, err
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
159 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
160 defer rows.Close()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
161
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
162 var bns stretchBottlenecks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
163
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
164 for rows.Next() {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
165 var bn stretchBottleneck
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
166 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
167 &bn.name,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
168 &bn.limiting,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
169 ); err != nil {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
170 return nil, err
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
171 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
172 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
173 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
174
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
175 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
176 return nil, err
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
177 }
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 return bns, nil
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
180 }
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 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
183
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
184 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
185 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
186 name := vars["name"]
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
187 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
188
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
189 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
190
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
191 from, ok := parseFormTime(rw, req, "from", time.Now().AddDate(-1, 0, 0))
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
192 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
193 return
3405
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
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
196 to, ok := parseFormTime(rw, req, "to", from.AddDate(1, 0, 0))
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
197 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
198 return
3405
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 to.Before(from) {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
202 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
203 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
204
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
205 los, ok := parseFormInt(rw, req, "los", 1)
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
206 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
207 return
3405
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
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
210 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
211 ctx := req.Context()
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 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
214 if err != nil {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
215 http.Error(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
216 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
217 http.StatusInternalServerError)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
218 return
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
219 }
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
220
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
221 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
222 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
223 return
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
224 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
225
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
226 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
227 depthbreaks = breaksToReferenceValue(b)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
228 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
229
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
230 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
231 widthbreaks = breaksToReferenceValue(b)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
232 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
233
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
234 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
235
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
236 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
237 http.Error(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
238 rw,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
239 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
240 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
241 http.StatusBadRequest,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
242 )
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
243 return
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
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
246 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
247
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
248 var loaded []*fullStretchBottleneck
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
249 var errors []error
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
250
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
251 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
252 l, err := loadFullStretchBottleneck(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
253 ctx,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
254 conn,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
255 &bns[i],
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
256 los,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
257 from, to,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
258 depthbreaks, widthbreaks,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
259 )
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
260 if err != nil {
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
261 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
262 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
263 continue
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
264 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
265 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
266 }
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 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
269 http.Error(
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
270 rw,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
271 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
272 http.StatusInternalServerError,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
273 )
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
274 return
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
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
277 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
278 if n == 0 {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
279 n = 1
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
280 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
281
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
282 jobCh := make(chan availJob)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
283 calcCh := make(chan availCalculation, n)
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
284 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
285
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
286 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
287
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
288 go func() {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
289 defer close(done)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
290 for calc := range calcCh {
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
291 calc.result.add(calc)
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
292 }
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 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
296 wg.Add(1)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
297 go func() {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
298 defer wg.Done()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
299 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
300 bn := job.bn
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
301 res := job.result
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
302 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
303 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
304 bn.ldc,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
305 bn.access,
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 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
308 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
309 bn.breaks,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
310 bn.access,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
311 )
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
312 calcCh <- availCalculation{
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
313 result: res,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
314 breaks: breaks,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
315 ldc: ldc,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
316 }
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
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
321 var results []*availResult
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
322
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
323 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
324
3424
0a666ba899fa fairway availabilty: removed level from class breaks.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3420
diff changeset
325 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
326
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
327 if useDepth {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
328 breaks = depthbreaks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
329 } else {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
330 breaks = widthbreaks
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
331 }
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 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
334
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
335 res := &availResult{
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
336 label: label,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
337 from: pfrom,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
338 to: pto,
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
339 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
340 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
341 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
342 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
343
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
344 for _, bn := range loaded {
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
345 jobCh <- availJob{bn: bn, result: res}
3405
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 }
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
348
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
349 close(jobCh)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
350 wg.Wait()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
351 close(calcCh)
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
352 <-done
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 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
355
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
356 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
357
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
358 // label, lnwl, classes
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
359 record := make([]string, 1+2+len(breaks)+1)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
360 record[0] = "# time"
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
361 record[1] = "# < LDC [h]"
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
362 record[2] = "# >= LDC [h]"
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
363 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
364 if useDepth && useWidth {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
365 if i == 0 {
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
366 record[3] = "# < break_1 [h]"
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
367 }
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
368 record[i+4] = fmt.Sprintf("# >= break_%d", i+1)
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
369 } else {
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
370 if i == 0 {
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
371 record[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
372 }
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
373 record[i+4] = 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
374 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
375 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
376
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
377 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
378 // 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
379 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
380 return
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
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
383 // 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
384 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
385
3405
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
386 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
387 record[0] = r.label
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
388 for i, v := range r.ldc {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
389 record[1+i] = fmt.Sprintf("%.3f", v.Hours()*scale)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
390 }
3405
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 for i, d := range r.breaks {
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
393 record[3+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
394 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
395
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
396 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
397 // 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
398 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
399 return
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
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
403 out.Flush()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
404 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
405 // 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
406 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
407 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
408 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
409
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
410 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
411 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
412 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
413 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
414 b.WriteString(", ")
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
415 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
416 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
417 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
418 return b.String()
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
419 }
2b5c22f6bb1f available fairway depth: Implemented backend for sections and stretches.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3245
diff changeset
420
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
421 func stretchAvailabilty(rw http.ResponseWriter, req *http.Request) {
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
422
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
423 vars := mux.Vars(req)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
424 stretch := vars["kind"] == "stretch"
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
425 name := vars["name"]
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
426 mode := intervalMode(req.FormValue("mode"))
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
427
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
428 if name == "" {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
429 http.Error(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
430 rw,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
431 fmt.Sprintf("Missing %s name", vars["kind"]),
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
432 http.StatusBadRequest,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
433 )
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
434 return
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
435 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
436
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
437 from, ok := parseFormTime(rw, req, "form", time.Now().AddDate(-1, 0, 0))
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
438 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
439 return
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
440 }
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
441
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
442 to, ok := parseFormTime(rw, req, "to", from.AddDate(1, 0, 0))
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
443 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
444 return
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
445 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
446
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
447 if to.Before(from) {
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
448 to, from = from, to
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
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
451 los, ok := parseFormInt(rw, req, "los", 1)
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
452 if !ok {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
453 return
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
454 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
455
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
456 depthbreaks, widthbreaks := afdRefs, afdRefs
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
457
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
458 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
459 depthbreaks = breaksToReferenceValue(b)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
460 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
461
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
462 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
463 widthbreaks = breaksToReferenceValue(b)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
464 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
465
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
466 conn := middleware.GetDBConn(req)
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
467 ctx := req.Context()
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
468
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
469 bns, err := loadStretchBottlenecks(ctx, conn, stretch, name)
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
470 if err != nil {
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
471 http.Error(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
472 rw, fmt.Sprintf("DB error: %v.", err),
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
473 http.StatusInternalServerError)
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
474 return
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
475 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
476
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
477 if len(bns) == 0 {
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
478 http.Error(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
479 rw,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
480 "No bottlenecks found.",
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
481 http.StatusNotFound,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
482 )
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
483 return
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
484 }
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 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
487
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
488 if useDepth && useWidth && len(widthbreaks) != len(depthbreaks) {
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
489 http.Error(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
490 rw,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
491 fmt.Sprintf("class breaks lengths differ: %d != %d",
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
492 len(widthbreaks), len(depthbreaks)),
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
493 http.StatusBadRequest,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
494 )
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
495 return
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
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
498 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
499
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
500 var loaded []*fullStretchBottleneck
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
501 var errors []error
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
502
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
503 for i := range bns {
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
504 l, err := loadFullStretchBottleneck(
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
505 ctx,
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
506 conn,
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
507 &bns[i],
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
508 los,
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
509 from, to,
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
510 depthbreaks, widthbreaks,
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 err != nil {
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
513 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
514 errors = append(errors, err)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
515 continue
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
516 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
517 loaded = append(loaded, l)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
518 }
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 if len(loaded) == 0 {
3439
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
521 http.Error(
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
522 rw,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
523 fmt.Sprintf("No bottleneck loaded: %v", joinErrors(errors)),
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
524 http.StatusInternalServerError,
d7ddb21f7017 Prepared to write the fairway availibilty as CSV, too. WIP.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3430
diff changeset
525 )
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
526 return
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
527 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
528
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
529 n := runtime.NumCPU() / 2
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
530 if n == 0 {
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
531 n = 1
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
532 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
533
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
534 jobCh := make(chan availJob)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
535 calcCh := make(chan availCalculation, n)
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
536 done := make(chan struct{})
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
537
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
538 var wg sync.WaitGroup
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 go func() {
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
541 defer close(done)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
542 for calc := range calcCh {
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
543 calc.result.add(calc)
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
544 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
545 }()
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
546
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
547 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
548 wg.Add(1)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
549 go func() {
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
550 defer wg.Done()
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
551 for job := range jobCh {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
552 bn := job.bn
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
553 res := job.result
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
554 ldc := bn.measurements.classify(
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
555 from, to,
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
556 bn.ldc,
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
557 (*availMeasurement).getValue,
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
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
560 breaks := bn.measurements.classify(
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
561 from, to,
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
562 bn.breaks,
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
563 bn.access,
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
564 )
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
565 calcCh <- availCalculation{
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
566 result: res,
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
567 breaks: breaks,
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
568 ldc: ldc,
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
569 }
3430
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 }()
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
572 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
573
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
574 var results []*availResult
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
575
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
576 interval := intervals[mode](from, to)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
577
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
578 var breaks []float64
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
579
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
580 if useDepth {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
581 breaks = depthbreaks
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
582 } else {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
583 breaks = widthbreaks
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
584 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
585
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
586 for pfrom, pto, label := interval(); label != ""; pfrom, pto, label = interval() {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
587
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
588 res := &availResult{
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
589 label: label,
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
590 from: pfrom,
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
591 to: pto,
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
592 ldc: make([]time.Duration, 2),
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
593 breaks: make([]time.Duration, len(breaks)+1),
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
594 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
595 results = append(results, res)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
596
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
597 for _, bn := range loaded {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
598 jobCh <- availJob{bn: bn, result: res}
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
599 }
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
600 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
601
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
602 close(jobCh)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
603 wg.Wait()
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
604 close(calcCh)
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
605 <-done
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
606
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
607 rw.Header().Add("Content-Type", "text/csv")
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
608
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
609 out := csv.NewWriter(rw)
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
610
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
611 // label, lnwl, classes
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
612 record := make([]string, 1+2+len(breaks)+1)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
613 record[0] = "# time"
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
614 record[1] = "# < LDC [%%]"
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
615 record[2] = "# >= LDC [%%]"
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
616 for i, v := range breaks {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
617 if useDepth && useWidth {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
618 if i == 0 {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
619 record[3] = "# < break_1 [%%]"
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
620 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
621 record[i+4] = fmt.Sprintf("# >= break_%d [%%]", i+1)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
622 } else {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
623 if i == 0 {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
624 record[3] = fmt.Sprintf("# < %.3f [%%]", v)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
625 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
626 record[i+4] = fmt.Sprintf("# >= %.3f [%%]", v)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
627 }
3430
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
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
630 if err := out.Write(record); err != nil {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
631 // Too late for HTTP status message.
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
632 log.Printf("error: %v\n", err)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
633 return
3430
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
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
636 for _, r := range results {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
637 duration := r.to.Sub(r.from) * time.Duration(len(loaded))
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
638
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
639 ldcPercents := durationsToPercentage(duration, r.ldc)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
640 breaksPercents := durationsToPercentage(duration, r.breaks)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
641
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
642 record[0] = r.label
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
643
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
644 for i, v := range ldcPercents {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
645 record[2+i] = fmt.Sprintf("%.3f", v)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
646 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
647
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
648 for i, v := range breaksPercents {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
649 record[3+i] = fmt.Sprintf("%.3f", v)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
650 }
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
651
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
652 if err := out.Write(record); err != nil {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
653 // Too late for HTTP status message.
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
654 log.Printf("error: %v\n", err)
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
655 return
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
656 }
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
657 }
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
658
3452
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
659 out.Flush()
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
660 if err := out.Error(); err != nil {
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
661 // Too late for HTTP status message.
ae6c09fbc590 fairway depth/availability: bring unified CVS to streches and sections, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3439
diff changeset
662 log.Printf("error: %v\n", err)
3430
6994602d2935 fairway availibilty: Implemented for streches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3424
diff changeset
663 }
3245
60f25cbe77fb Added API stubs from fairway availabilty for stretches and sections.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
664 }