changeset 3929:45be361f2d48

If the settings for sounding diffs are changed in a way that they need recalculation, flush the cache.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 11 Jul 2019 18:54:22 +0200
parents 90496300311d
children c2ac23175a8f
files pkg/controllers/system.go
diffstat 1 files changed, 20 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/system.go	Thu Jul 11 17:26:09 2019 +0200
+++ b/pkg/controllers/system.go	Thu Jul 11 18:54:22 2019 +0200
@@ -15,6 +15,7 @@
 
 import (
 	"bytes"
+	"context"
 	"database/sql"
 	"fmt"
 	"io/ioutil"
@@ -25,6 +26,7 @@
 
 	"github.com/gorilla/mux"
 
+	"gemma.intevation.de/gemma/pkg/auth"
 	"gemma.intevation.de/gemma/pkg/config"
 	"gemma.intevation.de/gemma/pkg/geoserver"
 	"gemma.intevation.de/gemma/pkg/models"
@@ -51,6 +53,9 @@
 INSERT INTO sys_admin.system_config (config_key, config_val)
 VALUES ($1, $2)
 ON CONFLICT (config_key) DO UPDATE SET config_val = $2`
+
+	deleteSoundingDiffsSQL = `
+DELETE FROM caching.sounding_differences`
 )
 
 // System status end points
@@ -230,13 +235,24 @@
 			return reconfigureClassBreaks(
 				old, curr,
 				"sounding_differences",
-				func() {
-					log.Println(
-						"todo: Trigger expensive recalculation of sounding differences contours.")
-				})
+				func() { go deleteSoundingDiffs() })
 		})
 }
 
+func deleteSoundingDiffs() {
+	// TODO: Better do that in import queue?
+	ctx := context.Background()
+
+	if err := auth.RunAs(ctx, "sys_admin",
+		func(conn *sql.Conn) error {
+			_, err := conn.ExecContext(ctx, deleteSoundingDiffsSQL)
+			return err
+		},
+	); err != nil {
+		log.Printf("error: Cleaning sounding diffs cache failed: %v\n", err)
+	}
+}
+
 func setSystemSettings(
 	input interface{},
 	req *http.Request,