feat: add go realtime redis api

This commit is contained in:
lingniu
2026-07-01 21:51:13 +08:00
parent bac8864e79
commit b55b075a97
7 changed files with 595 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
package realtime
import (
"encoding/json"
"errors"
"net/http"
"strings"
"github.com/redis/go-redis/v9"
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
)
type Handler struct {
repository *Repository
}
func NewHandler(repository *Repository) *Handler {
if repository == nil {
panic("realtime repository must not be nil")
}
return &Handler{repository: repository}
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
writeError(w, http.StatusMethodNotAllowed, "method not allowed")
return
}
path := strings.Trim(strings.TrimPrefix(r.URL.Path, "/api/realtime/vehicles/"), "/")
parts := strings.Split(path, "/")
if len(parts) == 0 || strings.TrimSpace(parts[0]) == "" {
writeError(w, http.StatusNotFound, "vehicle not found")
return
}
vin := parts[0]
switch {
case len(parts) == 1:
snapshot, err := h.repository.GetMerged(r.Context(), vin)
writeResult(w, snapshot, err)
case len(parts) == 2 && parts[1] == "online":
status, err := h.repository.IsOnline(r.Context(), vin)
writeResult(w, status, err)
case len(parts) == 3 && parts[1] == "protocols":
snapshot, err := h.repository.GetProtocol(r.Context(), vin, envelope.Protocol(strings.ToUpper(parts[2])))
writeResult(w, snapshot, err)
default:
writeError(w, http.StatusNotFound, "route not found")
}
}
func writeResult(w http.ResponseWriter, value any, err error) {
if err != nil {
if errors.Is(err, redis.Nil) {
writeError(w, http.StatusNotFound, "not found")
return
}
writeError(w, http.StatusInternalServerError, err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(value)
}
func writeError(w http.ResponseWriter, status int, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(map[string]any{"error": message})
}

View File

@@ -0,0 +1,38 @@
package realtime
import (
"time"
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
)
type Snapshot struct {
VIN string `json:"vin"`
Protocol envelope.Protocol `json:"protocol,omitempty"`
EventID string `json:"event_id,omitempty"`
EventTimeMS int64 `json:"event_time_ms"`
ReceivedAtMS int64 `json:"received_at_ms"`
SourceEndpoint string `json:"source_endpoint,omitempty"`
Fields map[string]any `json:"fields,omitempty"`
FieldTimesMS map[string]int64 `json:"field_times_ms,omitempty"`
UpdatedAtMS int64 `json:"updated_at_ms"`
}
type OnlineStatus struct {
VIN string `json:"vin"`
Online bool `json:"online"`
LastSeenMS int64 `json:"last_seen_ms"`
Protocols []envelope.Protocol `json:"protocols,omitempty"`
TTLSeconds int64 `json:"ttl_seconds"`
}
type Config struct {
OnlineTTL time.Duration
}
func (c Config) ttl() time.Duration {
if c.OnlineTTL <= 0 {
return 10 * time.Minute
}
return c.OnlineTTL
}

View File

@@ -0,0 +1,198 @@
package realtime
import (
"context"
"encoding/json"
"errors"
"sort"
"strings"
"time"
"github.com/redis/go-redis/v9"
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
)
type Repository struct {
client *redis.Client
cfg Config
}
func NewRepository(client *redis.Client, cfg Config) *Repository {
if client == nil {
panic("redis client must not be nil")
}
return &Repository{client: client, cfg: cfg}
}
func (r *Repository) Update(ctx context.Context, env envelope.FrameEnvelope) error {
vin := strings.TrimSpace(env.VIN)
if vin == "" {
return nil
}
nowMS := time.Now().UnixMilli()
eventMS := env.EventTimeMS
if eventMS <= 0 {
eventMS = env.ReceivedAtMS
}
protocolSnapshot := Snapshot{
VIN: vin,
Protocol: env.Protocol,
EventID: env.StableEventID(),
EventTimeMS: eventMS,
ReceivedAtMS: env.ReceivedAtMS,
SourceEndpoint: env.SourceEndpoint,
Fields: cloneFields(env.Fields),
FieldTimesMS: fieldTimes(env.Fields, eventMS),
UpdatedAtMS: nowMS,
}
if err := r.setJSON(ctx, protocolKey(vin, env.Protocol), protocolSnapshot, r.cfg.ttl()); err != nil {
return err
}
merged, err := r.GetMerged(ctx, vin)
if err != nil && !errors.Is(err, redis.Nil) {
return err
}
if merged.VIN == "" {
merged = Snapshot{VIN: vin, Fields: map[string]any{}, FieldTimesMS: map[string]int64{}}
}
mergeFields(&merged, env.Fields, eventMS)
if eventMS >= merged.EventTimeMS {
merged.EventTimeMS = eventMS
merged.EventID = env.StableEventID()
merged.ReceivedAtMS = env.ReceivedAtMS
merged.SourceEndpoint = env.SourceEndpoint
}
merged.UpdatedAtMS = nowMS
if err := r.setJSON(ctx, mergedKey(vin), merged, r.cfg.ttl()); err != nil {
return err
}
protocols, err := r.addProtocol(ctx, vin, env.Protocol)
if err != nil {
return err
}
online := OnlineStatus{
VIN: vin,
Online: true,
LastSeenMS: env.ReceivedAtMS,
Protocols: protocols,
TTLSeconds: int64(r.cfg.ttl().Seconds()),
}
if err := r.setJSON(ctx, onlineKey(vin), online, r.cfg.ttl()); err != nil {
return err
}
return r.client.ZAdd(ctx, "vehicle:last_seen", redis.Z{Score: float64(env.ReceivedAtMS), Member: vin}).Err()
}
func (r *Repository) GetMerged(ctx context.Context, vin string) (Snapshot, error) {
return r.getSnapshot(ctx, mergedKey(vin))
}
func (r *Repository) GetProtocol(ctx context.Context, vin string, protocol envelope.Protocol) (Snapshot, error) {
return r.getSnapshot(ctx, protocolKey(vin, protocol))
}
func (r *Repository) IsOnline(ctx context.Context, vin string) (OnlineStatus, error) {
var status OnlineStatus
payload, err := r.client.Get(ctx, onlineKey(vin)).Bytes()
if err != nil {
if errors.Is(err, redis.Nil) {
return OnlineStatus{VIN: vin, Online: false}, nil
}
return status, err
}
if err := json.Unmarshal(payload, &status); err != nil {
return status, err
}
ttl := r.client.TTL(ctx, onlineKey(vin)).Val()
status.Online = ttl > 0
status.TTLSeconds = int64(ttl.Seconds())
return status, nil
}
func (r *Repository) getSnapshot(ctx context.Context, key string) (Snapshot, error) {
var snapshot Snapshot
payload, err := r.client.Get(ctx, key).Bytes()
if err != nil {
return snapshot, err
}
return snapshot, json.Unmarshal(payload, &snapshot)
}
func (r *Repository) setJSON(ctx context.Context, key string, value any, ttl time.Duration) error {
payload, err := json.Marshal(value)
if err != nil {
return err
}
return r.client.Set(ctx, key, payload, ttl).Err()
}
func (r *Repository) addProtocol(ctx context.Context, vin string, protocol envelope.Protocol) ([]envelope.Protocol, error) {
key := protocolsKey(vin)
if err := r.client.SAdd(ctx, key, string(protocol)).Err(); err != nil {
return nil, err
}
_ = r.client.Expire(ctx, key, r.cfg.ttl()).Err()
values, err := r.client.SMembers(ctx, key).Result()
if err != nil {
return nil, err
}
sort.Strings(values)
out := make([]envelope.Protocol, 0, len(values))
for _, value := range values {
out = append(out, envelope.Protocol(value))
}
return out, nil
}
func mergeFields(snapshot *Snapshot, fields map[string]any, eventMS int64) {
if snapshot.Fields == nil {
snapshot.Fields = map[string]any{}
}
if snapshot.FieldTimesMS == nil {
snapshot.FieldTimesMS = map[string]int64{}
}
for key, value := range fields {
if eventMS >= snapshot.FieldTimesMS[key] {
snapshot.Fields[key] = value
snapshot.FieldTimesMS[key] = eventMS
}
}
}
func cloneFields(fields map[string]any) map[string]any {
if len(fields) == 0 {
return map[string]any{}
}
out := make(map[string]any, len(fields))
for key, value := range fields {
out[key] = value
}
return out
}
func fieldTimes(fields map[string]any, eventMS int64) map[string]int64 {
out := make(map[string]int64, len(fields))
for key := range fields {
out[key] = eventMS
}
return out
}
func mergedKey(vin string) string {
return "vehicle:latest:" + strings.TrimSpace(vin)
}
func protocolKey(vin string, protocol envelope.Protocol) string {
return "vehicle:latest:" + strings.TrimSpace(vin) + ":" + string(protocol)
}
func onlineKey(vin string) string {
return "vehicle:online:" + strings.TrimSpace(vin)
}
func protocolsKey(vin string) string {
return "vehicle:protocols:" + strings.TrimSpace(vin)
}

View File

@@ -0,0 +1,136 @@
package realtime
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
)
func TestRepositoryUpdatesMergedAndProtocolSnapshots(t *testing.T) {
repo, closeFn := newTestRepository(t)
defer closeFn()
ctx := context.Background()
if err := repo.Update(ctx, envelope.FrameEnvelope{
Protocol: envelope.ProtocolJT808,
VIN: "VIN001",
EventTimeMS: 1000,
ReceivedAtMS: 1100,
Fields: map[string]any{
envelope.FieldLongitude: 121.1,
envelope.FieldTotalMileageKM: 10.5,
},
}); err != nil {
t.Fatalf("Update() error = %v", err)
}
if err := repo.Update(ctx, envelope.FrameEnvelope{
Protocol: envelope.ProtocolGB32960,
VIN: "VIN001",
EventTimeMS: 900,
ReceivedAtMS: 1200,
Fields: map[string]any{
envelope.FieldLongitude: 120.0,
envelope.FieldLatitude: 30.5,
},
}); err != nil {
t.Fatalf("Update() error = %v", err)
}
merged, err := repo.GetMerged(ctx, "VIN001")
if err != nil {
t.Fatalf("GetMerged() error = %v", err)
}
if merged.Fields[envelope.FieldLongitude] != 121.1 {
t.Fatalf("older longitude overwrote newer value: %#v", merged.Fields)
}
if merged.Fields[envelope.FieldLatitude] != 30.5 {
t.Fatalf("new latitude missing: %#v", merged.Fields)
}
protocol, err := repo.GetProtocol(ctx, "VIN001", envelope.ProtocolJT808)
if err != nil {
t.Fatalf("GetProtocol() error = %v", err)
}
if protocol.Fields[envelope.FieldTotalMileageKM] != 10.5 {
t.Fatalf("protocol snapshot = %#v", protocol)
}
}
func TestRepositoryOnlineStatus(t *testing.T) {
repo, closeFn := newTestRepository(t)
defer closeFn()
ctx := context.Background()
status, err := repo.IsOnline(ctx, "VIN001")
if err != nil {
t.Fatalf("IsOnline() error = %v", err)
}
if status.Online {
t.Fatal("empty vin should be offline")
}
if err := repo.Update(ctx, envelope.FrameEnvelope{
Protocol: envelope.ProtocolJT808,
VIN: "VIN001",
EventTimeMS: 1000,
ReceivedAtMS: 1100,
Fields: map[string]any{envelope.FieldSpeedKMH: 12.3},
}); err != nil {
t.Fatalf("Update() error = %v", err)
}
status, err = repo.IsOnline(ctx, "VIN001")
if err != nil {
t.Fatalf("IsOnline() error = %v", err)
}
if !status.Online || status.LastSeenMS != 1100 {
t.Fatalf("online status = %#v", status)
}
}
func TestHandlerReturnsMergedSnapshot(t *testing.T) {
repo, closeFn := newTestRepository(t)
defer closeFn()
if err := repo.Update(context.Background(), envelope.FrameEnvelope{
Protocol: envelope.ProtocolJT808,
VIN: "VIN001",
EventTimeMS: 1000,
ReceivedAtMS: 1100,
Fields: map[string]any{envelope.FieldSpeedKMH: 12.3},
}); err != nil {
t.Fatalf("Update() error = %v", err)
}
req := httptest.NewRequest(http.MethodGet, "/api/realtime/vehicles/VIN001", nil)
rec := httptest.NewRecorder()
NewHandler(repo).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d body=%s", rec.Code, rec.Body.String())
}
if !stringsContains(rec.Body.String(), `"vin":"VIN001"`) {
t.Fatalf("unexpected body: %s", rec.Body.String())
}
}
func newTestRepository(t *testing.T) (*Repository, func()) {
t.Helper()
server, err := miniredis.Run()
if err != nil {
t.Fatalf("miniredis.Run() error = %v", err)
}
client := redis.NewClient(&redis.Options{Addr: server.Addr()})
return NewRepository(client, Config{OnlineTTL: time.Minute}), func() {
_ = client.Close()
server.Close()
}
}
func stringsContains(value string, pattern string) bool {
return strings.Contains(value, pattern)
}