feat(stats): expose selected mileage source

This commit is contained in:
lingniu
2026-07-08 15:19:43 +08:00
parent 6d633aa292
commit 800ca1a8b1
2 changed files with 88 additions and 20 deletions

View File

@@ -27,14 +27,17 @@ type MetricQuery struct {
}
type MetricRow struct {
VIN string `json:"vin"`
StatDate string `json:"stat_date"`
Protocol string `json:"protocol"`
DailyMileageKM float64 `json:"daily_mileage_km"`
FirstTotalMileageKM *float64 `json:"first_total_mileage_km,omitempty"`
LatestTotalMileageKM *float64 `json:"latest_total_mileage_km,omitempty"`
SampleCount int64 `json:"sample_count"`
UpdatedAt string `json:"updated_at"`
VIN string `json:"vin"`
StatDate string `json:"stat_date"`
Protocol string `json:"protocol"`
DailyMileageKM float64 `json:"daily_mileage_km"`
FirstTotalMileageKM *float64 `json:"first_total_mileage_km,omitempty"`
LatestTotalMileageKM *float64 `json:"latest_total_mileage_km,omitempty"`
TrustedSourceKey *string `json:"trusted_source_key,omitempty"`
TrustedPhone *string `json:"trusted_phone,omitempty"`
TrustedSourceEndpoint *string `json:"trusted_source_endpoint,omitempty"`
SampleCount int64 `json:"sample_count"`
UpdatedAt string `json:"updated_at"`
}
type MetricRepository struct {
@@ -64,6 +67,9 @@ func (r *MetricRepository) Query(ctx context.Context, query MetricQuery) ([]Metr
var updatedAt scanDateTime
var first sql.NullFloat64
var latest sql.NullFloat64
var trustedSourceKey sql.NullString
var trustedPhone sql.NullString
var trustedSourceEndpoint sql.NullString
if err := rows.Scan(
&row.VIN,
&statDate,
@@ -71,6 +77,9 @@ func (r *MetricRepository) Query(ctx context.Context, query MetricQuery) ([]Metr
&row.DailyMileageKM,
&first,
&latest,
&trustedSourceKey,
&trustedPhone,
&trustedSourceEndpoint,
&row.SampleCount,
&updatedAt,
); err != nil {
@@ -84,6 +93,15 @@ func (r *MetricRepository) Query(ctx context.Context, query MetricQuery) ([]Metr
if latest.Valid {
row.LatestTotalMileageKM = &latest.Float64
}
if trustedSourceKey.Valid {
row.TrustedSourceKey = &trustedSourceKey.String
}
if trustedPhone.Valid {
row.TrustedPhone = &trustedPhone.String
}
if trustedSourceEndpoint.Valid {
row.TrustedSourceEndpoint = &trustedSourceEndpoint.String
}
out = append(out, row)
}
return out, rows.Err()
@@ -120,7 +138,10 @@ func normalizeMetricQuery(query MetricQuery) MetricQuery {
func buildMetricSQL(query MetricQuery) (string, []any) {
where, args := buildMetricWhere(query)
sqlText := `SELECT vin, stat_date, protocol, daily_mileage_km, first_total_mileage_km, latest_total_mileage_km, sample_count, updated_at FROM vehicle_daily_mileage`
sqlText := `SELECT vin, stat_date, protocol, daily_mileage_km,
first_total_mileage_km, latest_total_mileage_km,
trusted_source_key, trusted_phone, trusted_source_endpoint,
sample_count, updated_at FROM vehicle_daily_mileage`
if len(where) > 0 {
sqlText += " WHERE " + strings.Join(where, " AND ")
}