Mercurial > gemma
comparison pkg/controllers/fwa.go @ 5221:6794e85dc2dd new-fwa
Write headers.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 12 May 2020 10:29:25 +0200 |
parents | 142ac550bd9a |
children | d4e4f7232513 |
comparison
equal
deleted
inserted
replaced
5220:142ac550bd9a | 5221:6794e85dc2dd |
---|---|
14 package controllers | 14 package controllers |
15 | 15 |
16 import ( | 16 import ( |
17 "context" | 17 "context" |
18 "database/sql" | 18 "database/sql" |
19 "encoding/csv" | |
19 "fmt" | 20 "fmt" |
20 "log" | 21 "log" |
21 "net/http" | 22 "net/http" |
22 "sort" | 23 "sort" |
23 "strconv" | 24 "strconv" |
196 los, ok := parseFormInt(rw, req, "los", 1) | 197 los, ok := parseFormInt(rw, req, "los", 1) |
197 if !ok { | 198 if !ok { |
198 return | 199 return |
199 } | 200 } |
200 | 201 |
201 // TODO: Use this. | |
202 availability := vars["type"] == "availability" | |
203 _ = availability | |
204 | |
205 ctx := req.Context() | 202 ctx := req.Context() |
206 conn := middleware.GetDBConn(req) | 203 conn := middleware.GetDBConn(req) |
207 | 204 |
208 // Function to extract the bottleneck_id's from the query. | 205 // Function to extract the bottleneck_id's from the query. |
209 var extract func(context.Context, *sql.Conn, string, time.Time, time.Time) (bottlenecks, error) | 206 var extract func(context.Context, *sql.Conn, string, time.Time, time.Time) (bottlenecks, error) |
276 http.StatusBadRequest, | 273 http.StatusBadRequest, |
277 ) | 274 ) |
278 return | 275 return |
279 } | 276 } |
280 | 277 |
278 availability := vars["type"] == "availability" | |
279 | |
280 var record []string | |
281 if availability { | |
282 // in days | |
283 record = makeHeader(useDepth && useWidth, 1, breaks, 'd') | |
284 } else { | |
285 // percentage | |
286 record = makeHeader(useDepth && useWidth, 3, breaks, '%') | |
287 } | |
288 | |
289 rw.Header().Add("Content-Type", "text/csv") | |
290 | |
291 out := csv.NewWriter(rw) | |
292 | |
293 if err := out.Write(record); err != nil { | |
294 // Too late for HTTP status message. | |
295 log.Printf("error: %v\n", err) | |
296 return | |
297 } | |
298 | |
299 for i := range record[1:] { | |
300 record[i+1] = "0.0" | |
301 } | |
302 | |
281 // For every day on every bottleneck we need to find out if this day is valid. | 303 // For every day on every bottleneck we need to find out if this day is valid. |
282 validities := make([]func(time.Time, time.Time) *limitingValidity, len(bottlenecks)) | 304 validities := make([]func(time.Time, time.Time) *limitingValidity, len(bottlenecks)) |
283 for i := range bottlenecks { | 305 for i := range bottlenecks { |
284 validities[i] = bottlenecks[i].validities.find() | 306 validities[i] = bottlenecks[i].validities.find() |
285 } | 307 } |
354 | 376 |
355 current = next | 377 current = next |
356 } | 378 } |
357 | 379 |
358 // TODO: Log missing LDCs | 380 // TODO: Log missing LDCs |
381 | |
382 out.Flush() | |
383 if err := out.Error(); err != nil { | |
384 // Too late for HTTP status message. | |
385 log.Printf("error: %v\n", err) | |
386 } | |
359 } | 387 } |
360 | 388 |
361 func dusk(t time.Time) time.Time { | 389 func dusk(t time.Time) time.Time { |
362 return time.Date( | 390 return time.Date( |
363 t.Year(), | 391 t.Year(), |