From 3287dbc49f3a9df43b086da84516b7e0a9902707 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 18 Oct 2024 22:51:50 +0300 Subject: implement pause days --- internal/schedule/schedule.go | 3 --- internal/schedule/stats.go | 25 +++++++++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) (limited to 'internal/schedule') diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go index 5cb80be..5512e4d 100644 --- a/internal/schedule/schedule.go +++ b/internal/schedule/schedule.go @@ -18,8 +18,6 @@ var ( ErrNothingQueued = errors.New("nothing queued") ) -// TODO: Make it so that I don't post 2 days in a row, make it configurable how -// many days between each post for each platform there should be. func Run(args config.Args, platform string) (entry.Entry, error) { dir := fmt.Sprintf("%s/db/platforms/%s", args.GosDir, strings.ToLower(platform)) stats, err := newStats(dir, args.Lookback, args.Target) @@ -29,7 +27,6 @@ func Run(args config.Args, platform string) (entry.Entry, error) { log.Println("For", platform, "stats:", stats) if stats.targetHit() { - log.Println("Target hit, not posting at", platform) return entry.Zero, ErrNothingToSchedule } diff --git a/internal/schedule/stats.go b/internal/schedule/stats.go index 172272f..bf5a12c 100644 --- a/internal/schedule/stats.go +++ b/internal/schedule/stats.go @@ -2,6 +2,7 @@ package schedule import ( "fmt" + "log" "os" "path/filepath" "time" @@ -18,11 +19,12 @@ type stats struct { sinceDays float64 postsPerDay float64 postsPerDayTarget float64 + lastPostDaysAgo float64 } func (s stats) String() string { - return fmt.Sprintf("posted:%d,queued:%d,sinceDays:%v,postsPerDay:%v >? postsPerDayTarget:%v", - s.posted, s.queued, s.sinceDays, s.postsPerDay, s.postsPerDayTarget, + return fmt.Sprintf("posted:%d,queued:%d,sinceDays:%v,postsPerDayTarget:%v>?%v,lastPostDaysAgo:%v", + s.posted, s.queued, s.sinceDays, s.postsPerDay, s.postsPerDayTarget, s.lastPostDaysAgo, ) } @@ -39,14 +41,24 @@ func newStats(dir string, lookback time.Duration, target int) (stats, error) { return stats, nil } -func (s stats) targetHit() bool { - return s.postsPerDay >= s.postsPerDayTarget +func (s stats) targetHit(pauseDays int) bool { + if s.postsPerDay >= s.postsPerDayTarget { + log.Println("Posts per day target hit") + return true + } + if s.lastPostDaysAgo <= float64(pauseDays) { + log.Println("Need to wait a bit longer as last post isn't", pauseDays, "ago yet") + return true + + } + return false } func (s *stats) gatherPostedStats(dir string, lookbackTime time.Time) error { var ( now time.Time = timestamp.NowTime() oldest time.Time = now + newest time.Time = timestamp.OldestValidTime() ) err := oi.TraverseDir(dir, func(file os.DirEntry) error { @@ -61,6 +73,9 @@ func (s *stats) gatherPostedStats(dir string, lookbackTime time.Time) error { if ent.Time.Before(oldest) { oldest = ent.Time } + if ent.Time.After(newest) { + newest = ent.Time + } s.posted++ return nil }) @@ -71,6 +86,8 @@ func (s *stats) gatherPostedStats(dir string, lookbackTime time.Time) error { since := now.Sub(oldest) s.sinceDays = since.Abs().Hours() / 24 s.postsPerDay = float64(s.posted) / float64(s.sinceDays) + s.lastPostDaysAgo = now.Sub(newest).Hours() / 24.0 + return nil } -- cgit v1.2.3