diff options
| author | Paul Buetow <paul@buetow.org> | 2024-10-01 10:05:11 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-10-01 10:05:11 +0300 |
| commit | 74bcb110cba9cd22b88c561115e332d500de7716 (patch) | |
| tree | 080287b50ff69e2e601e5ab781da500a93c4b43c /internal/schedule | |
| parent | cbdce79d25206573d1f79c9c647dcaa251b69463 (diff) | |
use of entry.Entry around the code base
Diffstat (limited to 'internal/schedule')
| -rw-r--r-- | internal/schedule/schedule.go | 13 | ||||
| -rw-r--r-- | internal/schedule/stats.go | 23 |
2 files changed, 14 insertions, 22 deletions
diff --git a/internal/schedule/schedule.go b/internal/schedule/schedule.go index 3d3061f..9a9121e 100644 --- a/internal/schedule/schedule.go +++ b/internal/schedule/schedule.go @@ -8,6 +8,7 @@ import ( "strings" "codeberg.org/snonux/gos/internal/config" + "codeberg.org/snonux/gos/internal/entry" "codeberg.org/snonux/gos/internal/oi" ) @@ -16,26 +17,26 @@ var ( ErrNothingQueued = errors.New("nothing queued") ) -func Run(args config.Args, platform string) (string, error) { +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) if err != nil { - return "", err + return entry.Zero, err } log.Println("For", platform, "stats:", stats) if stats.targetHit() { log.Println("Target hit, not posting at", platform) - return "", ErrNothingToSchedule + return entry.Zero, ErrNothingToSchedule } // Schedule random qeued entry for platform - randomEntry, err := oi.ReadDirRandomEntry(dir, func(file os.DirEntry) bool { + ent, err := oi.ReadDirRandomEntry(dir, func(file os.DirEntry) bool { return strings.HasSuffix(file.Name(), ".queued") }) if err != nil { - return randomEntry, fmt.Errorf("%w: %w", ErrNothingQueued, err) + return entry.Zero, fmt.Errorf("%w: %w", ErrNothingQueued, err) } - return randomEntry, nil + return ent, nil } diff --git a/internal/schedule/stats.go b/internal/schedule/stats.go index b36fd54..e6b2b29 100644 --- a/internal/schedule/stats.go +++ b/internal/schedule/stats.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "codeberg.org/snonux/gos/internal/entry" "codeberg.org/snonux/gos/internal/format" "codeberg.org/snonux/gos/internal/oi" ) @@ -58,16 +59,16 @@ func (s *stats) gatherPostedStats(dir string, lookbackTime time.Time) error { var errs []error for filePath := range ch { - entryTime, err := parseEntryPath(filePath) + ent, err := entry.New(filePath) if err != nil { errs = append(errs, err) continue } - if entryTime.Before(lookbackTime) { + if ent.Time.Before(lookbackTime) { continue } - if entryTime.Before(oldest) { - oldest = entryTime + if ent.Time.Before(oldest) { + oldest = ent.Time } s.posted++ } @@ -91,7 +92,8 @@ func (s *stats) gatherQueuedStats(dir string) error { errs []error ) for filePath := range ch { - if _, err := parseEntryPath(filePath); err != nil { + // Here, we only test whether we can parse the entry. + if _, err := entry.New(filePath); err != nil { errs = append(errs, err) continue } @@ -116,14 +118,3 @@ func nowTime() time.Time { func pastTime(duration time.Duration) time.Time { return nowTime().Add(-duration) } - -// TODO: Maybe introduce types.Entry struct, which encapsulates all the parsing -func parseEntryPath(filePath string) (time.Time, error) { - // Format: foobarbaz.something.here.txt.STAMP.{posted,queued} - // We want to get the STAMP! - parts := strings.Split(filePath, ".") - if len(parts) < 4 { - return time.Time{}, fmt.Errorf("not a valid entry path: %s", filePath) - } - return time.Parse(format.Time, parts[len(parts)-2]) -} |
