engine

package
v1.0.0-beta.233 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrioritizeGrants

func PrioritizeGrants(grants []grant.Grant)

PrioritizeGrants orders grants in place by burn-down precedence. Lower priority numbers, earlier expirations, and earlier creation cursors burn down first.

Types

type Engine

type Engine interface {
	// Burns down all grants in the defined period by the usage amounts.
	//
	// When the engine outputs a balance, it doesn't discriminate what should be in that balance.
	// If a grant is inactive at the end of the period, it will still be in the output.
	Run(ctx context.Context, params RunParams) (RunResult, error)
}

func NewEngine

func NewEngine(conf EngineConfig) Engine

type EngineConfig

type EngineConfig struct {
	QueryUsage QueryUsageFn
}

type GrantBurnDownHistory

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

func NewGrantBurnDownHistory

func NewGrantBurnDownHistory(segments []GrantBurnDownHistorySegment, startingSnapshot balance.Snapshot) (GrantBurnDownHistory, error)

NewGrantBurnDownHistory creates a history anchored to startingSnapshot. Segments must continuously cover time beginning at the snapshot.

func (GrantBurnDownHistory) ChunkByResets

func (g GrantBurnDownHistory) ChunkByResets() []GrantBurnDownHistory

func (*GrantBurnDownHistory) GetPeriods

func (g *GrantBurnDownHistory) GetPeriods() []timeutil.ClosedPeriod

func (*GrantBurnDownHistory) GetSnapshotAtStartOfSegment

func (g *GrantBurnDownHistory) GetSnapshotAtStartOfSegment(segmentIndex int) (balance.Snapshot, error)

func (GrantBurnDownHistory) MarshalJSON

func (g GrantBurnDownHistory) MarshalJSON() ([]byte, error)

func (*GrantBurnDownHistory) Overage

func (g *GrantBurnDownHistory) Overage() float64

func (*GrantBurnDownHistory) Segments

func (GrantBurnDownHistory) TotalGrantUsage

func (g GrantBurnDownHistory) TotalGrantUsage() alpacadecimal.Decimal

func (*GrantBurnDownHistory) TotalUsageInHistory

func (g *GrantBurnDownHistory) TotalUsageInHistory() float64

type GrantBurnDownHistorySegment

type GrantBurnDownHistorySegment struct {
	timeutil.ClosedPeriod
	BalanceAtStart     balance.Map
	TerminationReasons SegmentTerminationReason // Reason why the segment was terminated (could be multiple taking effect at same time)
	TotalUsage         float64                  // Total usage of the feature in the Period
	OverageAtStart     float64                  // Usage beyond what could be burnt down from the grants in the previous segment (if any)
	Overage            float64                  // Usage beyond what cloud be burnt down from the grants
	GrantUsages        GrantUsages              // Grant usages in the segment order by grant priority
}

GrantBurnDownHistorySegment represents the smallest segment of grant usage which we store and calculate.

A non-rollover segment represents a period of time in which: 1) The grant priority does not change 2) Grants do not recurr 3) There was no usage reset

A rollover segment is an instantaneous reset transition with no metered usage. Its starting balance is the rolled-over grant balance, and its grant usages capture preserved overage burnt from grants in the new usage period.

It is not necessarily the largest such segment.

func (GrantBurnDownHistorySegment) ApplyUsage

func (s GrantBurnDownHistorySegment) ApplyUsage() balance.Map

Returns GrantBalanceMap at the end of the segment

type GrantUsage

type GrantUsage struct {
	GrantID           string
	Usage             float64
	TerminationReason GrantUsageTerminationReason
}

type GrantUsageTerminationReason

type GrantUsageTerminationReason string
const (
	GrantUsageTerminationReasonExhausted          GrantUsageTerminationReason = "GRANT_EXHAUSTED"     // Grant has been fully used
	GrantUsageTerminationReasonSegmentTermination GrantUsageTerminationReason = "SEGMENT_TERMINATION" // Segment has been terminated
)

func (GrantUsageTerminationReason) IsValid

type GrantUsages

type GrantUsages []GrantUsage

func (GrantUsages) Sum

type QueryUsageFn

type QueryUsageFn func(ctx context.Context, from, to time.Time) (float64, error)

TODO: should return alpacadecimal instead of float64, its fine to hard depend on it for now

type RunParams

type RunParams struct {
	// Meter for the current run.
	Meter meter.Meter
	// List of all grants that are active at the relevant period at some point.
	Grants []grant.Grant
	// End of the period to burn down the grants for.
	Until time.Time
	// Starting snapshot of the balances at the START OF THE PERIOD.
	StartingSnapshot balance.Snapshot
	// ResetBehavior defines the behavior of the engine when a reset is encountered.
	ResetBehavior grant.ResetBehavior
	// Timeline of the resets that occurred in the period.
	// The resets must occur AFTER the starting snapshot and NOT AFTER the until time. (exclusive - inclusive)
	Resets timeutil.SimpleTimeline
}

func (RunParams) Clone

func (p RunParams) Clone() RunParams

type RunResult

type RunResult struct {
	// Snapshot of the balances at the END OF THE PERIOD.
	Snapshot balance.Snapshot
	// History of the grant burn down.
	History GrantBurnDownHistory
	// RunParams used to produce the result.
	RunParams RunParams
}

type SegmentTerminationReason

type SegmentTerminationReason struct {
	PriorityChange bool
	Recurrence     []string // Grant IDs
	UsageReset     bool
	// Rollover marks grant balance rollover followed by preserved overage burn.
	Rollover bool
}

Jump to

Keyboard shortcuts

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