store

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package store keeps Fundarr's state in one SQLite file: configured sources and destinations (secrets sealed), the normalized activity history, the latest balances, what was delivered where, and the run log.

Index

Constants

View Source
const TombstoneKeep = 180 * 24 * time.Hour

TombstoneKeep is how long a removal stays in the feed.

Variables

View Source
var ErrNotFound = errors.New("not found")

Functions

This section is empty.

Types

type APIToken

type APIToken struct {
	ID       string    `json:"id"`
	Name     string    `json:"name"`
	Created  time.Time `json:"created"`
	LastUsed time.Time `json:"lastUsed"`
	// NotifyURL, when set, is told after each change to the history.
	NotifyURL string `json:"notifyUrl"`
}

APIToken lets another program read Fundarr's data (the /api/v1 routes).

type ActivityFilter

type ActivityFilter struct {
	SourceID string
	Since    time.Time
	Limit    int
	Offset   int
	// Undelivered: only those without a delivered/duplicate/skipped result
	// for this destination.
	UndeliveredTo string
	// Newest lists the most recent first (oldest first otherwise).
	Newest bool
	// SettledBefore leaves out adjustments made after this time: a balance
	// correction waits a while before going out, in case the platform's
	// history catches up.
	SettledBefore time.Time
}

ActivityFilter selects activities; zero fields mean no filter.

type Adjustment

type Adjustment struct {
	model.Activity
	Created   time.Time `json:"created"`
	Delivered bool      `json:"delivered"` // sent to at least one destination
}

Adjustment is a balance correction with what the UI needs about it.

type Change

type Change struct {
	Seq  int64
	ID   string
	Data []byte
}

Change is a feed row: Data is empty when the activity was removed.

type Delivery

type Delivery struct {
	DestinationID string    `json:"destinationId"`
	ActivityID    string    `json:"activityId"`
	Status        string    `json:"status"`
	ExternalID    string    `json:"externalId,omitempty"`
	Message       string    `json:"message,omitempty"`
	At            time.Time `json:"at"`
}

type Entity

type Entity struct {
	ID       string            `json:"id"`
	Kind     string            `json:"kind"`
	Name     string            `json:"name"`
	Settings map[string]string `json:"settings"`
	Secrets  []byte            `json:"-"` // sealed JSON map
	State    []byte            `json:"-"`
	Enabled  bool              `json:"enabled"`
	Interval time.Duration     `json:"-"`
	Created  time.Time         `json:"created"`
	LastRun  time.Time         `json:"lastRun"`
	LastOK   time.Time         `json:"lastOk"`
	LastErr  string            `json:"lastError"`
}

Entity is a configured source or destination.

type FeedItem

type FeedItem struct {
	ID   string
	Data []byte
}

FeedItem is one activity as apps see it, encoded.

type FeedState

type FeedState struct {
	// Epoch changes when the feed starts over (a new database): cursors
	// of another epoch are meaningless.
	Epoch string
	// Floor: tombstones up to it were forgotten. Head: the last number.
	Floor, Head int64
}

FeedState is what cursors are checked against.

type NotifyTarget

type NotifyTarget struct {
	ID, URL string
	Secret  []byte // sealed
}

NotifyTarget is an app to tell about changes.

type PriceWindow

type PriceWindow = price.Window

PriceWindow is a range of days already asked to a provider.

type Route

type Route struct {
	DestinationID string    `json:"destinationId"`
	SourceID      string    `json:"sourceId"`
	Account       string    `json:"account"`
	Since         time.Time `json:"since"` // only deliver activities from this date (zero: all)
	// Accounts sends a sub-account of the source (a PEA beside a
	// compte-titres) to another destination account; the others go to
	// Account.
	Accounts map[string]string `json:"accounts,omitempty"`
}

func (Route) To added in v0.2.0

func (r Route) To(sub string) string

To is the destination account an activity of sub-account sub goes to.

type Run

type Run struct {
	ID        int64     `json:"id"`
	Kind      string    `json:"kind"` // "sync" or "push"
	SubjectID string    `json:"subjectId"`
	Started   time.Time `json:"started"`
	Ended     time.Time `json:"ended"`
	OK        *bool     `json:"ok"`
	Summary   string    `json:"summary"`
	Log       string    `json:"log,omitempty"`
}

type Statement

