Mercurial > gemma
comparison pkg/controllers/manualimports.go @ 1879:9a2fbeaabd52 dev-pdf-generation
merging in from branch default
author | Bernhard Reiter <bernhard@intevation.de> |
---|---|
date | Tue, 15 Jan 2019 10:07:10 +0100 |
parents | 614c6c766691 |
children | 77582da3adb0 |
comparison
equal
deleted
inserted
replaced
1878:f030182f82f1 | 1879:9a2fbeaabd52 |
---|---|
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 // * Raimund Renkert <raimund.renkert@intevation.de> | |
14 | |
15 package controllers | |
16 | |
17 import ( | |
18 "database/sql" | |
19 "log" | |
20 "net/http" | |
21 "time" | |
22 | |
23 "gemma.intevation.de/gemma/pkg/auth" | |
24 "gemma.intevation.de/gemma/pkg/common" | |
25 "gemma.intevation.de/gemma/pkg/imports" | |
26 "gemma.intevation.de/gemma/pkg/models" | |
27 ) | |
28 | |
29 func retry(a common.Attributes) (time.Time, int) { | |
30 due, _ := a.Time("due") | |
31 retries, ok := a.Int("retries") | |
32 if !ok { | |
33 retries = -1 | |
34 } | |
35 return due, retries | |
36 } | |
37 | |
38 func importBottleneck(input interface{}) (interface{}, time.Time, int, bool) { | |
39 bi := input.(*models.BottleneckImport) | |
40 bn := &imports.Bottleneck{ | |
41 URL: bi.URL, | |
42 Insecure: bi.Insecure, | |
43 } | |
44 due, retries := retry(bi.Attributes) | |
45 return bn, due, retries, bi.SendEmail | |
46 } | |
47 | |
48 func importGaugeMeasurement(input interface{}) (interface{}, time.Time, int, bool) { | |
49 gi := input.(*models.GaugeMeasurementImport) | |
50 gm := &imports.GaugeMeasurement{ | |
51 URL: gi.URL, | |
52 Insecure: gi.Insecure, | |
53 } | |
54 due, retries := retry(gi.Attributes) | |
55 return gm, due, retries, gi.SendEmail | |
56 } | |
57 | |
58 func importFairwayAvailability(input interface{}) (interface{}, time.Time, int, bool) { | |
59 fai := input.(*models.FairwayAvailabilityImport) | |
60 fa := &imports.FairwayAvailability{ | |
61 URL: fai.URL, | |
62 Insecure: fai.Insecure, | |
63 } | |
64 due, retries := retry(fai.Attributes) | |
65 return fa, due, retries, fai.SendEmail | |
66 } | |
67 | |
68 func importWaterwayAxis(input interface{}) (interface{}, time.Time, int, bool) { | |
69 wxi := input.(*models.WaterwayAxisImport) | |
70 wx := &imports.WaterwayAxis{ | |
71 URL: wxi.URL, | |
72 FeatureType: wxi.FeatureType, | |
73 SortBy: wxi.SortBy, | |
74 } | |
75 due, retries := retry(wxi.Attributes) | |
76 return wx, due, retries, wxi.SendEmail | |
77 } | |
78 | |
79 func importWaterwayArea(input interface{}) (interface{}, time.Time, int, bool) { | |
80 wai := input.(*models.WaterwayAreaImport) | |
81 wa := &imports.WaterwayArea{ | |
82 URL: wai.URL, | |
83 FeatureType: wai.FeatureType, | |
84 SortBy: wai.SortBy, | |
85 } | |
86 due, retries := retry(wai.Attributes) | |
87 return wa, due, retries, wai.SendEmail | |
88 } | |
89 | |
90 func manualImport( | |
91 kind imports.JobKind, | |
92 setup func(interface{}) (interface{}, time.Time, int, bool), | |
93 ) func(interface{}, *http.Request, *sql.Conn) (JSONResult, error) { | |
94 | |
95 return func(input interface{}, req *http.Request, _ *sql.Conn) ( | |
96 jr JSONResult, err error) { | |
97 | |
98 what, due, retries, sendEmail := setup(input) | |
99 | |
100 var serialized string | |
101 if serialized, err = common.ToJSONString(what); err != nil { | |
102 return | |
103 } | |
104 | |
105 session, _ := auth.GetSession(req) | |
106 | |
107 var jobID int64 | |
108 if jobID, err = imports.AddJob( | |
109 kind, | |
110 due, retries, | |
111 session.User, | |
112 sendEmail, | |
113 serialized, | |
114 ); err != nil { | |
115 return | |
116 } | |
117 | |
118 log.Printf("info: added import #%d to queue\n", jobID) | |
119 | |
120 result := struct { | |
121 ID int64 `json:"id"` | |
122 }{ | |
123 ID: jobID, | |
124 } | |
125 | |
126 jr = JSONResult{ | |
127 Code: http.StatusCreated, | |
128 Result: &result, | |
129 } | |
130 return | |
131 } | |
132 } |