add timestamp to proto
All checks were successful
Build and Publish / check-chart (push) Successful in 25s
Build and Publish / helm-release (push) Has been skipped
Build and Publish / release (push) Successful in 3m30s

This commit is contained in:
2025-03-23 12:15:18 -04:00
parent fc2d05e957
commit f079d106a1
4 changed files with 85 additions and 52 deletions

View File

@ -2,12 +2,16 @@ package grpc
import (
"slices"
"time"
"google.golang.org/protobuf/types/known/timestamppb"
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/util"
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
)
// Converts a slice of weather.WeatherUpdate to proto structs
func UpdatesToPbUpdates(u []*weather.WeatherUpdate) []*pb.WeatherUpdate {
updates := make([]*pb.WeatherUpdate, len(u))
for i, update := range u {
@ -16,7 +20,16 @@ func UpdatesToPbUpdates(u []*weather.WeatherUpdate) []*pb.WeatherUpdate {
return updates
}
// Maps a weather.WeatherUpdate to the proto version.
//
// This should be auto-generated code, but as the proto should be
// stable, it is probably not worth the effort.
func UpdateToPbUpdate(u *weather.WeatherUpdate) *pb.WeatherUpdate {
if u.DateUTC == nil {
now := time.Now()
u.DateUTC = &now
}
return &pb.WeatherUpdate{
StationName: u.StationConfig.Name,
StationType: util.DerefStr(u.StationType),
@ -45,6 +58,7 @@ func UpdateToPbUpdate(u *weather.WeatherUpdate) *pb.WeatherUpdate {
DewPointF: u.DewPointF,
WindChillF: u.WindChillF,
TempHumiditySensors: thSensorsToPbSensors(u.TempHumiditySensors),
UpdateTimestamp: timestamppb.New(*u.DateUTC),
}
}

View File

@ -5,6 +5,7 @@ import (
"testing"
"time"
"google.golang.org/protobuf/types/known/timestamppb"
"k8s.io/utils/ptr"
pb "gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/api/v1alpha1/weather"
@ -12,8 +13,10 @@ import (
"gitea.libretechconsulting.com/rmcguire/ambient-local-exporter/pkg/weather"
)
var now = time.Now()
var mockUpdate = &weather.WeatherUpdate{
DateUTC: &time.Time{},
DateUTC: &now,
StationConfig: &config.WeatherStation{
Name: "50W",
Equipment: "WS-5000",
@ -100,6 +103,7 @@ func TestUpdateToPbUpdate(t *testing.T) {
Humidity: nil,
},
},
UpdateTimestamp: timestamppb.New(now),
},
},
}