annotate pkg/controllers/gauges.go @ 2694:0d7a4fdb9e12

Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 15 Mar 2019 18:34:13 +0100
parents
children 87aed4f9b1b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2694
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2019 by via donau
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package controllers
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import (
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 "encoding/csv"
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "fmt"
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "log"
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "net/http"
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 "strconv"
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 "time"
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24 "github.com/gorilla/mux"
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 "gemma.intevation.de/gemma/pkg/middleware"
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 "gemma.intevation.de/gemma/pkg/models"
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 )
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 const (
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31 selectWaterlevelsSQL = `
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 SELECT
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
33 measure_date,
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 water_level,
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 predicted
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 FROM waterway.gauge_measurements
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37 WHERE
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 `
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 )
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 func waterlevels(rw http.ResponseWriter, req *http.Request) {
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 gauge := mux.Vars(req)["gauge"]
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 isrs, err := models.IsrsFromString(gauge)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45 if err != nil {
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 http.Error(
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 rw, fmt.Sprintf("error: Invalid ISRS code: %v", err),
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 http.StatusBadRequest)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 return
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 }
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 var fb filterBuilder
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 fb.stmt.WriteString(selectWaterlevelsSQL)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 fb.cond(
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 " fk_gauge_id = ($%d::char(2), $%d::char(3), $%d::char(5), $%d::char(5), $%d::int) ",
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 isrs.CountryCode,
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 isrs.LoCode,
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 isrs.FairwaySection,
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 isrs.Orc,
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 isrs.Hectometre,
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 )
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 if from := req.FormValue("from"); from != "" {
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 fromTime, err := time.Parse(models.ImportTimeFormat, from)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 if err != nil {
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 http.Error(
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 rw, fmt.Sprintf("error: Invalid from time: %v", err),
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 http.StatusBadRequest)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 return
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 }
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 fb.cond("measure_date >= $%d", fromTime)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 }
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 if to := req.FormValue("to"); to != "" {
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 toTime, err := time.Parse(models.ImportTimeFormat, to)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 if err != nil {
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 http.Error(
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 rw, fmt.Sprintf("error: Invalid from time: %v", err),
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 http.StatusBadRequest)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 return
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 }
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 fb.cond("measure_date <= $%d", toTime)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 }
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 conn := middleware.GetDBConn(req)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 ctx := req.Context()
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 rows, err := conn.QueryContext(ctx, fb.stmt.String(), fb.args...)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 if err != nil {
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 http.Error(
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 rw, fmt.Sprintf("error: %v", err),
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 http.StatusInternalServerError)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 return
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96 }
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
97 defer rows.Close()
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
98
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 rw.Header().Add("Content-Type", "text/csv")
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101 out := csv.NewWriter(rw)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
102
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 record := make([]string, 3)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 for rows.Next() {
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 var (
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 measureDate time.Time
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 waterlevel float64
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 predicted bool
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 )
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111 if err := rows.Scan(&measureDate, &waterlevel, &predicted); err != nil {
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
112 log.Printf("error: %v\n", err)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
113 // Too late for an HTTP error code.
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
114 return
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 }
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
116 record[0] = measureDate.Format(models.ImportTimeFormat)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
117 record[1] = strconv.FormatFloat(waterlevel, 'f', -1, 64)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 if predicted {
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 record[2] = "t"
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 } else {
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 record[2] = "f"
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 }
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 if err := out.Write(record); err != nil {
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124 log.Printf("error: %v", err)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125 // Too late for an HTTP error code.
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
126 return
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
127 }
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128 }
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 if err := rows.Err(); err != nil {
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131 log.Printf("error: %v", err)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132 // Too late for an HTTP error code.
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
133 return
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134 }
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
135
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
136 out.Flush()
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
137 if err := out.Error(); err != nil {
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
138 log.Printf("error: %v", err)
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
139 // Too late for an HTTP error code.
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
140 return
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
141 }
0d7a4fdb9e12 Added GET /api/data/waterlevels/{gauge isrs}?from={time_a}&to={time_b} to fetch waterlevels of gauge.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
142 }