refactor(go): default to go vehicle topics
This commit is contained in:
@@ -22,6 +22,7 @@ import (
|
|||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/observability"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/observability"
|
||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/protocol/gb32960"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/protocol/gb32960"
|
||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/protocol/jt808"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/protocol/jt808"
|
||||||
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/topics"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -192,11 +193,11 @@ func buildSink(ctx context.Context, logger *slog.Logger) (eventbus.Sink, error)
|
|||||||
sink, err := eventbus.NewKafkaSink(eventbus.KafkaConfig{
|
sink, err := eventbus.NewKafkaSink(eventbus.KafkaConfig{
|
||||||
Brokers: brokers,
|
Brokers: brokers,
|
||||||
RawTopics: map[envelope.Protocol]string{
|
RawTopics: map[envelope.Protocol]string{
|
||||||
envelope.ProtocolGB32960: env("KAFKA_TOPIC_GB32960_RAW", "vehicle.raw.gb32960.v1"),
|
envelope.ProtocolGB32960: env("KAFKA_TOPIC_GB32960_RAW", topics.RawGB32960),
|
||||||
envelope.ProtocolJT808: env("KAFKA_TOPIC_JT808_RAW", "vehicle.raw.jt808.v1"),
|
envelope.ProtocolJT808: env("KAFKA_TOPIC_JT808_RAW", topics.RawJT808),
|
||||||
envelope.ProtocolYutongMQTT: env("KAFKA_TOPIC_YUTONG_MQTT_RAW", "vehicle.raw.yutong-mqtt.v1"),
|
envelope.ProtocolYutongMQTT: env("KAFKA_TOPIC_YUTONG_MQTT_RAW", topics.RawYutongMQTT),
|
||||||
},
|
},
|
||||||
UnifiedTopic: env("KAFKA_TOPIC_UNIFIED", "vehicle.event.unified.v1"),
|
UnifiedTopic: env("KAFKA_TOPIC_UNIFIED", topics.Unified),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -239,11 +240,11 @@ func natsSinkConfigFromEnv() eventbus.NATSConfig {
|
|||||||
URL: env("NATS_URL", ""),
|
URL: env("NATS_URL", ""),
|
||||||
Name: env("NATS_CLIENT_NAME", "lingniu-vehicle-gateway"),
|
Name: env("NATS_CLIENT_NAME", "lingniu-vehicle-gateway"),
|
||||||
RawSubjects: map[envelope.Protocol]string{
|
RawSubjects: map[envelope.Protocol]string{
|
||||||
envelope.ProtocolGB32960: env("NATS_SUBJECT_GB32960_RAW", env("KAFKA_TOPIC_GB32960_RAW", "vehicle.raw.gb32960.v1")),
|
envelope.ProtocolGB32960: env("NATS_SUBJECT_GB32960_RAW", env("KAFKA_TOPIC_GB32960_RAW", topics.RawGB32960)),
|
||||||
envelope.ProtocolJT808: env("NATS_SUBJECT_JT808_RAW", env("KAFKA_TOPIC_JT808_RAW", "vehicle.raw.jt808.v1")),
|
envelope.ProtocolJT808: env("NATS_SUBJECT_JT808_RAW", env("KAFKA_TOPIC_JT808_RAW", topics.RawJT808)),
|
||||||
envelope.ProtocolYutongMQTT: env("NATS_SUBJECT_YUTONG_MQTT_RAW", env("KAFKA_TOPIC_YUTONG_MQTT_RAW", "vehicle.raw.yutong-mqtt.v1")),
|
envelope.ProtocolYutongMQTT: env("NATS_SUBJECT_YUTONG_MQTT_RAW", env("KAFKA_TOPIC_YUTONG_MQTT_RAW", topics.RawYutongMQTT)),
|
||||||
},
|
},
|
||||||
UnifiedSubject: env("NATS_SUBJECT_UNIFIED", env("KAFKA_TOPIC_UNIFIED", "vehicle.event.unified.v1")),
|
UnifiedSubject: env("NATS_SUBJECT_UNIFIED", env("KAFKA_TOPIC_UNIFIED", topics.Unified)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,3 +31,22 @@ func TestNATSSinkConfigFromEnvUsesExplicitSubjects(t *testing.T) {
|
|||||||
t.Fatalf("unified subject = %q, want %q", got, want)
|
t.Fatalf("unified subject = %q, want %q", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNATSSinkConfigFromEnvDefaultsToGoSubjects(t *testing.T) {
|
||||||
|
t.Setenv("NATS_URL", "nats://172.17.111.56:4222")
|
||||||
|
|
||||||
|
cfg := natsSinkConfigFromEnv()
|
||||||
|
|
||||||
|
if got, want := cfg.RawSubjects[envelope.ProtocolGB32960], "vehicle.raw.go.gb32960.v1"; got != want {
|
||||||
|
t.Fatalf("gb32960 subject = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
if got, want := cfg.RawSubjects[envelope.ProtocolJT808], "vehicle.raw.go.jt808.v1"; got != want {
|
||||||
|
t.Fatalf("jt808 subject = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
if got, want := cfg.RawSubjects[envelope.ProtocolYutongMQTT], "vehicle.raw.go.yutong-mqtt.v1"; got != want {
|
||||||
|
t.Fatalf("yutong subject = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
if got, want := cfg.UnifiedSubject, "vehicle.event.go.unified.v1"; got != want {
|
||||||
|
t.Fatalf("unified subject = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/history"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/history"
|
||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/metrics"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/metrics"
|
||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/observability"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/observability"
|
||||||
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/topics"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -143,7 +144,7 @@ type config struct {
|
|||||||
func loadConfig() config {
|
func loadConfig() config {
|
||||||
return config{
|
return config{
|
||||||
KafkaBrokers: splitCSV(env("KAFKA_BROKERS", "127.0.0.1:9092")),
|
KafkaBrokers: splitCSV(env("KAFKA_BROKERS", "127.0.0.1:9092")),
|
||||||
KafkaTopics: splitCSV(env("KAFKA_TOPICS", "vehicle.raw.gb32960.v1,vehicle.raw.jt808.v1,vehicle.raw.yutong-mqtt.v1")),
|
KafkaTopics: splitCSV(env("KAFKA_TOPICS", strings.Join([]string{topics.RawGB32960, topics.RawJT808, topics.RawYutongMQTT}, ","))),
|
||||||
KafkaGroup: env("KAFKA_GROUP", "go-history-writer"),
|
KafkaGroup: env("KAFKA_GROUP", "go-history-writer"),
|
||||||
TDengineDriver: env("TDENGINE_DRIVER", "taosWS"),
|
TDengineDriver: env("TDENGINE_DRIVER", "taosWS"),
|
||||||
TDengineDSN: env("TDENGINE_DSN", ""),
|
TDengineDSN: env("TDENGINE_DSN", ""),
|
||||||
|
|||||||
@@ -74,6 +74,16 @@ func TestProcessHistoryMessageRecordsMetrics(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadConfigDefaultsToGoRawTopics(t *testing.T) {
|
||||||
|
cfg := loadConfig()
|
||||||
|
|
||||||
|
got := strings.Join(cfg.KafkaTopics, ",")
|
||||||
|
want := "vehicle.raw.go.gb32960.v1,vehicle.raw.go.jt808.v1,vehicle.raw.go.yutong-mqtt.v1"
|
||||||
|
if got != want {
|
||||||
|
t.Fatalf("KafkaTopics = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type contextCheckingHistoryAppender struct {
|
type contextCheckingHistoryAppender struct {
|
||||||
ctxErr error
|
ctxErr error
|
||||||
count int
|
count int
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import (
|
|||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/health"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/health"
|
||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/metrics"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/metrics"
|
||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/observability"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/observability"
|
||||||
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/topics"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -101,10 +102,10 @@ type config struct {
|
|||||||
|
|
||||||
func loadConfig() config {
|
func loadConfig() config {
|
||||||
route := map[string]string{
|
route := map[string]string{
|
||||||
env("NATS_SUBJECT_GB32960_RAW", env("KAFKA_TOPIC_GB32960_RAW", "vehicle.raw.gb32960.v1")): env("KAFKA_TOPIC_GB32960_RAW", "vehicle.raw.gb32960.v1"),
|
env("NATS_SUBJECT_GB32960_RAW", env("KAFKA_TOPIC_GB32960_RAW", topics.RawGB32960)): env("KAFKA_TOPIC_GB32960_RAW", topics.RawGB32960),
|
||||||
env("NATS_SUBJECT_JT808_RAW", env("KAFKA_TOPIC_JT808_RAW", "vehicle.raw.jt808.v1")): env("KAFKA_TOPIC_JT808_RAW", "vehicle.raw.jt808.v1"),
|
env("NATS_SUBJECT_JT808_RAW", env("KAFKA_TOPIC_JT808_RAW", topics.RawJT808)): env("KAFKA_TOPIC_JT808_RAW", topics.RawJT808),
|
||||||
env("NATS_SUBJECT_YUTONG_MQTT_RAW", env("KAFKA_TOPIC_YUTONG_MQTT_RAW", "vehicle.raw.yutong-mqtt.v1")): env("KAFKA_TOPIC_YUTONG_MQTT_RAW", "vehicle.raw.yutong-mqtt.v1"),
|
env("NATS_SUBJECT_YUTONG_MQTT_RAW", env("KAFKA_TOPIC_YUTONG_MQTT_RAW", topics.RawYutongMQTT)): env("KAFKA_TOPIC_YUTONG_MQTT_RAW", topics.RawYutongMQTT),
|
||||||
env("NATS_SUBJECT_UNIFIED", env("KAFKA_TOPIC_UNIFIED", "vehicle.event.unified.v1")): env("KAFKA_TOPIC_UNIFIED", "vehicle.event.unified.v1"),
|
env("NATS_SUBJECT_UNIFIED", env("KAFKA_TOPIC_UNIFIED", topics.Unified)): env("KAFKA_TOPIC_UNIFIED", topics.Unified),
|
||||||
}
|
}
|
||||||
return config{
|
return config{
|
||||||
NATSURL: env("NATS_URL", "nats://127.0.0.1:4222"),
|
NATSURL: env("NATS_URL", "nats://127.0.0.1:4222"),
|
||||||
|
|||||||
@@ -113,6 +113,25 @@ func TestRecordNATSConsumerInfoMetrics(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadConfigDefaultsToGoSubjectRoutes(t *testing.T) {
|
||||||
|
cfg := loadConfig()
|
||||||
|
|
||||||
|
want := map[string]string{
|
||||||
|
"vehicle.raw.go.gb32960.v1": "vehicle.raw.go.gb32960.v1",
|
||||||
|
"vehicle.raw.go.jt808.v1": "vehicle.raw.go.jt808.v1",
|
||||||
|
"vehicle.raw.go.yutong-mqtt.v1": "vehicle.raw.go.yutong-mqtt.v1",
|
||||||
|
"vehicle.event.go.unified.v1": "vehicle.event.go.unified.v1",
|
||||||
|
}
|
||||||
|
if len(cfg.Route) != len(want) {
|
||||||
|
t.Fatalf("route len = %d, want %d: %#v", len(cfg.Route), len(want), cfg.Route)
|
||||||
|
}
|
||||||
|
for subject, topic := range want {
|
||||||
|
if got := cfg.Route[subject]; got != topic {
|
||||||
|
t.Fatalf("route[%q] = %q, want %q; route=%#v", subject, got, topic, cfg.Route)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type recordingBridgeWriter struct {
|
type recordingBridgeWriter struct {
|
||||||
messages []kafka.Message
|
messages []kafka.Message
|
||||||
err error
|
err error
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import (
|
|||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/observability"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/observability"
|
||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/realtime"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/realtime"
|
||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/stats"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/stats"
|
||||||
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/topics"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -163,15 +164,16 @@ func consumeKafka(ctx context.Context, logger interface {
|
|||||||
Error(string, ...any)
|
Error(string, ...any)
|
||||||
Warn(string, ...any)
|
Warn(string, ...any)
|
||||||
}, registry *metrics.Registry, updater realtimeUpdater, brokers []string) {
|
}, registry *metrics.Registry, updater realtimeUpdater, brokers []string) {
|
||||||
|
kafkaTopics := kafkaTopicsFromEnv()
|
||||||
reader := kafka.NewReader(kafka.ReaderConfig{
|
reader := kafka.NewReader(kafka.ReaderConfig{
|
||||||
Brokers: brokers,
|
Brokers: brokers,
|
||||||
GroupID: env("KAFKA_GROUP", "go-realtime-api"),
|
GroupID: env("KAFKA_GROUP", "go-realtime-api"),
|
||||||
GroupTopics: splitCSV(env("KAFKA_TOPICS", "vehicle.event.unified.v1")),
|
GroupTopics: kafkaTopics,
|
||||||
MinBytes: 1,
|
MinBytes: 1,
|
||||||
MaxBytes: 10e6,
|
MaxBytes: 10e6,
|
||||||
})
|
})
|
||||||
defer reader.Close()
|
defer reader.Close()
|
||||||
logger.Info("realtime kafka consumer started", "topics", env("KAFKA_TOPICS", "vehicle.event.unified.v1"))
|
logger.Info("realtime kafka consumer started", "topics", strings.Join(kafkaTopics, ","))
|
||||||
for {
|
for {
|
||||||
message, err := reader.FetchMessage(ctx)
|
message, err := reader.FetchMessage(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -185,6 +187,10 @@ func consumeKafka(ctx context.Context, logger interface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func kafkaTopicsFromEnv() []string {
|
||||||
|
return splitCSV(env("KAFKA_TOPICS", topics.Unified))
|
||||||
|
}
|
||||||
|
|
||||||
const kafkaMessageOperationTimeout = 30 * time.Second
|
const kafkaMessageOperationTimeout = 30 * time.Second
|
||||||
|
|
||||||
type realtimeUpdater interface {
|
type realtimeUpdater interface {
|
||||||
|
|||||||
@@ -95,6 +95,14 @@ func TestCompositeRealtimeUpdaterUpdatesBothStores(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestKafkaTopicsFromEnvDefaultsToGoUnifiedTopic(t *testing.T) {
|
||||||
|
got := strings.Join(kafkaTopicsFromEnv(), ",")
|
||||||
|
want := "vehicle.event.go.unified.v1"
|
||||||
|
if got != want {
|
||||||
|
t.Fatalf("topics = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type contextCheckingRealtimeUpdater struct {
|
type contextCheckingRealtimeUpdater struct {
|
||||||
ctxErr error
|
ctxErr error
|
||||||
count int
|
count int
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/metrics"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/metrics"
|
||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/observability"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/observability"
|
||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/stats"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/stats"
|
||||||
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/topics"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -142,7 +143,7 @@ func loadConfig() config {
|
|||||||
}
|
}
|
||||||
return config{
|
return config{
|
||||||
KafkaBrokers: splitCSV(env("KAFKA_BROKERS", "127.0.0.1:9092")),
|
KafkaBrokers: splitCSV(env("KAFKA_BROKERS", "127.0.0.1:9092")),
|
||||||
KafkaTopics: splitCSV(env("KAFKA_TOPICS", "vehicle.raw.gb32960.v1,vehicle.raw.jt808.v1")),
|
KafkaTopics: splitCSV(env("KAFKA_TOPICS", strings.Join([]string{topics.RawGB32960, topics.RawJT808, topics.RawYutongMQTT}, ","))),
|
||||||
KafkaGroup: env("KAFKA_GROUP", "go-stat-writer"),
|
KafkaGroup: env("KAFKA_GROUP", "go-stat-writer"),
|
||||||
MySQLDSN: env("MYSQL_DSN", ""),
|
MySQLDSN: env("MYSQL_DSN", ""),
|
||||||
EnsureSchema: env("MYSQL_ENSURE_SCHEMA", "true") != "false",
|
EnsureSchema: env("MYSQL_ENSURE_SCHEMA", "true") != "false",
|
||||||
|
|||||||
@@ -74,6 +74,16 @@ func TestProcessStatMessageRecordsMetrics(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadConfigDefaultsToGoRawTopics(t *testing.T) {
|
||||||
|
cfg := loadConfig()
|
||||||
|
|
||||||
|
got := strings.Join(cfg.KafkaTopics, ",")
|
||||||
|
want := "vehicle.raw.go.gb32960.v1,vehicle.raw.go.jt808.v1,vehicle.raw.go.yutong-mqtt.v1"
|
||||||
|
if got != want {
|
||||||
|
t.Fatalf("KafkaTopics = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type contextCheckingStatAppender struct {
|
type contextCheckingStatAppender struct {
|
||||||
ctxErr error
|
ctxErr error
|
||||||
count int
|
count int
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/segmentio/kafka-go"
|
"github.com/segmentio/kafka-go"
|
||||||
|
|
||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
|
||||||
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/topics"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Sink interface {
|
type Sink interface {
|
||||||
@@ -48,9 +49,9 @@ func NewKafkaSink(cfg KafkaConfig) (*KafkaSink, error) {
|
|||||||
|
|
||||||
func newKafkaSinkWithWriter(writer kafkaWriter, cfg KafkaConfig) *KafkaSink {
|
func newKafkaSinkWithWriter(writer kafkaWriter, cfg KafkaConfig) *KafkaSink {
|
||||||
rawTopics := map[envelope.Protocol]string{
|
rawTopics := map[envelope.Protocol]string{
|
||||||
envelope.ProtocolGB32960: "vehicle.raw.gb32960.v1",
|
envelope.ProtocolGB32960: topics.RawGB32960,
|
||||||
envelope.ProtocolJT808: "vehicle.raw.jt808.v1",
|
envelope.ProtocolJT808: topics.RawJT808,
|
||||||
envelope.ProtocolYutongMQTT: "vehicle.raw.yutong-mqtt.v1",
|
envelope.ProtocolYutongMQTT: topics.RawYutongMQTT,
|
||||||
}
|
}
|
||||||
for protocol, topic := range cfg.RawTopics {
|
for protocol, topic := range cfg.RawTopics {
|
||||||
if topic != "" {
|
if topic != "" {
|
||||||
@@ -59,7 +60,7 @@ func newKafkaSinkWithWriter(writer kafkaWriter, cfg KafkaConfig) *KafkaSink {
|
|||||||
}
|
}
|
||||||
unifiedTopic := cfg.UnifiedTopic
|
unifiedTopic := cfg.UnifiedTopic
|
||||||
if unifiedTopic == "" {
|
if unifiedTopic == "" {
|
||||||
unifiedTopic = "vehicle.event.unified.v1"
|
unifiedTopic = topics.Unified
|
||||||
}
|
}
|
||||||
return &KafkaSink{writer: writer, rawTopics: rawTopics, unifiedTopic: unifiedTopic}
|
return &KafkaSink{writer: writer, rawTopics: rawTopics, unifiedTopic: unifiedTopic}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ func TestKafkaSinkRoutesRawTopics(t *testing.T) {
|
|||||||
t.Fatalf("messages = %d", len(writer.messages))
|
t.Fatalf("messages = %d", len(writer.messages))
|
||||||
}
|
}
|
||||||
msg := writer.messages[0]
|
msg := writer.messages[0]
|
||||||
if msg.Topic != "vehicle.raw.jt808.v1" {
|
if got, want := msg.Topic, "vehicle.raw.go.jt808.v1"; got != want {
|
||||||
t.Fatalf("topic = %q", msg.Topic)
|
t.Fatalf("topic = %q, want %q", got, want)
|
||||||
}
|
}
|
||||||
if string(msg.Key) != "JT808:13307795425" {
|
if string(msg.Key) != "JT808:13307795425" {
|
||||||
t.Fatalf("key = %q", string(msg.Key))
|
t.Fatalf("key = %q", string(msg.Key))
|
||||||
@@ -82,7 +82,7 @@ func TestKafkaSinkPublishesDurableRecordsInSingleWriterCall(t *testing.T) {
|
|||||||
if len(writer.messages) != 2 {
|
if len(writer.messages) != 2 {
|
||||||
t.Fatalf("messages = %d, want 2", len(writer.messages))
|
t.Fatalf("messages = %d, want 2", len(writer.messages))
|
||||||
}
|
}
|
||||||
if writer.messages[0].Topic != "vehicle.raw.jt808.v1" || writer.messages[1].Topic != "custom.unified" {
|
if writer.messages[0].Topic != "vehicle.raw.go.jt808.v1" || writer.messages[1].Topic != "custom.unified" {
|
||||||
t.Fatalf("topics = %q, %q", writer.messages[0].Topic, writer.messages[1].Topic)
|
t.Fatalf("topics = %q, %q", writer.messages[0].Topic, writer.messages[1].Topic)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
|
|
||||||
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
|
||||||
|
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/topics"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NATSConfig struct {
|
type NATSConfig struct {
|
||||||
@@ -55,9 +56,9 @@ func NewNATSSink(cfg NATSConfig) (*NATSSink, error) {
|
|||||||
|
|
||||||
func newNATSSinkWithPublisher(publisher natsPublisher, cfg NATSConfig) *NATSSink {
|
func newNATSSinkWithPublisher(publisher natsPublisher, cfg NATSConfig) *NATSSink {
|
||||||
rawSubjects := map[envelope.Protocol]string{
|
rawSubjects := map[envelope.Protocol]string{
|
||||||
envelope.ProtocolGB32960: "vehicle.raw.gb32960.v1",
|
envelope.ProtocolGB32960: topics.RawGB32960,
|
||||||
envelope.ProtocolJT808: "vehicle.raw.jt808.v1",
|
envelope.ProtocolJT808: topics.RawJT808,
|
||||||
envelope.ProtocolYutongMQTT: "vehicle.raw.yutong-mqtt.v1",
|
envelope.ProtocolYutongMQTT: topics.RawYutongMQTT,
|
||||||
}
|
}
|
||||||
for protocol, subject := range cfg.RawSubjects {
|
for protocol, subject := range cfg.RawSubjects {
|
||||||
if subject != "" {
|
if subject != "" {
|
||||||
@@ -66,7 +67,7 @@ func newNATSSinkWithPublisher(publisher natsPublisher, cfg NATSConfig) *NATSSink
|
|||||||
}
|
}
|
||||||
unifiedSubject := cfg.UnifiedSubject
|
unifiedSubject := cfg.UnifiedSubject
|
||||||
if unifiedSubject == "" {
|
if unifiedSubject == "" {
|
||||||
unifiedSubject = "vehicle.event.unified.v1"
|
unifiedSubject = topics.Unified
|
||||||
}
|
}
|
||||||
return &NATSSink{
|
return &NATSSink{
|
||||||
publisher: publisher,
|
publisher: publisher,
|
||||||
|
|||||||
@@ -40,6 +40,23 @@ func TestNATSSinkRoutesRawAndUnifiedSubjects(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNATSSinkDefaultsToGoRawSubjects(t *testing.T) {
|
||||||
|
publisher := &recordingNATSPublisher{}
|
||||||
|
sink := newNATSSinkWithPublisher(publisher, NATSConfig{})
|
||||||
|
|
||||||
|
err := sink.PublishRaw(context.Background(), envelope.FrameEnvelope{
|
||||||
|
Protocol: envelope.ProtocolJT808,
|
||||||
|
Phone: "13307795425",
|
||||||
|
ReceivedAtMS: 1782918600000,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("PublishRaw() error = %v", err)
|
||||||
|
}
|
||||||
|
if got, want := publisher.messages[0].subject, "vehicle.raw.go.jt808.v1"; got != want {
|
||||||
|
t.Fatalf("subject = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type recordingNATSPublisher struct {
|
type recordingNATSPublisher struct {
|
||||||
messages []recordedNATSMessage
|
messages []recordedNATSMessage
|
||||||
}
|
}
|
||||||
|
|||||||
8
go/vehicle-gateway/internal/topics/topics.go
Normal file
8
go/vehicle-gateway/internal/topics/topics.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package topics
|
||||||
|
|
||||||
|
const (
|
||||||
|
RawGB32960 = "vehicle.raw.go.gb32960.v1"
|
||||||
|
RawJT808 = "vehicle.raw.go.jt808.v1"
|
||||||
|
RawYutongMQTT = "vehicle.raw.go.yutong-mqtt.v1"
|
||||||
|
Unified = "vehicle.event.go.unified.v1"
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user