engine

package
v1.0.0-beta.211 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrioritizeGrants

func PrioritizeGrants(grants []grant.Grant) error

The correct order to burn down grants is: 1. Grants with higher priority are burned down first 2. Grants with earlier expiration date are burned down first

TODO: figure out if this needs to return an error or not

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 {
	Granularity meter.WindowSize
	QueryUsage  QueryUsageFn
}

type GrantBurnDownHistory

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

func NewGrantBurnDownHistory

func NewGrantBurnDownHistory(segments []GrantBurnDownHistorySegment, usageAtStart balance.SnapshottedUsage) (GrantBurnDownHistory, error)

func (*GrantBurnDownHistory) GetSnapshotAtStartOfSegment

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

func (*GrantBurnDownHistory) GetUsageInPeriodUntilSegment

func (g *GrantBurnDownHistory) GetUsageInPeriodUntilSegment(segmentIndex int) (balance.SnapshottedUsage, error)

GetUsageInPeriodUntilSegment returns the SnapshottedUsage at the start of the given segment

func (*GrantBurnDownHistory) Overage

func (g *GrantBurnDownHistory) Overage() float64

func (*GrantBurnDownHistory) Segments

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        []GrantUsage             // Grant usages in the segment order by grant priority
}

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

A 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

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 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 {
	// 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
}

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
}

type SegmentTerminationReason

type SegmentTerminationReason struct {
	PriorityChange bool
	Recurrence     []string // Grant IDs
	UsageReset     bool
}

Jump to

Keyboard shortcuts

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