consumption

package
v1.4.13 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package consumption contains storage backends for recent tea consumption events.

Index

Constants

This section is empty.

Variables

View Source
var ErrNilDB = errors.New("pg consumption: nil db")

ErrNilDB is returned when the PGStore has not been initialized with a database handle.

Functions

func EnsurePGConsumptionsTable added in v1.4.5

func EnsurePGConsumptionsTable(ctx context.Context, db *sql.DB) error

EnsurePGConsumptionsTable can be used at startup to verify the schema exists. It's a light guard and not a full migration mechanism.

Types

type Consumption

type Consumption struct {
	TeaID uuid.UUID
	Time  time.Time
}

Consumption represents a single tea consumption event.

type MemoryStore

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

MemoryStore is a simple in-memory implementation of Store. It is process-local and not persisted across restarts.

func NewMemoryStore

func NewMemoryStore(retention time.Duration) *MemoryStore

func (*MemoryStore) Recent

func (m *MemoryStore) Recent(_ context.Context, userID uuid.UUID, since time.Time) ([]Consumption, error)

func (*MemoryStore) Record

func (m *MemoryStore) Record(_ context.Context, userID uuid.UUID, teaID uuid.UUID, ts time.Time) error

type PGStore added in v1.4.5

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

PGStore is a PostgreSQL-backed implementation of Store. It expects the schema defined in db/schema.sql (table: consumptions) and delegates SQL execution to code generated by sqlc (pkg/pgstore).

func NewPGStore added in v1.4.5

func NewPGStore(pg *sql.DB, retention time.Duration) *PGStore

NewPGStore creates a PostgreSQL-backed consumption store with the given retention window. If retention <= 0, a default of 30 days is used.

func (*PGStore) Ping added in v1.4.5

func (s *PGStore) Ping(ctx context.Context) error

Ping checks PG connectivity for this store.

func (*PGStore) Recent added in v1.4.5

func (s *PGStore) Recent(ctx context.Context, userID uuid.UUID, since time.Time) ([]Consumption, error)

Recent returns consumption events since the given time for the user, ordered by time descending.

func (*PGStore) Record added in v1.4.5

func (s *PGStore) Record(ctx context.Context, userID uuid.UUID, teaID uuid.UUID, ts time.Time) error

Record stores a consumption event for a user at a given timestamp, enforcing the retention window by pruning older entries for that user.

type Store

type Store interface {
	Record(ctx context.Context, userID uuid.UUID, teaID uuid.UUID, ts time.Time) error
	// Recent returns consumptions since the provided time (inclusive) for the given user.
	Recent(ctx context.Context, userID uuid.UUID, since time.Time) ([]Consumption, error)
}

Store defines operations for recording and querying recent tea consumption.

Jump to

Keyboard shortcuts

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