Skip to content

Commit

Permalink
api,sqlite: normalize end period
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Sep 28, 2023
1 parent 2c9e39c commit 2043a47
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 1 addition & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ func (a *api) handleGetWeb3Index(c jape.Context) {

start := now.AddDate(-1, 0, 0)
start = start.AddDate(0, 0, -int(start.Weekday()+1))
end := now.AddDate(0, 0, 1)

days, err := a.sp.Periods(start, end, stats.PeriodDaily)
days, err := a.sp.Periods(start, now, stats.PeriodDaily)
if err != nil {
c.Error(err, http.StatusInternalServerError)
return
Expand Down
13 changes: 13 additions & 0 deletions persist/sqlite/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ ORDER BY date_created ASC`
start = stats.NormalizePeriod(start, period)
end = stats.NormalizePeriod(end, period)

switch period {
case stats.PeriodHourly: // end of the hour
end = end.Add(time.Hour)
case stats.PeriodDaily: // end of the day
end = end.AddDate(0, 0, 1)
case stats.PeriodWeekly: // end of the week
end = end.AddDate(0, 0, 7-int(end.Weekday()))
case stats.PeriodMonthly: // end of the month
end = end.AddDate(0, 1, 0)
default:
panic("invalid period")
}

rows, err := tx.Query(query, sqlTime(start), sqlTime(end))
if err != nil {
return err
Expand Down

0 comments on commit 2043a47

Please sign in to comment.