refactor(stats): slim final daily mileage table
This commit is contained in:
@@ -27,17 +27,13 @@ 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"`
|
||||
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"`
|
||||
VIN string `json:"vin"`
|
||||
StatDate string `json:"stat_date"`
|
||||
Protocol string `json:"protocol"`
|
||||
SourceID *int64 `json:"source_id,omitempty"`
|
||||
DailyMileageKM float64 `json:"daily_mileage_km"`
|
||||
LatestTotalMileageKM *float64 `json:"latest_total_mileage_km,omitempty"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
type MetricRepository struct {
|
||||
@@ -65,43 +61,27 @@ func (r *MetricRepository) Query(ctx context.Context, query MetricQuery) ([]Metr
|
||||
var row MetricRow
|
||||
var statDate scanDate
|
||||
var updatedAt scanDateTime
|
||||
var first sql.NullFloat64
|
||||
var sourceID sql.NullInt64
|
||||
var latest sql.NullFloat64
|
||||
var trustedSourceKey sql.NullString
|
||||
var trustedPhone sql.NullString
|
||||
var trustedSourceEndpoint sql.NullString
|
||||
if err := rows.Scan(
|
||||
&row.VIN,
|
||||
&statDate,
|
||||
&row.Protocol,
|
||||
&sourceID,
|
||||
&row.DailyMileageKM,
|
||||
&first,
|
||||
&latest,
|
||||
&trustedSourceKey,
|
||||
&trustedPhone,
|
||||
&trustedSourceEndpoint,
|
||||
&row.SampleCount,
|
||||
&updatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
row.StatDate = statDate.String
|
||||
row.UpdatedAt = updatedAt.String
|
||||
if first.Valid {
|
||||
row.FirstTotalMileageKM = &first.Float64
|
||||
if sourceID.Valid {
|
||||
row.SourceID = &sourceID.Int64
|
||||
}
|
||||
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()
|
||||
@@ -138,10 +118,9 @@ 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,
|
||||
trusted_source_key, trusted_phone, trusted_source_endpoint,
|
||||
sample_count, updated_at FROM vehicle_daily_mileage`
|
||||
sqlText := `SELECT vin, stat_date, protocol, source_id,
|
||||
daily_mileage_km, latest_total_mileage_km, updated_at
|
||||
FROM vehicle_daily_mileage`
|
||||
if len(where) > 0 {
|
||||
sqlText += " WHERE " + strings.Join(where, " AND ")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user