fix: key daily metrics by vehicle identity

This commit is contained in:
lingniu
2026-07-02 00:18:35 +08:00
parent 9045b871d6
commit bcf7cdae2e
5 changed files with 201 additions and 31 deletions

View File

@@ -17,16 +17,18 @@ type Queryer interface {
}
type MetricQuery struct {
VIN string
Protocol string
MetricKey string
DateFrom string
DateTo string
Limit int
Offset int
VehicleKey string
VIN string
Protocol string
MetricKey string
DateFrom string
DateTo string
Limit int
Offset int
}
type MetricRow struct {
VehicleKey string `json:"vehicle_key"`
VIN string `json:"vin"`
StatDate string `json:"stat_date"`
Protocol string `json:"protocol"`
@@ -70,6 +72,7 @@ func (r *MetricRepository) Query(ctx context.Context, query MetricQuery) ([]Metr
var first sql.NullFloat64
var latest sql.NullFloat64
if err := rows.Scan(
&row.VehicleKey,
&row.VIN,
&statDate,
&row.Protocol,
@@ -100,6 +103,7 @@ func (r *MetricRepository) Query(ctx context.Context, query MetricQuery) ([]Metr
}
func normalizeMetricQuery(query MetricQuery) MetricQuery {
query.VehicleKey = strings.TrimSpace(query.VehicleKey)
query.VIN = strings.TrimSpace(query.VIN)
query.Protocol = strings.ToUpper(strings.TrimSpace(query.Protocol))
query.MetricKey = strings.TrimSpace(query.MetricKey)
@@ -121,6 +125,9 @@ func buildMetricSQL(query MetricQuery) (string, []any) {
if query.VIN != "" {
add("vin = ?", query.VIN)
}
if query.VehicleKey != "" {
add("vehicle_key = ?", query.VehicleKey)
}
if query.Protocol != "" {
add("protocol = ?", query.Protocol)
}
@@ -133,11 +140,11 @@ func buildMetricSQL(query MetricQuery) (string, []any) {
if query.DateTo != "" {
add("stat_date <= ?", query.DateTo)
}
sqlText := `SELECT vin, stat_date, protocol, metric_key, metric_value, metric_unit, first_total_mileage_km, latest_total_mileage_km, sample_count, calculation_method, created_at, updated_at FROM vehicle_daily_metric`
sqlText := `SELECT vehicle_key, vin, stat_date, protocol, metric_key, metric_value, metric_unit, first_total_mileage_km, latest_total_mileage_km, sample_count, calculation_method, created_at, updated_at FROM vehicle_daily_metric`
if len(where) > 0 {
sqlText += " WHERE " + strings.Join(where, " AND ")
}
sqlText += " ORDER BY stat_date DESC, vin ASC, protocol ASC, metric_key ASC LIMIT ? OFFSET ?"
sqlText += " ORDER BY stat_date DESC, vehicle_key ASC, protocol ASC, metric_key ASC LIMIT ? OFFSET ?"
args = append(args, query.Limit, query.Offset)
return sqlText, args
}
@@ -192,13 +199,14 @@ func parseMetricQuery(r *http.Request) (MetricQuery, error) {
return MetricQuery{}, err
}
query := MetricQuery{
VIN: values.Get("vin"),
Protocol: values.Get("protocol"),
MetricKey: values.Get("metricKey"),
DateFrom: values.Get("dateFrom"),
DateTo: values.Get("dateTo"),
Limit: limit,
Offset: offset,
VehicleKey: values.Get("vehicleKey"),
VIN: values.Get("vin"),
Protocol: values.Get("protocol"),
MetricKey: values.Get("metricKey"),
DateFrom: values.Get("dateFrom"),
DateTo: values.Get("dateTo"),
Limit: limit,
Offset: offset,
}
if !validDate(query.DateFrom) || !validDate(query.DateTo) {
return MetricQuery{}, errors.New("dateFrom/dateTo must use YYYY-MM-DD")