meter

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2025 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Aggregation

type Aggregation struct {
	Type types.AggregationType `json:"type"`

	// Field is the key in $event.properties on which the aggregation is to be applied
	// For ex if the aggregation type is sum for API usage, the field could be "duration_ms"
	Field string `json:"field,omitempty"`
}

type Filter

type Filter struct {
	// Key is the key for the filter from $event.properties
	// Currently we support only first level keys in the properties and not nested keys
	Key string `json:"key"`

	// Values are the possible values for the filter to be considered for the meter
	// For ex "model_name" could have values "o1-mini", "gpt-4o" etc
	Values []string `json:"values"`
}

type Meter

type Meter struct {
	// ID is the unique identifier for the meter
	ID string `db:"id" json:"id"`

	// EventName is the unique identifier for the event that this meter is tracking
	// It is a mandatory field in the events table and hence being used as the primary matching field
	// We can have multiple meters tracking the same event but with different filters and aggregation
	EventName string `db:"event_name" json:"event_name"`

	// Name is the display name of the meter
	Name string `db:"name" json:"name"`

	// Aggregation defines the aggregation type and field for the meter
	// It is used to aggregate the events into a single value for calculating the usage
	Aggregation Aggregation `db:"aggregation" json:"aggregation"`

	// Filters define the criteria for the meter to be applied on the events before aggregation
	// It also defines the possible values on which later the charges will be applied
	Filters []Filter `db:"filters" json:"filters"`

	// ResetUsage defines whether the usage should be reset periodically or not
	// For ex meters tracking total storage used do not get reset but meters tracking
	// total API requests do.
	ResetUsage types.ResetUsage `db:"reset_usage" json:"reset_usage"`

	// EnvironmentID is the environment identifier for the meter
	EnvironmentID string `db:"environment_id" json:"environment_id"`

	// BaseModel is the base model for the meter
	types.BaseModel
}

func FromEnt

func FromEnt(e *ent.Meter) *Meter

FromEnt converts an Ent Meter to a domain Meter

func FromEntList

func FromEntList(list []*ent.Meter) []*Meter

FromEntList converts a list of Ent Meters to domain Meters

func NewMeter

func NewMeter(name string, tenantID, createdBy string) *Meter

Constructor for creating new meters with defaults

func (*Meter) ToEntAggregation

func (m *Meter) ToEntAggregation() schema.MeterAggregation

ToEntAggregation converts domain Aggregation to Ent Aggregation

func (*Meter) ToEntFilters

func (m *Meter) ToEntFilters() []schema.MeterFilter

ToEntFilters converts domain Filters to Ent Filters

func (*Meter) Validate

func (m *Meter) Validate() error

Validate validates the meter configuration

type Repository

type Repository interface {
	CreateMeter(ctx context.Context, meter *Meter) error
	GetMeter(ctx context.Context, id string) (*Meter, error)
	List(ctx context.Context, filter *types.MeterFilter) ([]*Meter, error)
	ListAll(ctx context.Context, filter *types.MeterFilter) ([]*Meter, error)
	Count(ctx context.Context, filter *types.MeterFilter) (int, error)
	DisableMeter(ctx context.Context, id string) error
	UpdateMeter(ctx context.Context, id string, filters []Filter) error
}

Jump to

Keyboard shortcuts

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