From 4c409224b02f3212f31f9752459c63941742c923 Mon Sep 17 00:00:00 2001 From: Simon Rieger Date: Sat, 28 Sep 2024 15:05:40 +0200 Subject: [PATCH] =?UTF-8?q?Algorithmus=20zum=20l=C3=B6scheb=20alter=20Date?= =?UTF-8?q?nbankeintr=C3=A4ge=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.yml | 1 + main.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index ed7926c..b8580c9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,6 +15,7 @@ services: - API_BASE_URL=http://db-rest:3000 - MAX_RESULTS=200 - DURATION=240 + - DELETE_AFTER_MINUTES=30 - BUS=false - FERRY=false - TRAM=false diff --git a/main.go b/main.go index b7330aa..ad0c521 100644 --- a/main.go +++ b/main.go @@ -63,6 +63,10 @@ func main() { if err != nil { log.Fatalf("Ungültiger Wert für TAXI: %v", err) } + deleteAfter, err := strconv.Atoi(os.Getenv("DELETE_AFTER_MINUTES")) + if err != nil { + log.Fatalf("Ungültiger Wert für DELETE_AFTER_MINUTES: %v", err) + } stationIDs := strings.Split(os.Getenv("STATION_IDS"), ",") db, err := sql.Open("mysql", dbDSN) @@ -78,6 +82,7 @@ func main() { savePosition(db, dep) } } + deleteOldEntries(db, deleteAfter) time.Sleep(1 * time.Minute) } } @@ -146,3 +151,14 @@ func savePosition(db *sql.DB, dep Departure) { log.Printf("Fehler bei der Überprüfung des existierenden Eintrags: %v\n", err) } } + +func deleteOldEntries(db *sql.DB, deleteAfterMinutes int) { + deleteTime := time.Now().Add(time.Duration(-deleteAfterMinutes) * time.Minute) + result, err := db.Exec("DELETE FROM trips WHERE timestamp < ?", deleteTime) + if err != nil { + log.Printf("Fehler beim Löschen alter Einträge: %v\n", err) + return + } + rowsAffected, _ := result.RowsAffected() + log.Printf("%d alte Einträge gelöscht\n", rowsAffected) +}