refactor(go): simplify daily mileage storage
This commit is contained in:
@@ -20,7 +20,6 @@ type MetricQuery struct {
|
||||
VehicleKey string
|
||||
VIN string
|
||||
Protocol string
|
||||
MetricKey string
|
||||
DateFrom string
|
||||
DateTo string
|
||||
Limit int
|
||||
@@ -32,13 +31,10 @@ type MetricRow struct {
|
||||
VIN string `json:"vin"`
|
||||
StatDate string `json:"stat_date"`
|
||||
Protocol string `json:"protocol"`
|
||||
MetricKey string `json:"metric_key"`
|
||||
MetricValue float64 `json:"metric_value"`
|
||||
MetricUnit string `json:"metric_unit"`
|
||||
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"`
|
||||
CalculationMethod string `json:"calculation_method"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
@@ -76,13 +72,10 @@ func (r *MetricRepository) Query(ctx context.Context, query MetricQuery) ([]Metr
|
||||
&row.VIN,
|
||||
&statDate,
|
||||
&row.Protocol,
|
||||
&row.MetricKey,
|
||||
&row.MetricValue,
|
||||
&row.MetricUnit,
|
||||
&row.DailyMileageKM,
|
||||
&first,
|
||||
&latest,
|
||||
&row.SampleCount,
|
||||
&row.CalculationMethod,
|
||||
&createdAt,
|
||||
&updatedAt,
|
||||
); err != nil {
|
||||
@@ -124,7 +117,6 @@ 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)
|
||||
query.DateFrom = strings.TrimSpace(query.DateFrom)
|
||||
query.DateTo = strings.TrimSpace(query.DateTo)
|
||||
if query.Limit <= 0 {
|
||||
@@ -135,18 +127,18 @@ func normalizeMetricQuery(query MetricQuery) MetricQuery {
|
||||
|
||||
func buildMetricSQL(query MetricQuery) (string, []any) {
|
||||
where, args := buildMetricWhere(query)
|
||||
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`
|
||||
sqlText := `SELECT vehicle_key, vin, stat_date, protocol, daily_mileage_km, first_total_mileage_km, latest_total_mileage_km, sample_count, created_at, updated_at FROM vehicle_daily_mileage`
|
||||
if len(where) > 0 {
|
||||
sqlText += " WHERE " + strings.Join(where, " AND ")
|
||||
}
|
||||
sqlText += " ORDER BY stat_date DESC, vehicle_key ASC, protocol ASC, metric_key ASC LIMIT ? OFFSET ?"
|
||||
sqlText += " ORDER BY stat_date DESC, vehicle_key ASC, protocol ASC LIMIT ? OFFSET ?"
|
||||
args = append(args, query.Limit, query.Offset)
|
||||
return sqlText, args
|
||||
}
|
||||
|
||||
func buildMetricCountSQL(query MetricQuery) (string, []any) {
|
||||
where, args := buildMetricWhere(query)
|
||||
sqlText := `SELECT COUNT(*) FROM vehicle_daily_metric`
|
||||
sqlText := `SELECT COUNT(*) FROM vehicle_daily_mileage`
|
||||
if len(where) > 0 {
|
||||
sqlText += " WHERE " + strings.Join(where, " AND ")
|
||||
}
|
||||
@@ -169,9 +161,6 @@ func buildMetricWhere(query MetricQuery) ([]string, []any) {
|
||||
if query.Protocol != "" {
|
||||
add("protocol = ?", query.Protocol)
|
||||
}
|
||||
if query.MetricKey != "" {
|
||||
add("metric_key = ?", query.MetricKey)
|
||||
}
|
||||
if query.DateFrom != "" {
|
||||
add("stat_date >= ?", query.DateFrom)
|
||||
}
|
||||
@@ -239,7 +228,6 @@ func parseMetricQuery(r *http.Request) (MetricQuery, error) {
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user