cache

package
v0.1.22 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MergeCycleSlices added in v0.1.10

func MergeCycleSlices(a, b []whoop.Cycle) []whoop.Cycle

MergeCycleSlices merges two cycle slices, deduplicating by ID.

func MergeRecoverySlices

func MergeRecoverySlices(a, b []whoop.Recovery) []whoop.Recovery

MergeRecoverySlices combines two recovery slices, deduping by CycleID. MergeRecoverySlices merges two recovery slices, deduplicating by CycleID.

Types

type AlwaysStaleChecker

type AlwaysStaleChecker struct{}

func (*AlwaysStaleChecker) ShouldRefresh

func (c *AlwaysStaleChecker) ShouldRefresh(scoreState whoop.ScoreState, dataDate, fetchedAt time.Time) bool

type CacheResult

type CacheResult[T any] struct {
	Data      T
	FromCache bool
}

CacheResult wraps a single cached item with metadata about its source.

type CacheService

type CacheService interface {
	// Single-date operations
	GetCycleForDate(ctx context.Context, date time.Time) (*CacheResult[*whoop.Cycle], error)
	GetRecoveryForCycle(ctx context.Context, cycleID int64) (*CacheResult[*whoop.Recovery], error)
	GetSleepForCycle(ctx context.Context, cycleID int64) (*CacheResult[*whoop.Sleep], error)
	GetWorkoutsForDateRange(ctx context.Context, start, end time.Time) (*CacheResult[[]whoop.Workout], error)

	// Range operations (for charts, historical data)
	GetCyclesForRange(ctx context.Context, start, end time.Time) (*DateRangeResult[whoop.Cycle], error)
	GetRecoveriesForRange(ctx context.Context, start, end time.Time) (*DateRangeResult[whoop.Recovery], error)
	GetSleepsForRange(ctx context.Context, start, end time.Time) (*DateRangeResult[whoop.Sleep], error)

	// Calendar-specific (fetches recoveries and cycles for proper date mapping)
	GetCalendarData(ctx context.Context, month time.Time) (*CalendarData, error)

	// Historical bundle (30-day data for charts)
	GetHistoricalData(ctx context.Context, referenceDate time.Time, days int) (*HistoricalData, error)

	// SetAPIKey updates the API key on the underlying client (called after OAuth flow)
	SetAPIKey(apiKey string)
}

CacheService abstracts all caching decisions from the TUI. It wraps repository + API client, handling staleness, coverage detection, and deciding when to use cached data vs. fetching from the API.

type CalendarData added in v0.1.10

type CalendarData struct {
	Recoveries []whoop.Recovery
	Cycles     []whoop.Cycle
	FromCache  bool
}

CalendarData bundles recoveries and cycles for calendar display.

type DateRangeResult

type DateRangeResult[T any] struct {
	Records      []T
	FromCache    bool
	PartialCache bool // some from cache, some from API
}

DateRangeResult wraps a collection of records with cache metadata.

type DefaultStalenessChecker

type DefaultStalenessChecker struct {
	// contains filtered or unexported fields
}

DefaultStalenessChecker implements time-based staleness rules: - UNSCORABLE: never refresh (data is final) - PENDING_SCORE: refresh after 5 minutes (waiting for scoring) - SCORED + today: refresh after 15 minutes (might have updates) - SCORED + historical: refresh after 24 hours (very stable)

func NewDefaultStalenessChecker

func NewDefaultStalenessChecker() *DefaultStalenessChecker

NewDefaultStalenessChecker creates a StalenessChecker with sensible defaults.

func (*DefaultStalenessChecker) ShouldRefresh

func (c *DefaultStalenessChecker) ShouldRefresh(scoreState whoop.ScoreState, dataDate, fetchedAt time.Time) bool

ShouldRefresh implements StalenessChecker.

type HistoricalData

type HistoricalData struct {
	Cycles     []whoop.Cycle
	Recoveries []whoop.Recovery
	Sleeps     []whoop.Sleep
	Workouts   []whoop.Workout
	FromCache  bool
}

HistoricalData bundles multiple data types for chart display.

type NeverStaleChecker

type NeverStaleChecker struct{}

func (*NeverStaleChecker) ShouldRefresh

func (c *NeverStaleChecker) ShouldRefresh(scoreState whoop.ScoreState, dataDate, fetchedAt time.Time) bool

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service implements CacheService by wrapping a repository and API client.

func NewService

func NewService(client *whoop.Client, repo *repository.Repository) *Service

NewService creates a CacheService with the given dependencies.

func (*Service) GetCalendarData added in v0.1.10

func (s *Service) GetCalendarData(ctx context.Context, month time.Time) (*CalendarData, error)

GetCalendarRecoveries fetches recovery data for a calendar month. This is optimized to only fetch recoveries (not cycles), since the calendar only needs recovery scores for coloring days.

func (*Service) GetCycleForDate

func (s *Service) GetCycleForDate(ctx context.Context, date time.Time) (*CacheResult[*whoop.Cycle], error)

GetCycleForDate fetches the cycle for a date, checking cache first.

func (*Service) GetCyclesForRange

func (s *Service) GetCyclesForRange(ctx context.Context, start, end time.Time) (*DateRangeResult[whoop.Cycle], error)

GetCyclesForRange fetches cycles within a date range.

func (*Service) GetHistoricalData

func (s *Service) GetHistoricalData(ctx context.Context, referenceDate time.Time, days int) (*HistoricalData, error)

GetHistoricalData fetches bundled historical data for charts.

func (*Service) GetRecoveriesForRange

func (s *Service) GetRecoveriesForRange(ctx context.Context, start, end time.Time) (*DateRangeResult[whoop.Recovery], error)

GetRecoveriesForRange fetches recoveries within a date range.

func (*Service) GetRecoveryForCycle

func (s *Service) GetRecoveryForCycle(ctx context.Context, cycleID int64) (*CacheResult[*whoop.Recovery], error)

GetRecoveryForCycle fetches a recovery by cycle ID.

func (*Service) GetSleepForCycle

func (s *Service) GetSleepForCycle(ctx context.Context, cycleID int64) (*CacheResult[*whoop.Sleep], error)

GetSleepForCycle fetches sleep data for a cycle ID.

func (*Service) GetSleepsForRange

func (s *Service) GetSleepsForRange(ctx context.Context, start, end time.Time) (*DateRangeResult[whoop.Sleep], error)

GetSleepsForRange fetches sleeps within a date range.

func (*Service) GetWorkoutsForDateRange

func (s *Service) GetWorkoutsForDateRange(ctx context.Context, start, end time.Time) (*CacheResult[[]whoop.Workout], error)

GetWorkoutsForDateRange fetches workouts within a date range.

func (*Service) SetAPIKey

func (s *Service) SetAPIKey(apiKey string)

SetAPIKey updates the API key on the underlying whoop client.

func (*Service) WithStalenessChecker

func (s *Service) WithStalenessChecker(checker StalenessChecker) *Service

WithStalenessChecker sets a custom staleness checker.

type StalenessChecker

type StalenessChecker interface {
	// ShouldRefresh returns true if the cached data is stale and should be re-fetched.
	ShouldRefresh(scoreState whoop.ScoreState, dataDate, fetchedAt time.Time) bool
}

StalenessChecker determines whether cached data should be refreshed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL