comparison pkg/controllers/system.go @ 3854:3fcc4e11fc00

Validate the config values of the morpho classes when saving. Also don't trigger the expensive re-calculation of the contour lines if only the colors changed.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 09 Jul 2019 11:31:49 +0200
parents d68c6be758b6
children 45be361f2d48
comparison
equal deleted inserted replaced
3853:abc15a3182c7 3854:3fcc4e11fc00
16 import ( 16 import (
17 "bytes" 17 "bytes"
18 "database/sql" 18 "database/sql"
19 "fmt" 19 "fmt"
20 "io/ioutil" 20 "io/ioutil"
21 "log"
21 "net/http" 22 "net/http"
22 "strings" 23 "strings"
23 "sync" 24 "sync"
24 25
25 "github.com/gorilla/mux" 26 "github.com/gorilla/mux"
144 145
145 jr = JSONResult{Result: settings} 146 jr = JSONResult{Result: settings}
146 return 147 return
147 } 148 }
148 149
150 type reconfFunc func(sql.NullString, string) (func(), error)
151
149 var ( 152 var (
150 reconfigureFuncsMu sync.Mutex 153 reconfigureFuncsMu sync.Mutex
151 reconfigureFuncs = map[string]func(){} 154 reconfigureFuncs = map[string]reconfFunc{}
152 ) 155 )
153 156
154 func registerReconfigureFunc(key string, fn func()) { 157 func registerReconfigureFunc(key string, fn reconfFunc) {
155 reconfigureFuncsMu.Lock() 158 reconfigureFuncsMu.Lock()
156 defer reconfigureFuncsMu.Unlock() 159 defer reconfigureFuncsMu.Unlock()
157 reconfigureFuncs[key] = fn 160 reconfigureFuncs[key] = fn
158 } 161 }
159 162
160 func reconfigureFunc(key string) func() { 163 func reconfigureFunc(key string) reconfFunc {
161 reconfigureFuncsMu.Lock() 164 reconfigureFuncsMu.Lock()
162 defer reconfigureFuncsMu.Unlock() 165 defer reconfigureFuncsMu.Unlock()
163 return reconfigureFuncs[key] 166 return reconfigureFuncs[key]
164 } 167 }
165 168
169 func reconfigureClassBreaks(old sql.NullString, curr, which string, recalc func()) (func(), error) {
170
171 // If new values are broken, don't proceed.
172 currCVs, err := models.ParseColorValues(curr)
173 if err != nil {
174 return nil, err
175 }
176
177 doBoth := func() {
178 log.Printf("info: Trigger re-calculation of %s.", which)
179 geoserver.ReconfigureStyle(which)
180 recalc()
181 }
182
183 if !old.Valid {
184 return doBoth, nil
185 }
186
187 oldCVs, err := models.ParseColorValues(old.String)
188 if err != nil {
189 log.Printf("warn: old config value is broken: %v\n", err)
190 return doBoth, nil
191 }
192
193 if len(currCVs) != len(oldCVs) {
194 return doBoth, nil
195 }
196
197 colorChanged := false
198
199 for i := range currCVs {
200 if currCVs[i].Value != oldCVs[i].Value {
201 return doBoth, nil
202 }
203 if currCVs[i].Color != oldCVs[i].Color {
204 colorChanged = true
205 }
206 }
207
208 // Only the color changed -> no expensive recalc needed.
209 if colorChanged {
210 log.Println("info: Only colors changed.")
211 return func() { geoserver.ReconfigureStyle(which) }, nil
212 }
213
214 return nil, nil
215 }
216
166 func init() { 217 func init() {
167 registerReconfigureFunc("morphology_classbreaks", func() { 218 registerReconfigureFunc("morphology_classbreaks",
168 geoserver.ReconfigureStyle("sounding_results_contour_lines_geoserver") 219 func(old sql.NullString, curr string) (func(), error) {
169 }) 220 return reconfigureClassBreaks(
170 registerReconfigureFunc("morphology_classbreaks_compare", func() { 221 old, curr,
171 geoserver.ReconfigureStyle("sounding_differences") 222 "sounding_results_contour_lines_geoserver",
172 }) 223 func() {
224 log.Println(
225 "todo: Trigger expensive recalculation of sounding result contours.")
226 })
227 })
228 registerReconfigureFunc("morphology_classbreaks_compare",
229 func(old sql.NullString, curr string) (func(), error) {
230 return reconfigureClassBreaks(
231 old, curr,
232 "sounding_differences",
233 func() {
234 log.Println(
235 "todo: Trigger expensive recalculation of sounding differences contours.")
236 })
237 })
173 } 238 }
174 239
175 func setSystemSettings( 240 func setSystemSettings(
176 input interface{}, 241 input interface{},
177 req *http.Request, 242 req *http.Request,
208 old.Valid, err = false, nil 273 old.Valid, err = false, nil
209 case err != nil: 274 case err != nil:
210 return 275 return
211 } 276 }
212 277
213 if !old.Valid || old.String != value { 278 if cmp := reconfigureFunc(key); cmp != nil {
214 if fn := reconfigureFunc(key); fn != nil { 279 var fn func()
280 if fn, err = cmp(old, value); err != nil {
281 return
282 }
283 if fn != nil {
215 reconfigure[key] = fn 284 reconfigure[key] = fn
216 } 285 }
217 } 286 }
218 287
219 if _, err = setStmt.ExecContext(ctx, key, value); err != nil { 288 if _, err = setStmt.ExecContext(ctx, key, value); err != nil {