type Statement struct {
	ID         int64     `json:"id"`
	SourceID   string    `json:"sourceId"`
	Name       string    `json:"name"`
	From       time.Time `json:"from"`
	To         time.Time `json:"to"`
	Activities int       `json:"activities"`
	At         time.Time `json:"at"`
	// Accounts: the sub-accounts it covers (nil: all).
	Accounts []string `json:"accounts,omitempty"`
	// Synced: downloaded by a sync, which replaces its previous copy
	// (same name) instead of adding a statement.
	Synced bool `json:"synced,omitempty"`
}

Statement is an imported platform statement: the reference for its period.

type Store

type Store struct {

	// Clock dates new rows (replaceable in tests).
	Clock func() time.Time
	// contains filtered or unexported fields
}

func Open

func Open(path string) (*Store, error)

func (*Store) APITokens

func (s *Store) APITokens(ctx context.Context) ([]APIToken, error)

func (*Store) Activities

func (s *Store) Activities(ctx context.Context, f ActivityFilter) ([]model.Activity, error)

func (*Store) Activity

func (s *Store) Activity(ctx context.Context, sourceID, id string) (*model.Activity, error)

Activity returns one activity of a source.

func (*Store) AddActivities

func (s *Store) AddActivities(ctx context.Context, acts []model.Activity) (int, error)

AddActivities inserts new activities and ignores ones already known (same source and native id). Returns how many were new.

func (*Store) Adjustments

func (s *Store) Adjustments(ctx context.Context, sourceID string) ([]Adjustment, error)

Adjustments lists a source's balance corrections, oldest first.

func (*Store) ApplyFeed

func (s *Store) ApplyFeed(ctx context.Context, items []FeedItem) (int, error)

ApplyFeed makes the feed match items, the whole current set, in one transaction: what is new or different gets a new sequence number, what is gone becomes a tombstone. Returns how many rows changed.

func (*Store) Balances

func (s *Store) Balances(ctx context.Context, sourceID string) ([]model.Balance, time.Time, error)

func (*Store) BalancesDerived added in v0.2.0

func (s *Store) BalancesDerived(ctx context.Context, sourceID string) bool

BalancesDerived reports whether a source's balances come from its history rather than the platform.

func (*Store) Changes

func (s *Store) Changes(ctx context.Context, after int64, limit int) ([]Change, bool, error)

Changes returns up to limit rows after seq, in order, and whether more follow. From 0 (a first copy) tombstones are left out.

func (*Store) CheckAPIToken

func (s *Store) CheckAPIToken(ctx context.Context, token string) bool

CheckAPIToken says whether token is a live one, and marks it used.

func (*Store) Close

func (s *Store) Close() error

func (*Store) CloseInterrupted

func (s *Store) CloseInterrupted(ctx context.Context) (int64, error)

CloseInterrupted marks runs left open by a stop or crash as failed.

func (*Store) CountActivities

func (s *Store) CountActivities(ctx context.Context, sourceID string) (int, error)

func (*Store) CreateAPIToken

func (s *Store) CreateAPIToken(ctx context.Context, name string) (APIToken, string, error)

CreateAPIToken makes a token and returns it: this is the only time it can be read.

func (*Store) DeleteAPIToken

func (s *Store) DeleteAPIToken(ctx context.Context, id string) error

func (*Store) DeleteActivities

func (s *Store) DeleteActivities(ctx context.Context, sourceID string, ids []string) error

DeleteActivities removes some of a source's activities (and what was recorded about their delivery).

func (*Store) DeleteDestination

func (s *Store) DeleteDestination(ctx context.Context, id string) error

func (*Store) DeleteRoute

func (s *Store) DeleteRoute(ctx context.Context, destID, srcID string) error

func (*Store) DeleteSource

func (s *Store) DeleteSource(ctx context.Context, id string) error

func (*Store) DeliveryCounts

func (s *Store) DeliveryCounts(ctx context.Context, destID string) (map[string]int, error)

DeliveryCounts returns, for a destination, how many activities are in each status.

func (*Store) Destination

func (s *Store) Destination(ctx context.Context, id string) (*Entity, error)

func (*Store) DestinationError

func (s *Store) DestinationError(ctx context.Context, id, msg string) error

func (*Store) Destinations

func (s *Store) Destinations(ctx context.Context) ([]*Entity, error)

func (*Store) EndRun

func (s *Store) EndRun(ctx context.Context, id int64, ok bool, summary, log string) error

