feat: add vehicle mileage statistics

This commit is contained in:
lingniu
2026-07-14 13:18:52 +08:00
parent bb59303a4b
commit 6cddc0a43d
15 changed files with 522 additions and 1 deletions

View File

@@ -33,6 +33,7 @@ type Store interface {
RawFrames(context.Context, RawFrameQuery) (Page[RawFrameRow], error)
MileageSummary(context.Context, url.Values) (MileageSummary, error)
DailyMileage(context.Context, url.Values) (Page[DailyMileageRow], error)
MileageStatistics(context.Context, url.Values) (MileageStatistics, error)
QualitySummary(context.Context, url.Values) (QualitySummary, error)
QualityIssues(context.Context, url.Values) (Page[QualityIssueRow], error)
OpsHealth(context.Context) (OpsHealth, error)
@@ -3105,6 +3106,42 @@ func (s *Service) DailyMileage(ctx context.Context, query url.Values) (Page[Dail
return s.store.DailyMileage(ctx, resolvedQuery)
}
func (s *Service) MileageStatistics(ctx context.Context, query url.Values) (MileageStatistics, error) {
resolvedQuery, err := s.resolveVehicleQuery(ctx, query)
if err != nil {
return MileageStatistics{}, err
}
resolvedQuery, err = normalizeMileageStatisticsWindow(resolvedQuery, time.Now())
if err != nil {
return MileageStatistics{}, err
}
return s.store.MileageStatistics(ctx, resolvedQuery)
}
func normalizeMileageStatisticsWindow(query url.Values, now time.Time) (url.Values, error) {
next := cloneValues(query)
dateTo := strings.TrimSpace(next.Get("dateTo"))
if dateTo == "" {
dateTo = now.Format("2006-01-02")
next.Set("dateTo", dateTo)
}
dateFrom := strings.TrimSpace(next.Get("dateFrom"))
if dateFrom == "" {
parsedTo, _ := time.ParseInLocation("2006-01-02", dateTo, now.Location())
dateFrom = parsedTo.AddDate(0, 0, -29).Format("2006-01-02")
next.Set("dateFrom", dateFrom)
}
from, fromErr := time.ParseInLocation("2006-01-02", dateFrom, now.Location())
to, toErr := time.ParseInLocation("2006-01-02", dateTo, now.Location())
if fromErr != nil || toErr != nil || from.After(to) {
return nil, clientError{Code: "INVALID_DATE_RANGE", Message: "统计日期范围无效"}
}
if to.Sub(from) > 365*24*time.Hour {
return nil, clientError{Code: "DATE_RANGE_TOO_LARGE", Message: "车辆统计最多查询 366 个自然日"}
}
return next, nil
}
func (s *Service) OnlineStatisticsSummary(ctx context.Context, query url.Values) (OnlineStatisticsSummary, error) {
resolvedQuery, err := s.resolveVehicleQuery(ctx, query)
if err != nil {