dosage

package
v0.0.0-...-7f6f21a Latest Latest
Warning

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

Go to latest
Published: May 10, 2026 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoDoseMatched = errors.New("no dose matched")
)
View Source
var Module = fx.Module("dosage",
	fx.Decorate(func(slog *slog.Logger) *slog.Logger {
		return slog.With("module", "dosage")
	}),
	fx.Provide(
		NewExporterService,
		NewDosageReminderService,
	),
)

Functions

This section is empty.

Types

type CSVDoseRecord

type CSVDoseRecord struct {
	DeliveryMethod string  `csv:"deliveryMethod"`
	Dose           float32 `csv:"dose"`
	TakenAt        string  `csv:"takenAt"`    // RFC3339
	TakenOffAt     string  `csv:"takenOffAt"` // RFC3339 or ""
	Comment        string  `csv:"comment"`
}

CSVDoseRecord is a single dose record in a CSV file. This is version 1.

type DeliveryMethod

type DeliveryMethod = openapi.DeliveryMethod

DeliveryMethod describes a method of delivery for medication.

type Dosage

type Dosage = openapi.Dosage

Dosage describes a dosage schedule.

type DosageReminder

type DosageReminder struct {
	// UserSecret is the secret of the user.
	UserSecret user.Secret
	// Username is the username of the user.
	Username string
	// Dosage is the dosage information of the user.
	Dosage Dosage
	// LastDose is the last dose taken by the user.
	LastDose Dose
	// LastRemindedDose is the TakenAt time of the last reminded dose.
	// This field is optional and is only set if the reminder was recorded.
	//
	// Deprecated: consider removing this field entirely, as it is not used in any
	// calculations anymore.
	LastRemindedDose *time.Time
	// LastRemindedAt is the time when the reminder was last sent, if it was at
	// all.
	LastRemindedAt *time.Time // sent_at
	// SnoozedUntil is the time until the reminder is snoozed.
	// This field is optional and is only set if the reminder is snoozed by the
	// user on the last notification.
	SnoozedUntil *time.Time
}

DosageReminder is a reminder for a dosage.

func (DosageReminder) NextNotification

func (r DosageReminder) NextNotification() (time.Time, bool)

NextNotification returns the time of the next notification only if it should have been sent, meaning that the notification time is in the past of [now].

It returns the snoozed time if the reminder is snoozed, otherwise it returns the time of the last dose plus the interval.

type DosageReminderService

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

DosageReminderService is a service for managing dosage reminders.

func NewDosageReminderService

func NewDosageReminderService(
	storage DosageReminderStorage,
	notifs notification.UserNotificationService,
	slog *slog.Logger,
	lc fx.Lifecycle,
) *DosageReminderService

NewDosageReminderService creates a new DosageReminderService.

type DosageReminderStorage

type DosageReminderStorage interface {
	// UpcomingDosageReminders returns the upcoming dosage reminders as a
	// streaming iterator. A satisfying but least optimized implementation
	// can return every single person with a dosage schedule and history.
	//
	// No strict ordering is required, but the reminders should be returned in
	// the order of the last dosage time.
	//
	// Users with no dosage schedule or history should not be included in the
	// results.
	UpcomingDosageReminders(ctx context.Context) ([]DosageReminder, error)

	// RecordRemindedDoseAttempts records the reminded dose attempts.
	// This is used to mark the reminder as sent or failed.
	RecordRemindedDoseAttempts(ctx context.Context, remindedDoses []RemindedDoseAttempt) error
}

DosageReminderStorage is a storage for dosage reminder data.

type DosageStorage

type DosageStorage interface {
	// DeliveryMethods returns the available delivery methods.
	DeliveryMethods(ctx context.Context) ([]DeliveryMethod, error)
	// Dosage returns the dosage for a user.
	// If the user has no schedule yet, this returns nil.
	Dosage(ctx context.Context, secret user.Secret) (*Dosage, error)
	// SetDosage sets the dosage for a user.
	// The user secret is taken from the Schedule.
	SetDosage(ctx context.Context, secret user.Secret, s Dosage) error
	// ClearDosage clears the dosage for a user.
	ClearDosage(ctx context.Context, secret user.Secret) error
}

DosageStorage is a storage for dosage data.

type Dose

