engine

package
v1.0.0-beta.168 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2024 License: Apache-2.0 Imports: 10 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, grants []grant.Grant, startingBalances balance.Map, startingOverage float64, period recurrence.Period) (endingBalances balance.Map, endingOverage float64, history []GrantBurnDownHistorySegment, err error)
}

func NewEngine

func NewEngine(getFeatureUsage QueryUsageFn, granuality models.WindowSize) Engine

type GrantBurnDownHistory

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

func NewGrantBurnDownHistory

func NewGrantBurnDownHistory(segments []GrantBurnDownHistorySegment) (*GrantBurnDownHistory, error)

func (*GrantBurnDownHistory) Overage

func (g *GrantBurnDownHistory) Overage() float64

func (*GrantBurnDownHistory) Segments

func (*GrantBurnDownHistory) TotalUsage

func (g *GrantBurnDownHistory) TotalUsage() float64

type GrantBurnDownHistorySegment

type GrantBurnDownHistorySegment struct {
	recurrence.Period
	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

func (*GrantBurnDownHistorySegment) ToSnapshot

Creates a GrantBalanceSnapshot from the starting state of the segment

type GrantUsage

type GrantUsage struct {
	GrantID           string
	Usage             float64
	TerminationReason GrantUsageTerminationReason
}

func BurnDownGrants

func BurnDownGrants(startingBalances balance.Map, prioritized []grant.Grant, usage float64) (balance.Map, []GrantUsage, float64, error)

Burns down the grants of the priority sorted list. Manages overage.

FIXME: calculations happen on inexact representations as float64, this can lead to rounding errors.

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)

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