# JT808 Kafka Streaming Mileage Plan

## Current Goal

Consume JT808 Kafka location events and write the derived daily mileage into the common vehicle-stat metric repository.

## Current Design

- Source topic: `vehicle.event.jt808.v1`
- Runtime app: `vehicle-analytics-app`
- Runtime state: none outside `vehicle_stat_metric`
- Metric output: `VehicleStatRepository.recordDailyMileageSample(...)`
- Production metric storage: JDBC/MySQL `vehicle_stat_metric`
- Date boundary: `Asia/Shanghai`
- Calculation method: `JT808_TOTAL_MILEAGE_DIFF`

JT808 daily mileage is calculated only from the GPS total mileage reported by JT808 location additional information:

```text
daily_mileage_km = max_total_mileage_km - min_total_mileage_km
```

The first valid local-day sample writes `daily_mileage_km=0.0`. Later ordered or replayed samples update the same metric row with:

```text
metric_value = max_total_mileage_km - min_total_mileage_km
metric_key = daily_mileage_km
```

The local-day minimum and maximum GPS total mileage values stay on that same metric row as calculation source columns so restarts recover from MySQL without Redis or another state table.

## Runtime Properties

```text
KAFKA_TOPIC_JT808_EVENT=vehicle.event.jt808.v1
VEHICLE_STAT_ENABLED=true
VEHICLE_STAT_JT808_MILEAGE_ENABLED=true
MYSQL_JDBC_URL=<jdbc-url>
MYSQL_USERNAME=<user>
MYSQL_PASSWORD=<password>
```

## Notes

Do not create or write a protocol-specific JT808 daily-mileage table. Do not add distance accumulation, integral calculation, Redis state, or memory state back into this path unless the mileage definition changes again.
