Optimiere Code
This commit is contained in:
parent
7f1e7e5e8c
commit
152e9eb6c8
1 changed files with 23 additions and 26 deletions
49
go/main.go
49
go/main.go
|
@ -172,6 +172,19 @@ func isTrainActive(departureTimeStr, arrivalTimeStr string) bool {
|
||||||
return now.After(departureTime) && now.Before(arrivalTime)
|
return now.After(departureTime) && now.Before(arrivalTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getTripType(tripType int) string {
|
||||||
|
switch tripType {
|
||||||
|
case 0:
|
||||||
|
return "personal"
|
||||||
|
case 1:
|
||||||
|
return "business"
|
||||||
|
case 2:
|
||||||
|
return "commute"
|
||||||
|
default:
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func updateMetricsForUser(username string) {
|
func updateMetricsForUser(username string) {
|
||||||
statusData, err := fetchStatuses(username)
|
statusData, err := fetchStatuses(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -189,15 +202,7 @@ func updateMetricsForUser(username string) {
|
||||||
for key := range existingTrips {
|
for key := range existingTrips {
|
||||||
found := false
|
found := false
|
||||||
for _, trip := range statusData.Data {
|
for _, trip := range statusData.Data {
|
||||||
tripType := "unknown"
|
tripType := getTripType(trip.Train.TripType)
|
||||||
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)
|
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 {
|
if key == tripKey {
|
||||||
found = true
|
found = true
|
||||||
|
@ -212,15 +217,7 @@ func updateMetricsForUser(username string) {
|
||||||
for _, trip := range statusData.Data {
|
for _, trip := range statusData.Data {
|
||||||
active := isTrainActive(trip.Train.Origin.DepartureReal, trip.Train.Destination.ArrivalReal)
|
active := isTrainActive(trip.Train.Origin.DepartureReal, trip.Train.Destination.ArrivalReal)
|
||||||
|
|
||||||
tripType := "unknown"
|
tripType := getTripType(trip.Train.TripType)
|
||||||
switch trip.Train.TripType {
|
|
||||||
case 0:
|
|
||||||
tripType = "personal"
|
|
||||||
case 1:
|
|
||||||
tripType = "business"
|
|
||||||
case 2:
|
|
||||||
tripType = "commute"
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
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)
|
||||||
|
|
||||||
|
@ -229,20 +226,20 @@ func updateMetricsForUser(username string) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
metric := currentTrainStatuses.WithLabelValues(
|
currentTrainStatuses.WithLabelValues(
|
||||||
username,
|
username,
|
||||||
trip.Train.LineName,
|
trip.Train.LineName,
|
||||||
trip.Train.Origin.Name,
|
trip.Train.Origin.Name,
|
||||||
trip.Train.Destination.Name,
|
trip.Train.Destination.Name,
|
||||||
trip.Train.Category,
|
trip.Train.Category,
|
||||||
tripType,
|
tripType,
|
||||||
)
|
).Set(func() float64 {
|
||||||
|
if active {
|
||||||
if active {
|
return 1
|
||||||
metric.Set(1)
|
} else {
|
||||||
} else {
|
return 0
|
||||||
metric.Set(0)
|
}
|
||||||
}
|
}())
|
||||||
|
|
||||||
existingTrips[key] = true
|
existingTrips[key] = true
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue