Das Problem mit den Metriken wurde behoben, da veraltete Einträge von Statusen verwendet wurde. Alte Einträge werden nun einfach übersprungen

This commit is contained in:
Simon Rieger 2025-03-11 14:45:06 +01:00
parent 4aefb78dd1
commit 7f1e7e5e8c
2 changed files with 43 additions and 9 deletions

View file

@ -5,7 +5,7 @@ services:
build:
context: go/.
dockerfile: Dockerfile
no_cache: true
#no_cache: true
#ports:
# - "8080:8080" # Exponiere Port 8080 für Prometheus-Scraping
restart: always # Neustart bei Fehlern oder Updates

View file

@ -80,6 +80,7 @@ var (
},
[]string{"username"},
)
existingTrips = map[string]bool{}
)
func fetchStatuses(username string) (*StatusResponse, error) {
@ -184,6 +185,30 @@ func updateMetricsForUser(username string) {
return
}
// Bereinige die existingTrips-Map
for key := range existingTrips {
found := false
for _, trip := range statusData.Data {
tripType := "unknown"
switch trip.Train.TripType {
case 0:
tripType = "personal"
case 1:
tripType = "business"
case 2:
tripType = "commute"
}
tripKey := fmt.Sprintf("%s_%s_%s_%s_%s_%s", username, trip.Train.LineName, trip.Train.Origin.Name, trip.Train.Destination.Name, trip.Train.Category, tripType)
if key == tripKey {
found = true
break
}
}
if !found {
delete(existingTrips, key)
}
}
for _, trip := range statusData.Data {
active := isTrainActive(trip.Train.Origin.DepartureReal, trip.Train.Destination.ArrivalReal)
@ -197,20 +222,29 @@ func updateMetricsForUser(username string) {
tripType = "commute"
}
currentTrainStatuses.WithLabelValues(
key := fmt.Sprintf("%s_%s_%s_%s_%s_%s", username, trip.Train.LineName, trip.Train.Origin.Name, trip.Train.Destination.Name, trip.Train.Category, tripType)
if existingTrips[key] {
log.Printf("Fahrt %s für Benutzer '%s' bereits vorhanden, überspringe...\n", trip.Train.LineName, username)
continue
}
metric := currentTrainStatuses.WithLabelValues(
username,
trip.Train.LineName,
trip.Train.Origin.Name,
trip.Train.Destination.Name,
trip.Train.Category,
tripType,
).Set(func() float64 {
if active {
return 1
} else {
return 0
}
}())
)
if active {
metric.Set(1)
} else {
metric.Set(0)
}
existingTrips[key] = true
if active {
log.Printf("Aktive Verbindung für Benutzer '%s':\n", username)