diff options
| -rw-r--r-- | cmd/gos/main.go | 2 | ||||
| -rw-r--r-- | internal/config/args.go | 1 | ||||
| -rw-r--r-- | internal/schedule/schedule.go | 6 | ||||
| -rw-r--r-- | internal/schedule/stats.go | 23 |
4 files changed, 22 insertions, 10 deletions
diff --git a/cmd/gos/main.go b/cmd/gos/main.go index 2a457d4..3724f3e 100644 --- a/cmd/gos/main.go +++ b/cmd/gos/main.go @@ -19,6 +19,7 @@ func main() { version := flag.Bool("version", false, "Display version") gosDir := flag.String("gosDir", "./gosdir", "Gos' directory") platforms := flag.String("platforms", "Mastodon,LinkedIn", "Platforms enabled") + target := flag.Int("target", 2, "How many posts per week are the target?") lookback := flag.Int("lookback", 30, "How many days look back in time for posting history") flag.Parse() @@ -26,6 +27,7 @@ func main() { DryRun: *dry, GosDir: *gosDir, Platforms: strings.Split(*platforms, ","), + Target: *target, Lookback: time.Duration(*lookback) * time.Hour * 24, } diff --git a/internal/config/args.go b/internal/config/args.go index 78b55de..39e6762 100644 --- a/internal/config/args.go +++ b/internal/config/args.go @@ -13,6 +13,7 @@ type Args struct { GosDir string DryRun bool Platforms []string + Target int Lookback time.Duration } 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...) } |
