summaryrefslogtreecommitdiff
path: root/internal/schedule
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-09-28 11:48:25 +0300
committerPaul Buetow <paul@buetow.org>2024-09-28 11:48:25 +0300
commit9dda6355b81f6e6b93fc67d846657a388fcce4b7 (patch)
treec02302ab96885f5238e6a20c215115083f52ff37 /internal/schedule
parent262af22115291863609319b6b0f313873891e15f (diff)
can hit and not hit the target
Diffstat (limited to 'internal/schedule')
-rw-r--r--internal/schedule/schedule.go6
-rw-r--r--internal/schedule/stats.go23
2 files changed, 19 insertions, 10 deletions
diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go
index 427c208..17c9588 100644
--- a/internal/schedule/schedule.go
+++ b/internal/schedule/schedule.go
@@ -13,12 +13,16 @@ 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, args.Lookback)
+ stats, err := newStats(dir, args.Lookback, args.Target)
if err != nil {
return "", err
}
log.Println("For", platform, "stats:", stats)
+ if stats.targetHit() {
+ log.Println("Target hit, not posting at", platform)
+ return "", NothingToSchedule
+ }
return "", NothingToSchedule
}
diff --git a/internal/schedule/stats.go b/internal/schedule/stats.go
index a969272..aac3737 100644
--- a/internal/schedule/stats.go
+++ b/internal/schedule/stats.go
@@ -13,20 +13,21 @@ import (
// Posting stats
type stats struct {
- posted int
- queued int
- sinceDays float64
- postsPerDay float64
+ posted int
+ queued int
+ sinceDays float64
+ postsPerDay float64
+ postsPerDayTarget float64
}
func (s stats) String() string {
- return fmt.Sprintf("posted:%d,queued:%d,sinceDays:%v,postsPerDay:%v",
- s.posted, s.queued, s.sinceDays, s.postsPerDay,
+ return fmt.Sprintf("posted:%d,queued:%d,sinceDays:%v,postsPerDay:%v >? %v",
+ s.posted, s.queued, s.sinceDays, s.postsPerDay, s.postsPerDayTarget,
)
}
-func newStats(dir string, lookback time.Duration) (stats, error) {
- var stats stats
+func newStats(dir string, lookback time.Duration, target int) (stats, error) {
+ stats := stats{postsPerDayTarget: float64(target) / 7}
if err := stats.gatherPostedStats(dir, pastTime(lookback)); err != nil {
return stats, err
@@ -38,6 +39,10 @@ func newStats(dir string, lookback time.Duration) (stats, error) {
return stats, nil
}
+func (s stats) targetHit() bool {
+ return s.postsPerDayTarget <= s.postsPerDay
+}
+
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")
@@ -69,7 +74,7 @@ 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) / s.sinceDays
+ s.postsPerDay = float64(s.posted) / float64(s.sinceDays)
return errors.Join(errs...)
}