type Dose struct {
	// DeliveryMethod is the method of delivery for the medication
	// at the time the dose was taken.
	DeliveryMethod string
	// Dose is the amount of medication that was taken.
	Dose float32
	// TakenAt is the time the dose was taken.
	TakenAt time.Time
	// TakenOffAt is the time the dose was taken off the body.
	// This is only relevant if DeliveryMethod is "patch".
	TakenOffAt *time.Time
	// Comment is a comment about the dose.
	Comment string
}

Dose describes a dose of medication in time.

func (Dose) ToCSV

func (d Dose) ToCSV() CSVDoseRecord

func (Dose) ToOpenAPI

func (d Dose) ToOpenAPI() openapi.Dose

type DoseHistoryStorage

type DoseHistoryStorage interface {
	// RecordDose records a single dose. The dose observation's ID is returned.
	RecordDose(ctx context.Context, secret user.Secret, dose Dose) error
	// ImportDoses imports doses in bulk and returns the number of doses
	// imported.
	// This is a separate method from RecordDose to allow for more efficient
	// bulk imports.
	ImportDoses(ctx context.Context, secret user.Secret, doseSeq iter.Seq[Dose]) (int64, error)
	// EditDose edits a dose by the previous takenAt time.
	// All fields are updated.
	EditDose(ctx context.Context, secret user.Secret, doseTime time.Time, dose Dose) error
	// ForgetDoses forgets the given doses.
	ForgetDoses(ctx context.Context, secret user.Secret, doseTimes []time.Time) error
	// DoseHistory returns the history of a dosage schedule. If end is zero, it
	// is considered to be now. If begin is zero, it is considered to be
	// since the beginning of time.
	// The history is ordered by time taken, with the oldest dose first.
	// If there's an error, the returned sequence will yield the error with a
	// zero-value [Observation].
	DoseHistory(ctx context.Context, secret user.Secret, begin, end time.Time) ([]Dose, error)
}

DoseHistoryStorage is a storage for dose history data.

type ExportDoseHistoryOptions

type ExportDoseHistoryOptions struct {
	Begin  time.Time // zero means from beginning of time
	End    time.Time // zero means until now
	Format ExportFormat
}

ExportDoseHistoryOptions are options for exporting dose history as a CSV file.

type ExportFormat

type ExportFormat string

ExportFormat is the format of the export.

const (
	ExportCSV  ExportFormat = "text/csv"
	ExportJSON ExportFormat = "application/json"
)

func (ExportFormat) AsMIME

func (f ExportFormat) AsMIME() string

AsMIME returns itself.

type ExporterService

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

ExporterService exports dosage data to various formats.

func NewExporterService

func NewExporterService(storage DoseHistoryStorage, lc fx.Lifecycle, logger *slog.Logger) *ExporterService

NewExporterService creates a new CSVExporterService.

func (*ExporterService) ExportDoseHistory

func (s *ExporterService) ExportDoseHistory(ctx context.Context, out io.Writer, secret user.Secret, o ExportDoseHistoryOptions) (int64, error)

ExportDoseHistory exports the dose history of the user as a CSV file into the given writer. It returns the number of records exported.

func (*ExporterService) ImportDoseHistory

ImportDoseHistory imports dose history from a CSV file.

type ImportDoseHistoryOptions

type ImportDoseHistoryOptions struct {
	Format ExportFormat
}

ImportDoseHistoryOptions are options for importing dose history from a file.

type ImportDoseHistoryResult

type ImportDoseHistoryResult struct {
	Records   int64
	Succeeded int64
}

ImportDoseHistoryResult is the result of importing dose history from a file.

type RecordedDosesResult

type RecordedDosesResult struct {
	// Created is the number of doses that were created.
	// This is the number of doses that were not already in the database.
	Created int
}

RecordedDosesResult is the result of recording doses.

type RemindedDoseAttempt

type RemindedDoseAttempt struct {
	// UserSecret is the secret of the user.
	UserSecret user.Secret
	// RemindedAt is the time when the reminder was sent.
	RemindedAt time.Time
	// RemindedDose is the dose that was reminded.
	// This is the TakenAt time of the dose.
	RemindedDose time.Time
	// ClearSnooze is true if the snooze should be cleared.
	// This is the case if the reminder was sent at the snoozed time.
	ClearSnooze bool
	// ErrorReason is the error reason if the reminder failed, if any.
	ErrorReason *string
}

RemindedDoseAttempt is a dose that has been reminded.

Directories

Path Synopsis
Package openapi provides primitives to interact with the openapi HTTP API.
Package openapi provides primitives to interact with the openapi HTTP API.

Jump to

Keyboard shortcuts

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