Skip to content

Commit 1ca85eb

Browse files
committed
update handling
1 parent 02047c3 commit 1ca85eb

File tree

1 file changed

+75
-10
lines changed

1 file changed

+75
-10
lines changed

main.go

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"syscall"
1717
"time"
1818
"encoding/json"
19-
19+
"math"
2020
"github.com/aws/aws-sdk-go/aws"
2121
"github.com/aws/aws-sdk-go/aws/credentials"
2222
"github.com/aws/aws-sdk-go/aws/session"
@@ -648,6 +648,54 @@ func insertTelemetryData(db *sql.DB, vin string, datum *protos.Datum, createdAt
648648
return nil
649649
}
650650

651+
func mapCruiseStateString(value string) float64 {
652+
switch value {
653+
case "Unknown":
654+
return 0
655+
case "Off":
656+
return 1
657+
case "Standby":
658+
return 2
659+
case "On":
660+
return 3
661+
case "Standstill":
662+
return 4
663+
case "Override":
664+
return 5
665+
case "Fault":
666+
return 6
667+
case "PreFault":
668+
return 7
669+
case "PreCancel":
670+
return 8
671+
default:
672+
log.Printf("Unhandled CruiseState string value '%s'", value)
673+
return 0 // Default to Unknown
674+
}
675+
}
676+
677+
func mapChargingStateString(value string) float64 {
678+
switch value {
679+
case "ChargeStateUnknown":
680+
return 0
681+
case "ChargeStateDisconnected":
682+
return 1
683+
case "ChargeStateNoPower":
684+
return 2
685+
case "ChargeStateStarting":
686+
return 3
687+
case "ChargeStateCharging":
688+
return 4
689+
case "ChargeStateComplete":
690+
return 5
691+
case "ChargeStateStopped":
692+
return 6
693+
default:
694+
log.Printf("Unhandled ChargingState string value '%s'", value)
695+
return 0 // Default to Unknown
696+
}
697+
}
698+
651699
// processValue handles different types of Protobuf values and updates Prometheus metrics
652700
func processValue(datum *protos.Datum, service *Service, vin string, createdAt time.Time) {
653701
fieldName := datum.Key.String()
@@ -662,7 +710,17 @@ func processValue(datum *protos.Datum, service *Service, vin string, createdAt t
662710
// Update Prometheus metrics
663711
switch v := datum.Value.Value.(type) {
664712
case *protos.Value_StringValue:
665-
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(1)
713+
// Handle fields that should be mapped from string to numeric values
714+
if fieldName == "CruiseState" {
715+
cruiseStateValue := mapCruiseStateString(v.StringValue)
716+
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(cruiseStateValue)
717+
} else if fieldName == "ChargingState" {
718+
chargingStateValue := mapChargingStateString(v.StringValue)
719+
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(chargingStateValue)
720+
} else {
721+
// For other string values, you might set a label or an info metric
722+
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(1)
723+
}
666724
case *protos.Value_IntValue:
667725
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(float64(v.IntValue))
668726
case *protos.Value_LongValue:
@@ -681,9 +739,6 @@ func processValue(datum *protos.Datum, service *Service, vin string, createdAt t
681739
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(float64(v.ChargingValue))
682740
case *protos.Value_ShiftStateValue:
683741
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(float64(v.ShiftStateValue))
684-
case *protos.Value_Invalid:
685-
log.Printf("Invalid value received for field '%s', setting as NaN", fieldName)
686-
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(0)
687742
case *protos.Value_LaneAssistLevelValue:
688743
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(float64(v.LaneAssistLevelValue))
689744
case *protos.Value_ScheduledChargingModeValue:
@@ -704,9 +759,6 @@ func processValue(datum *protos.Datum, service *Service, vin string, createdAt t
704759
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(float64(v.ChargePortLatchValue))
705760
case *protos.Value_CruiseStateValue:
706761
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(float64(v.CruiseStateValue))
707-
case *protos.Value_DoorValue:
708-
// Assuming DoorValue is a complex type, handle it accordingly
709-
// Example: service.PrometheusMetrics.Gauge.WithLabelValues(fieldName+"_driver_front", vin).Set(boolToFloat64(v.DoorValue.DriverFront))
710762
case *protos.Value_DriveInverterStateValue:
711763
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(float64(v.DriveInverterStateValue))
712764
case *protos.Value_HvilStatusValue:
@@ -726,10 +778,23 @@ func processValue(datum *protos.Datum, service *Service, vin string, createdAt t
726778
case *protos.Value_TrailerAirStatusValue:
727779
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(float64(v.TrailerAirStatusValue))
728780
case *protos.Value_TimeValue:
729-
// Assuming TimeValue is a complex type, handle it accordingly
730-
// Example: service.PrometheusMetrics.Gauge.WithLabelValues(fieldName+"_hour", vin).Set(float64(v.TimeValue.Hour))
781+
// Since TimeValue is a complex type, you can break it down
782+
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName+"_hour", vin).Set(float64(v.TimeValue.Hour))
783+
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName+"_minute", vin).Set(float64(v.TimeValue.Minute))
784+
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName+"_second", vin).Set(float64(v.TimeValue.Second))
731785
case *protos.Value_DetailedChargeStateValue:
732786
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(float64(v.DetailedChargeStateValue))
787+
case *protos.Value_DoorValue:
788+
// Doors is a complex type; handle each door status separately
789+
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName+"_driver_front", vin).Set(boolToFloat64(v.DoorValue.DriverFront))
790+
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName+"_passenger_front", vin).Set(boolToFloat64(v.DoorValue.PassengerFront))
791+
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName+"_driver_rear", vin).Set(boolToFloat64(v.DoorValue.DriverRear))
792+
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName+"_passenger_rear", vin).Set(boolToFloat64(v.DoorValue.PassengerRear))
793+
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName+"_trunk_front", vin).Set(boolToFloat64(v.DoorValue.TrunkFront))
794+
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName+"_trunk_rear", vin).Set(boolToFloat64(v.DoorValue.TrunkRear))
795+
case *protos.Value_Invalid:
796+
log.Printf("Invalid value received for field '%s', setting as NaN", fieldName)
797+
service.PrometheusMetrics.Gauge.WithLabelValues(fieldName, vin).Set(math.NaN())
733798
default:
734799
log.Printf("Unhandled value type for field '%s'", fieldName)
735800
}

0 commit comments

Comments
 (0)