From 6865389682c865717bb28f19943c09d0610a7981 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 28 Sep 2024 11:02:37 +0300 Subject: add lookback time --- internal/schedule/schedule.go | 2 +- internal/schedule/stats.go | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) (limited to 'internal/schedule') diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go index 321e096..427c208 100644 --- a/internal/schedule/schedule.go +++ b/internal/schedule/schedule.go @@ -13,7 +13,7 @@ var NothingToSchedule = errors.New("nothing to schedule") func Run(args config.Args, platform string) (string, error) { dir := fmt.Sprintf("%s/db/platforms/%s", args.GosDir, strings.ToLower(platform)) - stats, err := newStats(dir) + stats, err := newStats(dir, args.Lookback) if err != nil { return "", err } diff --git a/internal/schedule/stats.go b/internal/schedule/stats.go index ac1671a..d6cbf20 100644 --- a/internal/schedule/stats.go +++ b/internal/schedule/stats.go @@ -25,10 +25,10 @@ func (s stats) String() string { ) } -func newStats(dir string) (stats, error) { +func newStats(dir string, lookback time.Duration) (stats, error) { var stats stats - if err := stats.gatherPostedStats(dir); err != nil { + if err := stats.gatherPostedStats(dir, pastTime(lookback)); err != nil { return stats, err } if err := stats.gatherQueuedStats(dir); err != nil { @@ -38,8 +38,7 @@ func newStats(dir string) (stats, error) { return stats, nil } -// TODO: Only take entries into consideration from last n days -func (s *stats) gatherPostedStats(dir string) error { +func (s *stats) gatherPostedStats(dir string, lookbackTime time.Time) error { ch, err := oi.ReadDirFilter(dir, func(file os.DirEntry) bool { return strings.HasSuffix(file.Name(), ".posted") }) @@ -53,14 +52,18 @@ func (s *stats) gatherPostedStats(dir string) error { ) var errs []error + // TODO: Maybe refactor to include in ReadDirFilter filter for filePath := range ch { - newOldest, err := parseEntryPath(filePath) + entryTime, err := parseEntryPath(filePath) if err != nil { errs = append(errs, err) continue } - if newOldest.Before(oldest) { - oldest = newOldest + if entryTime.Before(lookbackTime) { + continue + } + if entryTime.Before(oldest) { + oldest = entryTime } s.posted++ } @@ -106,6 +109,10 @@ func nowTime() time.Time { return simplerNow } +func pastTime(duration time.Duration) time.Time { + return nowTime().Add(-duration) +} + func parseEntryPath(filePath string) (time.Time, error) { // Format: foobarbaz.something.here.txt.STAMP.{posted,queued} // We want to get the STAMP! -- cgit v1.2.3