transformations

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2025 License: Apache-2.0, MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSummarizeByFieldNotFound   = errors.New("summarize by field not found. Not applying summarize")
	ErrNotUniqueFieldNames        = errors.New("field names are not unique. Not applying filter")
	ErrEvaluatingFilterExpression = errors.New("error evaluating filter expression")
	ErrInvalidFilterExpression    = errors.New("invalid filter expression")

	ErrMergeTransformationNoFrameSupplied     = errors.New("no frames supplied for merge frame transformation")
	ErrMergeTransformationDifferentFields     = errors.New("unable to merge fields due to different fields")
	ErrMergeTransformationDifferentFieldNames = errors.New("unable to merge field due to different field names")
	ErrMergeTransformationDifferentFieldTypes = errors.New("unable to merge fields due to different field types")

	ErrEvaluatingFilterExpressionWithEmptyFrame = errors.Join(ErrEvaluatingFilterExpression, errors.New("unable to apply filter on nil frame"))
)
View Source
var ExpressionFunctions = map[string]govaluate.ExpressionFunction{
	"trim": func(arguments ...any) (any, error) {
		if len(arguments) < 1 {
			return nil, errors.New("invalid arguments to trim method")
		}
		if arg, ok := arguments[0].(*string); ok {
			return strings.TrimSpace(*arg), nil
		}
		if arg, ok := arguments[0].(string); ok {
			return strings.TrimSpace(arg), nil
		}
		return "", nil
	},
	"tolower": func(arguments ...any) (any, error) {
		if len(arguments) < 1 {
			return nil, errors.New("invalid arguments to tolower method")
		}
		if arg, ok := arguments[0].(*string); ok {
			return strings.ToLower(*arg), nil
		}
		if arg, ok := arguments[0].(string); ok {
			return strings.ToLower(arg), nil
		}
		return "", nil
	},
	"toupper": func(arguments ...any) (any, error) {
		if len(arguments) < 1 {
			return nil, errors.New("invalid arguments to toupper method")
		}
		if arg, ok := arguments[0].(*string); ok {
			return strings.ToUpper(*arg), nil
		}
		if arg, ok := arguments[0].(string); ok {
			return strings.ToUpper(arg), nil
		}
		return "", nil
	},
	"startswith": func(arguments ...any) (any, error) {
		if len(arguments) < 2 {
			return nil, errors.New("invalid arguments to startswith method")
		}
		var first *string
		var second *string
		if arg, ok := arguments[0].(*string); ok {
			first = arg
		}
		if arg, ok := arguments[0].(string); ok {
			first = &arg
		}
		if arg, ok := arguments[1].(*string); ok {
			second = arg
		}
		if arg, ok := arguments[1].(string); ok {
			second = &arg
		}
		if first != nil && second != nil {
			return strings.HasPrefix(*first, *second), nil
		}
		return false, nil
	},
	"endswith": func(arguments ...any) (any, error) {
		if len(arguments) < 2 {
			return nil, errors.New("invalid arguments to endswith method")
		}
		var first *string
		var second *string
		if arg, ok := arguments[0].(*string); ok {
			first = arg
		}
		if arg, ok := arguments[0].(string); ok {
			first = &arg
		}
		if arg, ok := arguments[1].(*string); ok {
			second = arg
		}
		if arg, ok := arguments[1].(string); ok {
			second = &arg
		}
		if first != nil && second != nil {
			return strings.HasSuffix(*first, *second), nil
		}
		return false, nil
	},
	"contains": func(arguments ...any) (any, error) {
		if len(arguments) < 2 {
			return nil, errors.New("invalid arguments to endswith method")
		}
		var first *string
		var second *string
		if arg, ok := arguments[0].(*string); ok {
			first = arg
		}
		if arg, ok := arguments[0].(string); ok {
			first = &arg
		}
		if arg, ok := arguments[1].(*string); ok {
			second = arg
		}
		if arg, ok := arguments[1].(string); ok {
			second = &arg
		}
		if first != nil && second != nil {
			return strings.Contains(*first, *second), nil
		}
		return false, nil
	},
	"replace": func(arguments ...any) (any, error) {
		if len(arguments) < 3 {
			return nil, errors.New("invalid arguments to endswith method")
		}
		var first *string
		var second *string
		var third *string
		if arg, ok := arguments[0].(*string); ok {
			first = arg
		}
		if arg, ok := arguments[0].(string); ok {
			first = &arg
		}
		if arg, ok := arguments[1].(*string); ok {
			second = arg
		}
		if arg, ok := arguments[1].(string); ok {
			second = &arg
		}
		if arg, ok := arguments[2].(*string); ok {
			third = arg
		}
		if arg, ok := arguments[2].(string); ok {
			third = &arg
		}

		if first != nil && second != nil && third != nil {
			return strings.ReplaceAll(*first, *second, *third), nil
		}
		return "", nil
	},
	"replace_all": func(arguments ...any) (any, error) {
		if len(arguments) < 3 {
			return nil, errors.New("invalid arguments to endswith method")
		}
		var first *string
		var second *string
		var third *string
		if arg, ok := arguments[0].(*string); ok {
			first = arg
		}
		if arg, ok := arguments[0].(string); ok {
			first = &arg
		}
		if arg, ok := arguments[1].(*string); ok {
			second = arg
		}
		if arg, ok := arguments[1].(string); ok {
			second = &arg
		}
		if arg, ok := arguments[2].(*string); ok {
			third = arg
		}
		if arg, ok := arguments[2].(string); ok {
			third = &arg
		}
		if first != nil && second != nil && third != nil {
			return strings.ReplaceAll(*first, *second, *third), nil
		}
		return "", nil
	},
	"guid": func(arguments ...any) (any, error) {
		id := uuid.New()
		return id.String(), nil
	},
	"uuid": func(arguments ...any) (any, error) {
		id := uuid.New()
		return id.String(), nil
	},
	"totime": func(arguments ...any) (any, error) {
		if len(arguments) < 1 {
			return nil, errors.New("invalid arguments to todatetime function")
		}
		if arguments[0] == nil {
			return nil, errors.New("invalid argument to totime function")
		}
		var first *string
		if arg, ok := arguments[0].(*string); ok {
			first = arg
		}
		if arg, ok := arguments[0].(string); ok {
			first = &arg
		}
		if first != nil {
			return dateparse.ParseAny(*first)
		}
		return nil, nil
	},
	"tomillis": func(arguments ...any) (any, error) {
		if len(arguments) < 1 {
			return nil, errors.New("invalid arguments to tomillis function")
		}
		if arguments[0] == nil {
			return nil, errors.New("invalid argument to tomillis function")
		}
		var first *time.Time
		if arg, ok := arguments[0].(*time.Time); ok {
			first = arg
		}
		if arg, ok := arguments[0].(time.Time); ok {
			first = &arg
		}
		if first != nil {
			return first.UnixMilli(), nil
		}
		return nil, nil
	},
}

