comparison pkg/controllers/agmimports.go @ 1741:44398a8bdf94

Approved gauge measurements: Added a stub to upload a CSV file for parsing. TODO: Implement the parsing and store the values in the DB.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 09 Jan 2019 18:26:52 +0100
parents
children 807569b08513
comparison
equal deleted inserted replaced
1740:82a36eaf2366 1741:44398a8bdf94
1 // This is Free Software under GNU Affero General Public License v >= 3.0
2 // without warranty, see README.md and license for details.
3 //
4 // SPDX-License-Identifier: AGPL-3.0-or-later
5 // License-Filename: LICENSES/AGPL-3.0.txt
6 //
7 // Copyright (C) 2018 by via donau
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
9 // Software engineering by Intevation GmbH
10 //
11 // Author(s):
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
13
14 package controllers
15
16 import (
17 "bufio"
18 "io"
19 "io/ioutil"
20 "log"
21 "net/http"
22 "os"
23 "path/filepath"
24 "strconv"
25 "time"
26
27 "gemma.intevation.de/gemma/pkg/auth"
28 "gemma.intevation.de/gemma/pkg/common"
29 "gemma.intevation.de/gemma/pkg/config"
30 "gemma.intevation.de/gemma/pkg/imports"
31 )
32
33 const (
34 maxApprovedGaugeMeasurementSize = 25 * 1024 * 1024
35 approvedGaugeMeasurementsName = "approvedgm"
36 )
37
38 func storeApprovedGaugeMeasurements(req *http.Request) (string, error) {
39
40 // Check for direct upload.
41 f, _, err := req.FormFile(approvedGaugeMeasurementsName)
42 if err != nil {
43 return "", err
44 }
45 defer f.Close()
46
47 dir, err := ioutil.TempDir(config.TmpDir(), approvedGaugeMeasurementsName)
48 if err != nil {
49 return "", err
50 }
51
52 o, err := os.Create(filepath.Join(dir, "agm.csv"))
53 if err != nil {
54 os.RemoveAll(dir)
55 return "", err
56 }
57
58 out := bufio.NewWriter(o)
59
60 if _, err = io.Copy(out, io.LimitReader(f, maxApprovedGaugeMeasurementSize)); err != nil {
61 o.Close()
62 os.RemoveAll(dir)
63 return "", err
64 }
65
66 if err = out.Flush(); err != nil {
67 o.Close()
68 os.RemoveAll(dir)
69 return "", err
70 }
71
72 return dir, nil
73 }
74
75 func importApprovedGaugeMeasurements(rw http.ResponseWriter, req *http.Request) {
76
77 dir, err := storeApprovedGaugeMeasurements(req)
78 if err != nil {
79 log.Printf("error: %v\n", err)
80 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
81 return
82 }
83
84 agm := &imports.ApprovedGaugeMeasurements{Dir: dir}
85
86 serialized, err := common.ToJSONString(agm)
87 if err != nil {
88 log.Printf("error: %v\n", err)
89 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
90 return
91 }
92
93 session, _ := auth.GetSession(req)
94
95 sendEmail := req.FormValue("bottleneck") != ""
96
97 var due time.Time
98 if d := req.FormValue("due"); d != "" {
99 var err error
100 if due, err = time.Parse("2006-01-02T15:04:05", d); err != nil {
101 log.Printf("error: %v\n", err)
102 }
103 }
104
105 retries := -1
106 if r := req.FormValue("retries"); r != "" {
107 var err error
108 if retries, err = strconv.Atoi(r); err != nil {
109 log.Printf("error: %v\n", err)
110 retries = -1
111 }
112 }
113
114 jobID, err := imports.AddJob(
115 imports.AGMJobKind,
116 due, retries,
117 session.User,
118 sendEmail, false,
119 serialized)
120
121 if err != nil {
122 log.Printf("error: %v\n", err)
123 http.Error(rw, "error: "+err.Error(), http.StatusInternalServerError)
124 return
125 }
126
127 log.Printf("info: added import #%d to queue\n", jobID)
128
129 result := struct {
130 ID int64 `json:"id"`
131 }{
132 ID: jobID,
133 }
134 SendJSON(rw, http.StatusCreated, &result)
135 }