diff options
| author | Paul Buetow <paul@buetow.org> | 2025-01-11 00:05:16 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-01-11 00:05:16 +0200 |
| commit | 94f78c38bbadcb880b35652aa2bdb2ab7cc586f0 (patch) | |
| tree | fb18b6a8c7e5a2cc7698864a2403cbca1202d183 | |
| parent | 73f70adef4f1b4248a5c5da44566f834764c3dea (diff) | |
add all time / lifetime stats
| -rw-r--r-- | internal/schedule/schedule.go | 2 | ||||
| -rw-r--r-- | internal/schedule/stats.go | 99 |
2 files changed, 65 insertions, 36 deletions
diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go index 9b31f88..1872559 100644 --- a/internal/schedule/schedule.go +++ b/internal/schedule/schedule.go @@ -27,7 +27,7 @@ func Run(args config.Args, platform platforms.Platform) (entry.Entry, error) { if err != nil { return entry.Zero, err } - stats.Render(platform) + stats.RenderTable(platform) if stats.queued < args.MinQueued { _ = prompt.Acknowledge( diff --git a/internal/schedule/stats.go b/internal/schedule/stats.go index a9d5ba4..b863450 100644 --- a/internal/schedule/stats.go +++ b/internal/schedule/stats.go @@ -24,6 +24,10 @@ type stats struct { postsPerDay float64 postsPerDayTarget float64 lastPostDaysAgo float64 + + totalPosted int + totalSinceDays float64 + totalPostsPerDay float64 } func newStats(dir string, lookback time.Duration, target int) (stats, error) { @@ -45,36 +49,6 @@ func (s stats) String() string { ) } -func (s stats) Render(platform platforms.Platform) { - var sb strings.Builder - - sep := colour.SInfo2f("+%s+%s+", strings.Repeat("-", 22), strings.Repeat("-", 13)) - sb.WriteString(sep) - sb.WriteString("\n") - sb.WriteString(colour.SInfo2f("| %-20s | %-11s |", platform, "Stat. value")) - sb.WriteString("\n") - sb.WriteString(sep) - sb.WriteString("\n") - sb.WriteString(colour.SInfo2f("| %-20s | %-11s |", "Stats since (days)", fmt.Sprintf("%.02f", s.sinceDays))) - sb.WriteString("\n") - sb.WriteString(colour.SInfo2f("| %-20s | %-11s |", "#Posted entries", strconv.Itoa(s.posted))) - sb.WriteString("\n") - sb.WriteString(colour.SInfo2f("| %-20s | %-11s |", "#Queued entries", strconv.Itoa(s.queued))) - sb.WriteString("\n") - sb.WriteString(colour.SInfo2f("| %-20s | %-11s |", "Enough for (days)", fmt.Sprintf("%.02f", s.queuedForDays))) - sb.WriteString("\n") - sb.WriteString(colour.SInfo2f("| %-20s | %-11s |", "Last post (days ago)", fmt.Sprintf("%.02f", s.lastPostDaysAgo))) - sb.WriteString("\n") - sb.WriteString(colour.SInfo2f("| %-20s | %-11s |", "Posts per day", fmt.Sprintf("%.02f", s.postsPerDay))) - sb.WriteString("\n") - sb.WriteString(colour.SInfo2f("| %-20s | %-11s |", "Posts per day target", fmt.Sprintf("%.02f", s.postsPerDayTarget))) - sb.WriteString("\n") - sb.WriteString(sep) - sb.WriteString("\n") - - fmt.Print(sb.String()) -} - func (s stats) targetHit(pauseDays, maxQueuedDays int) bool { if s.queuedForDays > float64(maxQueuedDays) { s.postsPerDayTarget++ @@ -93,9 +67,10 @@ func (s stats) targetHit(pauseDays, maxQueuedDays int) bool { 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() + now time.Time = timestamp.NowTime() + newest time.Time = timestamp.OldestValidTime() + oldest time.Time = now // Oldest since lookbackTime + totalOldest time.Time = now // All time oldest ) err := oi.TraverseDir(dir, func(file os.DirEntry) error { @@ -104,7 +79,14 @@ func (s *stats) gatherPostedStats(dir string, lookbackTime time.Time) error { if err != nil { return err } - if ent.State != entry.Posted || ent.Time.Before(lookbackTime) { + if ent.State != entry.Posted { + return nil + } + if ent.Time.Before(totalOldest) { + totalOldest = ent.Time + } + s.totalPosted++ + if ent.Time.Before(lookbackTime) { return nil } // Ignore .now. @@ -125,10 +107,14 @@ func (s *stats) gatherPostedStats(dir string, lookbackTime time.Time) error { } since := now.Sub(oldest) - s.sinceDays = since.Abs().Hours() / 24 + s.sinceDays = since.Abs().Hours() / 24.0 s.postsPerDay = float64(s.posted) / float64(s.sinceDays) s.lastPostDaysAgo = now.Sub(newest).Hours() / 24.0 + since = now.Sub(totalOldest) + s.totalSinceDays = since.Abs().Hours() / 24.0 + s.totalPostsPerDay = float64(s.totalPosted) / float64(s.totalSinceDays) + return nil } @@ -155,6 +141,49 @@ func (s *stats) gatherQueuedStats(dir string) error { return err } +func (s stats) RenderTable(platform platforms.Platform) { + var sb strings.Builder + + dataRow := func(descr1, val1, descr2, val2 string) { + const format = "| %-21s | %-11s | %-21s | %-11s |" + sb.WriteString(colour.SInfo2f(format, descr1, val1, descr2, val2)) + sb.WriteString("\n") + } + + sep := colour.SInfo2f("+%s+%s+%s+%s+", strings.Repeat("-", 23), + strings.Repeat("-", 13), strings.Repeat("-", 23), strings.Repeat("-", 13)) + + separator := func() { + sb.WriteString(sep) + sb.WriteString("\n") + } + + val := func(val any) string { + switch v := val.(type) { + case int: + return strconv.Itoa(v) + case float64: + return fmt.Sprintf("%0.2f", v) + default: + panic("unexpeced type") + } + } + + separator() + dataRow(platform.String(), "value", "Lifetime stats", "value") + separator() + dataRow("Since (days)", val(s.sinceDays), "Total since (days)", val(s.totalSinceDays)) + dataRow("#Posted entries", val(s.posted), "#Total posted entries", val(s.totalPosted)) + dataRow("#Queued entries", val(s.queued), "", "") + dataRow("Enough for (days)", val(s.queuedForDays), "", "") + dataRow("Last post (days ago)", val(s.lastPostDaysAgo), "", "") + dataRow("Posts per day", val(s.postsPerDay), "Total posts per day", val(s.totalPostsPerDay)) + dataRow("Posts per day target", val(s.postsPerDayTarget), "", "") + separator() + + fmt.Print(sb.String()) +} + func pastTime(duration time.Duration) time.Time { return timestamp.NowTime().Add(-duration) } |
