fix(stats): preserve realtime baseline from current candidate
This commit is contained in:
@@ -318,6 +318,35 @@ func lookupPreviousSourceBaseline(ctx context.Context, query Queryer, vin string
|
||||
}, latestTotal.Valid, nil
|
||||
}
|
||||
|
||||
func lookupCurrentSourceBaseline(ctx context.Context, query Queryer, vin string, statDate string, protocol envelope.Protocol, sourceKey string) (sourceBaseline, bool, error) {
|
||||
if query == nil || strings.TrimSpace(vin) == "" || strings.TrimSpace(statDate) == "" || strings.TrimSpace(sourceKey) == "" {
|
||||
return sourceBaseline{}, false, nil
|
||||
}
|
||||
rows, err := query.QueryContext(ctx, currentSourceBaselineSQL, vin, statDate, string(protocol), sourceKey)
|
||||
if err != nil {
|
||||
return sourceBaseline{}, false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
if !rows.Next() {
|
||||
if err := rows.Err(); err != nil {
|
||||
return sourceBaseline{}, false, err
|
||||
}
|
||||
return sourceBaseline{}, false, nil
|
||||
}
|
||||
var firstTotal sql.NullFloat64
|
||||
var firstEvent sql.NullTime
|
||||
if err := rows.Scan(&firstTotal, &firstEvent); err != nil {
|
||||
return sourceBaseline{}, false, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return sourceBaseline{}, false, err
|
||||
}
|
||||
return sourceBaseline{
|
||||
LatestTotalKM: firstTotal.Float64,
|
||||
LatestEventTime: firstEvent.Time,
|
||||
}, firstTotal.Valid, nil
|
||||
}
|
||||
|
||||
func previousStatDate(statDate string) (string, bool) {
|
||||
day, err := time.Parse("2006-01-02", strings.TrimSpace(statDate))
|
||||
if err != nil {
|
||||
@@ -336,3 +365,15 @@ WHERE vin = ?
|
||||
ORDER BY latest_event_time DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
const currentSourceBaselineSQL = `
|
||||
SELECT first_total_mileage_km, first_event_time
|
||||
FROM vehicle_daily_mileage_source
|
||||
WHERE vin = ?
|
||||
AND stat_date = ?
|
||||
AND protocol = ?
|
||||
AND source_key = ?
|
||||
AND quality_status = '` + QualityOK + `'
|
||||
ORDER BY latest_event_time DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
Reference in New Issue
Block a user