utils

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeTraitUsageDuration

func ComputeTraitUsageDuration(start, end time.Time, traitName string, traitValue any, usages ...*v1alpha1.ResourceUsage) (time.Duration, time.Duration, error)

ComputeTraitUsageDuration works similarly to ComputeUsageDuration, but instead of the resource's usage, it computes the total duration for which a specific trait had a specific value within the given time frame. The second return value contains the remainder duration for which no tracking information exists within the given ResourceUsage objects. It will include the duration for which the ResourceUsage objects exist, but do not contain any information about the given trait (usually happens if the configuration was changed to contain the trait at some point and the data comes from before and after that change). The trait's value can be passed in either as raw JSON ([]byte type) or as a Go value (any type). In the latter case, the value will be marshaled to JSON before comparison. The same points mentioned for ComputeUsageDuration also apply here.

func ComputeUsageDuration

func ComputeUsageDuration(start, end time.Time, usages ...*v1alpha1.ResourceUsage) (time.Duration, time.Duration, error)

ComputeUsageDuration takes a start and end time, and any amount of ResourceUsage objects. It computes the total duration within start (inclusive) and end (exclusive) for which the resource was tracked. The second return value contains the remainder duration for which no tracking information exists within the given ResourceUsage objects. Returns an error, if the end time is earlier than the start time. Also returns an error, if any of the given ResourceUsage objects does not have a valid tracking period (start and end time must be set and the start time must be earlier than the end time). A few points should be noted for this function to behave as expected:

  • All given ResourceUsage objects are taken into account. This means that only ResourceUsage objects belonging to the same resource (GVK + namespace + name) should be passed to this function, and also only completed ones, unless data from ongoing ResourceUsage objects is explicitly desired.
  • There should never exist any future ResourceUsage objects, which means that the end time should never be later than 'now'.
  • All returned durations are truncated to the nearest minute, any seconds or smaller units are discarded.
  • The sum of the first and second return values will never be greater than the duration between start and end.

func ComputeUsageDurationWithTraits

func ComputeUsageDurationWithTraits(start, end time.Time, usages ...*v1alpha1.ResourceUsage) (time.Duration, time.Duration, map[string]TraitValueDurations, error)

ComputeUsageDurationWithTraits works similarly to ComputeUsageDuration, but in addition to the total usage duration and the remainder duration, it also computes the total duration for which each trait had each value within the given time frame. The same points mentioned for ComputeUsageDuration also apply here.

func Pointerize

func Pointerize[T any](objects []T) []*T

Pointerize takes a slice of objects and returns a slice of pointers to those objects. It can be used to make the <object>List.Items slice from client.List calls compatible with the functions below.

func RawJSONValueEqual

func RawJSONValueEqual(a, b []byte) bool

RawJSONValueEqual compares two raw JSON values and returns true if they are equal, false otherwise. This is basically bytes.Equal, with the exception that an empty/nil value is considered equal to one containing only 'null'.

Types

type TraitValueDuration

type TraitValueDuration struct {
	// Value is the value of the trait.
	// +nullable
	Value apiextensionsv1.JSON `json:"value"`
	// Duration is the duration for which the trait had this value.
	Duration time.Duration `json:"duration"`
}

func (*TraitValueDuration) ValueAs

func (tvd *TraitValueDuration) ValueAs(target any) error

ValueAs unmarshals the trait value's raw JSON into the given pointer target.

type TraitValueDurations

type TraitValueDurations []*TraitValueDuration

func (TraitValueDurations) GetDominantTraitValue

func (tvds TraitValueDurations) GetDominantTraitValue(includeNull bool) *TraitValueDuration

GetDominantTraitValue returns the *TraitValueDuration with the longest duration. In case of a tie, the first one encountered is returned. If includeNull is false, any TraitValueDuration with a value of 'null' will be ignored. If there are no TraitValueDurations or all of them are ignored, nil is returned (no error).

func (TraitValueDurations) GetDurationForValue

func (tvds TraitValueDurations) GetDurationForValue(value any) (time.Duration, error)

GetDurationForValue returns the duration for which the trait had the given value. The value can be passed in either as raw JSON ([]byte type) or as a Go value (any type). In the latter case, the value will be marshaled to JSON before comparison. If the value is not found in the list, a duration of 0 is returned (no error).

Jump to

Keyboard shortcuts

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