Functions

func ApplyFilter

func ApplyFilter(frame *data.Frame, filterExpression string) (*data.Frame, error)

func FieldExists

func FieldExists(frame *data.Frame, field *data.Field) bool

FieldExists checks if a field exist in a frame only field type and field name for uniqueness

func FilterExpression

func FilterExpression(input []*data.Frame, options FilterExpressionOptions) ([]*data.Frame, error)

func GetFrameWithComputedColumns

func GetFrameWithComputedColumns(frame *data.Frame, columns []ComputedColumn) (*data.Frame, error)

func GetNormalizedValueForExpressionEvaluation added in v1.0.6

func GetNormalizedValueForExpressionEvaluation(field *data.Field, index int) (v any)

GetNormalizedValueForExpressionEvaluation normalizes the value of a field at a specific row index for use in expression evaluation. It handles nullable fields, time fields, and ensures the value is in a consistent format for evaluation.

func GetSummarizeByFrame

func GetSummarizeByFrame(frame *data.Frame, expression, by string, alias string) (*data.Frame, error)

func GetSummaryFrame

func GetSummaryFrame(frame *data.Frame, expression string, by string, alias string) (*data.Frame, error)

func Limit

func Limit(input []*data.Frame, options LimitOptions) ([]*data.Frame, error)

func Merge

func Merge(inputFrames []*data.Frame, option MergeFramesOptions) (*data.Frame, error)

Merge transformation used to merge multiple dataframe of same structure into single frame Requirement: Fields length should be same across the frame Requirement: Field names for all the input frames must be same Requirement: Fields must not have labels. Any labels for the fields will be ignored Requirement: Field must be in same order. (??) Ref: https://github.com/grafana/grafana/blob/v9.5.2/packages/grafana-data/src/transformations/transformers/merge.ts

Types

type ComputedColumn

type ComputedColumn struct {
	Selector string `json:"selector"`
	Text     string `json:"text"`
}

type FilterExpressionOptions

type FilterExpressionOptions struct {
	Expression string `json:"expression,omitempty"`
}

type LimitOptions

type LimitOptions struct {
	LimitField int `json:"limitField,omitempty"`
}

type MergeFramesOptions

type MergeFramesOptions struct {
}

Jump to

Keyboard shortcuts

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