func (*Store) FeedState

func (s *Store) FeedState(ctx context.Context) (FeedState, error)

func (*Store) NotifyTargets

func (s *Store) NotifyTargets(ctx context.Context) ([]NotifyTarget, error)

func (*Store) Ping

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

func (*Store) PriceSeries

func (s *Store) PriceSeries(ctx context.Context, pair string) (map[string]string, error)

PriceSeries returns the daily closes stored for a market, by day.

func (*Store) PriceWindows

func (s *Store) PriceWindows(ctx context.Context, pair string) ([]PriceWindow, error)

func (*Store) PutBalances

func (s *Store) PutBalances(ctx context.Context, sourceID string, at time.Time, b []model.Balance) error

func (*Store) PutBalancesFrom added in v0.2.0

func (s *Store) PutBalancesFrom(ctx context.Context, sourceID string, at time.Time, b []model.Balance, derived bool) error

PutBalancesFrom records balances; derived: the platform did not say, they are what the history adds up to.

func (*Store) PutDeliveries

func (s *Store) PutDeliveries(ctx context.Context, ds []Delivery) error

func (*Store) PutDestination

func (s *Store) PutDestination(ctx context.Context, e *Entity) error

func (*Store) PutPrices

func (s *Store) PutPrices(ctx context.Context, pair string, w PriceWindow, closes map[string]string) error

PutPrices stores closes fetched for a window of days.

func (*Store) PutRoute

func (s *Store) PutRoute(ctx context.Context, r Route) error

func (*Store) PutSetting

func (s *Store) PutSetting(ctx context.Context, key, value string) error

func (*Store) PutSource

func (s *Store) PutSource(ctx context.Context, e *Entity) error

func (*Store) PutSourceState added in v0.2.0

func (s *Store) PutSourceState(ctx context.Context, id string, state []byte) error

PutSourceState replaces a source's state outside a sync (after a sign-in, or when a sync ended because the session had expired).

func (s *Store) PutTransferLink(ctx context.Context, l TransferLink) error

PutTransferLink records a choice; kind "" forgets the choices about out and in (back to automatic matching).

func (*Store) ReplacePeriod

func (s *Store) ReplacePeriod(ctx context.Context, st Statement, acts []model.Activity) error

ReplacePeriod swaps a source's history between from and to (inclusive), in the statement's accounts, for acts, and records the statement, in one transaction. Activities that are in both keep their deliveries.

func (*Store) ResetDeliveries

func (s *Store) ResetDeliveries(ctx context.Context, destID, sourceID string) error

ResetDeliveries forgets what was delivered to a destination (to push again).

func (*Store) Routes

func (s *Store) Routes(ctx context.Context) ([]Route, error)

func (*Store) Runs

func (s *Store) Runs(ctx context.Context, subject string, limit int) ([]Run, error)

func (*Store) SetNotify

func (s *Store) SetNotify(ctx context.Context, id, url string, secret []byte) error

SetNotify sets where a token's app is told about changes, with the sealed secret that signs the calls; url "" stops it.

func (*Store) Setting

func (s *Store) Setting(ctx context.Context, key string) (string, error)

Setting returns a stored setting, "" when unset.

func (*Store) Source

func (s *Store) Source(ctx context.Context, id string) (*Entity, error)

func (*Store) SourceRan

func (s *Store) SourceRan(ctx context.Context, id string, at time.Time, runErr error, state []byte) error

SourceRan records the outcome of a sync; state is only replaced on success.

func (*Store) Sources

func (s *Store) Sources(ctx context.Context) ([]*Entity, error)

func (*Store) StartRun

func (s *Store) StartRun(ctx context.Context, kind, subject string) (int64, error)

func (*Store) Statements

func (s *Store) Statements(ctx context.Context, sourceID string) ([]Statement, error)

func (*Store) SubAccounts added in v0.2.0

func (s *Store) SubAccounts(ctx context.Context, sourceID string) ([]string, error)

SubAccounts lists the sub-accounts a source's history uses, besides the main one ("").

func (s *Store) TransferLinks(ctx context.Context) ([]TransferLink, error)
type TransferLink struct {
	OutID string `json:"outId"`
	InID  string `json:"inId"`
	Kind  string `json:"kind"` // "pair" or "refuse"
}

TransferLink is a choice the user made about a withdrawal and a deposit.

Jump to

Keyboard shortcuts

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