diff options
| -rw-r--r-- | internal/entry/entry.go | 29 | ||||
| -rw-r--r-- | internal/oi/oi.go | 9 | ||||
| -rw-r--r-- | internal/run.go | 4 | ||||
| -rw-r--r-- | internal/schedule/schedule.go | 13 | ||||
| -rw-r--r-- | internal/schedule/stats.go | 23 |
5 files changed, 39 insertions, 39 deletions
diff --git a/internal/entry/entry.go b/internal/entry/entry.go index 4e837a0..6934eba 100644 --- a/internal/entry/entry.go +++ b/internal/entry/entry.go @@ -30,7 +30,7 @@ func (s State) String() string { } // The time this code was written a:round, actually. -const oldestValidTime = "20240922-102800" +const oldestValidTime = "20240912-102800" type Entry struct { Path string @@ -38,28 +38,35 @@ type Entry struct { State State } +func (e Entry) String() string { + return fmt.Sprintf("Path:%s;Stamp:%s,State:%s", + e.Path, e.Time.Format(format.Time), e.State) +} + +var Zero = Entry{} + // filePath format: /foo/foobarbaz.something.here.txt.STAMP.{posted,queued} func New(filePath string) (Entry, error) { - ent := Entry{Path: filePath} + e := Entry{Path: filePath} // We want to get the STAMP! parts := strings.Split(filePath, ".") if len(parts) < 4 { - return ent, fmt.Errorf("not a valid entry path: %s", filePath) + return e, fmt.Errorf("not a valid entry path: %s", filePath) } switch parts[len(parts)-1] { case "queued": - ent.State = Queued + e.State = Queued case "posted": - ent.State = Posted + e.State = Posted default: - return ent, fmt.Errorf("can't parse state from path: %s", filePath) + return e, fmt.Errorf("can't parse state from path: %s", filePath) } var err error - if ent.Time, err = time.Parse(format.Time, parts[len(parts)-2]); err != nil { - return ent, err + if e.Time, err = time.Parse(format.Time, parts[len(parts)-2]); err != nil { + return e, err } oldestValid, err := time.Parse(format.Time, oldestValidTime) @@ -67,9 +74,9 @@ func New(filePath string) (Entry, error) { panic(err) } - if ent.Time.Before(oldestValid) { - return ent, fmt.Errorf("entry time does not seem legic, it is too old: %v", ent.Time) + if e.Time.Before(oldestValid) { + return e, fmt.Errorf("entry time does not seem legit, it is too old: %v", e.Time) } - return ent, nil + return e, nil } diff --git a/internal/oi/oi.go b/internal/oi/oi.go index ca5c8c6..ea95212 100644 --- a/internal/oi/oi.go +++ b/internal/oi/oi.go @@ -8,6 +8,7 @@ import ( "path/filepath" "time" + "codeberg.org/snonux/gos/internal/entry" "golang.org/x/exp/rand" ) @@ -63,17 +64,17 @@ func ReadDirSlurp(dir string, filter func(file os.DirEntry) bool) ([]string, err return files, nil } -func ReadDirRandomEntry(dir string, filter func(file os.DirEntry) bool) (string, error) { +func ReadDirRandomEntry(dir string, filter func(file os.DirEntry) bool) (entry.Entry, error) { files, err := ReadDirSlurp(dir, filter) if err != nil { - return "", err + return entry.Zero, err } if len(files) == 0 { - return "", ErrNotFound + return entry.Zero, ErrNotFound } rand.Seed(uint64(time.Now().UnixNano())) - return files[rand.Intn(len(files))], nil + return entry.New(files[rand.Intn(len(files))]) } func IsRegular(path string) bool { diff --git a/internal/run.go b/internal/run.go index 1bfc06a..51573f5 100644 --- a/internal/run.go +++ b/internal/run.go @@ -16,7 +16,7 @@ func Run(ctx context.Context, args config.Args) error { } for _, platform := range args.Platforms { - path, err := schedule.Run(args, platform) + ent, err := schedule.Run(args, platform) switch { case errors.Is(err, schedule.ErrNothingToSchedule): log.Println("Nothing to be scheduled for", platform) @@ -26,7 +26,7 @@ func Run(ctx context.Context, args config.Args) error { return err } - log.Println("Scheduling", path) + log.Println("Scheduling", ent) } return nil 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]) -} |
