Documentation
¶
Overview ¶
Package engine runs syncs (source → history) and pushes (history → destinations), on demand and on a schedule.
Index ¶
- Constants
- Variables
- type Engine
- func (e *Engine) Adjust(ctx context.Context, ent *store.Entity) (gaps []model.Gap, changed bool, err error)
- func (e *Engine) Build(ent *store.Entity) (source.Source, error)
- func (e *Engine) BuildDest(ent *store.Entity) (dest.Destination, error)
- func (e *Engine) Costs(ctx context.Context, l *Ledger, ccy string) map[string]decimal.Decimal
- func (e *Engine) Currency(ctx context.Context) string
- func (e *Engine) DeleteManual(ctx context.Context, sourceID, activityID string) error
- func (e *Engine) Import(ctx context.Context, id, name string, data []byte) (*ImportResult, error)
- func (e *Engine) Ledger(ctx context.Context) (*Ledger, error)
- func (e *Engine) Push(ctx context.Context, destID string) (*PushResult, error)
- func (e *Engine) Readjust(ctx context.Context, id string) error
- func (e *Engine) Reclassify(ctx context.Context, sourceID, activityID string, r Reclassify) (*model.Activity, error)
- func (e *Engine) Reconcile(ctx context.Context, sourceID string) ([]model.Gap, error)
- func (e *Engine) Resync(ctx context.Context, id string) (*SyncResult, error)
- func (e *Engine) Run(ctx context.Context)
- func (e *Engine) Running(id string) bool
- func (e *Engine) Seal(secrets map[string]string) ([]byte, error)
- func (e *Engine) Secrets(ent *store.Entity) (map[string]string, error)
- func (e *Engine) Sync(ctx context.Context, id string) (*SyncResult, error)
- func (e *Engine) Test(ctx context.Context, ent *store.Entity) error
- func (e *Engine) Tolerance() decimal.Decimal
- func (e *Engine) Transfers(ctx context.Context, acts []model.Activity) ([]model.Pair, error)
- func (e *Engine) UnitPrice(ctx context.Context, ccy string) func(model.Asset, time.Time) (decimal.Decimal, bool)
- func (e *Engine) Values(ent *store.Entity) (map[string]string, error)
- type ImportResult
- type Ledger
- type PushResult
- type Reclassify
- type SyncResult
Constants ¶
const AdjustSetting = "fundarr.adjust"
AdjustSetting, in a source's settings, turns balance corrections off ("off").
const Hold = 48 * time.Hour
Hold is how long a balance correction waits before going to destinations: a platform's history sometimes catches up a sync or two later.
Variables ¶
var ErrBusy = errors.New("already running")
var ErrDelivered = errors.New("this correction was already sent to a destination: remove it there first, then forget its delivery")
var ErrNoImport = errors.New("this source cannot import statements")
Functions ¶
This section is empty.
Types ¶
type Engine ¶
type Engine struct {
Store *store.Store
Box *secret.Box
HTTP *http.Client
Log *slog.Logger
// Prices values assets (corrections, rewards, transfers); nil leaves
// them without a price.
Prices *price.Service
// Now is replaceable in tests.
Now func() time.Time
// OnChange, when set, is called after the history may have changed
// (a sync, an import, a correction rewritten). It must not block.
OnChange func()
// contains filtered or unexported fields
}
func (*Engine) Adjust ¶
func (e *Engine) Adjust(ctx context.Context, ent *store.Entity) (gaps []model.Gap, changed bool, err error)
Adjust brings a source's corrections in line with its gaps: corrections not delivered anywhere yet are recomputed from scratch; delivered ones stay and are corrected by new ones if they turn out wrong. Returns the gaps that existed before correcting.
func (*Engine) Costs ¶
Costs is what each incoming asset cost, in ccy (see model.Costs). Without a price service, only what was bought with ccy itself is known.
func (*Engine) Currency ¶
Currency returns the currency values are shown in (Settings, default EUR).
func (*Engine) DeleteManual ¶
DeleteManual removes an activity the user entered; the balance check corrects again whatever it explained.
func (*Engine) Import ¶
Import reads a statement exported from the platform and makes it the history of the period it covers.
func (*Engine) Reclassify ¶
func (*Engine) Resync ¶
Resync fetches the whole history again, as on the first run (after a connector learned new kinds of activity). Known activities are deduplicated.
func (*Engine) Run ¶
Run syncs enabled sources whose interval has elapsed, every minute, until ctx is done. Retries failed pushes once an hour.
func (*Engine) Secrets ¶
Secrets returns the decrypted secrets alone (to keep them when a form is saved without retyping them).
func (*Engine) Tolerance ¶
Tolerance is the relative difference ignored when reconciling (rounding of displayed balances).
type ImportResult ¶
type ImportResult struct {
From time.Time `json:"from"`
To time.Time `json:"to"`
Activities int `json:"activities"`
Replaced int `json:"replaced"`
Notes []string `json:"notes"`
Gaps []model.Gap `json:"gaps"`
}
ImportResult summarizes a statement import.
type Ledger ¶
type Ledger struct {
Acts []model.Activity
Pairs []model.Pair
// In maps a deposit to its withdrawal, Out a withdrawal to its deposit.
In, Out map[string]string
ByID map[string]model.Activity
}
Ledger is everything that crosses sources, computed together.
type PushResult ¶
PushResult summarizes one push.
type Reclassify ¶
type Reclassify struct {
As string `json:"as"` // "reward", "deposit", "withdrawal", "buy", "sell", "fee"
Time time.Time `json:"time"`
Quote model.Amount `json:"quote"` // buy and sell: what was paid or received
Note string `json:"note"`
}
Reclassify replaces a balance correction with what really happened, told by the user: a reward, a transfer from another wallet of theirs, a purchase (or a sale) for some amount of another asset.
type SyncResult ¶
type SyncResult struct {
Fetched int `json:"fetched"`
New int `json:"new"`
// Adjusted: assets whose history did not add up and got a correction.
Adjusted int `json:"adjusted"`
// Gaps left unexplained (corrections turned off for the source).
Gaps []model.Gap `json:"gaps"`
}
SyncResult summarizes one sync.