annotate pkg/controllers/report.go @ 5591:0011f50cf216 surveysperbottleneckid

Removed no longer used alternative api for surveys/ endpoint. As bottlenecks in the summary for SR imports are now identified by their id and no longer by the (not guarantied to be unique!) name, there is no longer the need to request survey data by the name+date tuple (which isn't reliable anyway). So the workaround was now reversed.
author Sascha Wilde <wilde@sha-bang.de>
date Wed, 06 Apr 2022 13:30:29 +0200
parents 5f47eeea988d
children bbc257dd9abf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5321
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 // This is Free Software under GNU Affero General Public License v >= 3.0
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2 // without warranty, see README.md and license for details.
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 //
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 // SPDX-License-Identifier: AGPL-3.0-or-later
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 // License-Filename: LICENSES/AGPL-3.0.txt
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 //
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 // Copyright (C) 2021 by via donau
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 // Software engineering by Intevation GmbH
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 //
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 // Author(s):
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 package controllers
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import (
5333
6c0f40676984 Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5324
diff changeset
17 "database/sql"
5321
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 "fmt"
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 "net/http"
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 "os"
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 "path/filepath"
5324
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
22 "sort"
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
23 "strings"
5321
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 "gemma.intevation.de/gemma/pkg/config"
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 "gemma.intevation.de/gemma/pkg/middleware"
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 "gemma.intevation.de/gemma/pkg/xlsx"
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5478
diff changeset
29 "gemma.intevation.de/gemma/pkg/log"
5324
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
30 mw "gemma.intevation.de/gemma/pkg/middleware"
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
31
5321
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
32 "github.com/gorilla/mux"
5478
699048c86848 Updated 3rd-party libs of the gemma server to the latest bug fix release.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5333
diff changeset
33 "github.com/xuri/excelize/v2"
5321
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 )
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35
5324
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
36 func listReports(req *http.Request) (jr mw.JSONResult, err error) {
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
37 path := config.ReportPath()
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
38 if path == "" {
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
39 err = mw.JSONError{
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
40 Code: http.StatusNotFound,
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
41 Message: http.StatusText(http.StatusNotFound),
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
42 }
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
43 return
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
44 }
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
45
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
46 // This would be easier with Go 1.16+.
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
47
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
48 dir, err := os.Open(path)
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
49 if err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5478
diff changeset
50 log.Errorf("%v\n", err)
5324
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
51 err = mw.JSONError{
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
52 Code: http.StatusInternalServerError,
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
53 Message: "Listing report templates failed.",
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
54 }
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
55 return
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
56 }
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
57 defer dir.Close()
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
58 files, err := dir.Readdirnames(-1)
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
59 if err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5478
diff changeset
60 log.Errorf("%v\n", err)
5324
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
61 err = mw.JSONError{
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
62 Code: http.StatusInternalServerError,
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
63 Message: "Listing report templates failed.",
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
64 }
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
65 return
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
66 }
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
67
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
68 pairs := map[string]int{}
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
69
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
70 all:
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
71 for _, file := range files {
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
72 var mask int
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
73 switch {
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
74 case strings.HasSuffix(file, ".xlsx"):
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
75 mask = 1
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
76 case strings.HasSuffix(file, ".yaml"):
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
77 mask = 2
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
78 default:
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
79 continue all
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
80 }
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
81 basename := filepath.Base(file)
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
82 name := strings.TrimSuffix(basename, filepath.Ext(basename))
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
83 pairs[name] |= mask
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
84 }
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
85
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
86 var reports []string
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
87 for name, mask := range pairs {
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
88 if mask == 3 {
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
89 reports = append(reports, name)
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
90 }
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
91 }
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
92 sort.Strings(reports)
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
93
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
94 out := struct {
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
95 Reports []string `json:"reports"`
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
96 }{
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
97 Reports: reports,
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
98 }
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
99 jr = mw.JSONResult{Result: out}
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
100 return
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
101 }
348d91848278 Added and endpoint GET /api/data/reports to list the available report templates.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5322
diff changeset
102
5321
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
103 func report(rw http.ResponseWriter, req *http.Request) {
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 path := config.ReportPath()
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 if path == "" {
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 http.NotFound(rw, req)
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 return
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109 }
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110
5322
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
111 if stat, err := os.Stat(path); err != nil {
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
112 if os.IsNotExist(err) {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5478
diff changeset
113 log.Errorf("report dir '%s' does not exists.\n", path)
5322
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
114 http.NotFound(rw, req)
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
115 } else {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5478
diff changeset
116 log.Errorf("%v\n", err)
5322
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
117 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError)
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
118 }
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
119 return
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
120 } else if !stat.Mode().IsDir() {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5478
diff changeset
121 log.Errorf("report dir '%s' is not a directory.\n", path)
5322
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
122 http.NotFound(rw, req)
5321
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 return
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124 }
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
125
5322
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
126 vars := mux.Vars(req)
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
127 name := vars["name"]
5321
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
128
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
129 xlsxFilename := filepath.Join(path, name+".xlsx")
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 yamlFilename := filepath.Join(path, name+".yaml")
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131
5322
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
132 for _, check := range []string{xlsxFilename, yamlFilename} {
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
133 if _, err := os.Stat(check); err != nil {
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
134 if os.IsNotExist(err) {
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
135 http.NotFound(rw, req)
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
136 } else {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5478
diff changeset
137 log.Errorf("%v\n", err)
5322
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
138 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError)
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
139 }
80d9fd782f00 Straightened the error logging in report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5321
diff changeset
140 return
5321
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
141 }
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
142 }
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
143
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 template, err := excelize.OpenFile(xlsxFilename)
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
145 if err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5478
diff changeset
146 log.Errorf("%v\n", err)
5321
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
147 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError)
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
148 return
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
149 }
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
150
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
151 action, err := xlsx.ActionFromFile(yamlFilename)
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
152 if err != nil {
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
153 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError)
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5478
diff changeset
154 log.Errorf("%v\n", err)
5321
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
155 return
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
156 }
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
158 ctx := req.Context()
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
159 conn := middleware.GetDBConn(req)
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
160
5333
6c0f40676984 Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5324
diff changeset
161 tx, err := conn.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
6c0f40676984 Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5324
diff changeset
162 defer tx.Rollback()
6c0f40676984 Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5324
diff changeset
163
6c0f40676984 Run xlsx templating in a read-only transaction.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5324
diff changeset
164 if err := action.Execute(ctx, tx, template); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5478
diff changeset
165 log.Errorf("%v\n", err)
5321
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166 http.Error(rw, "Error: "+err.Error(), http.StatusInternalServerError)
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167 return
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
168 }
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
169 rw.Header().Set(
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
170 "Content-Disposition",
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171 fmt.Sprintf("attachment; filename=%s.xlsx", name))
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
172 rw.Header().Set(
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
173 "Content-Type",
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
174 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
175
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
176 if _, err := template.WriteTo(rw); err != nil {
5490
5f47eeea988d Use own logging package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 5478
diff changeset
177 log.Errorf("%v\n", err)
5321
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178 }
0919946f624b Added a report controller.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
179 }