calc

package
v0.0.0-...-2ad74da Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2024 License: BSD-3-Clause Imports: 7 Imported by: 2

Documentation

Overview

Lexer and parser for expressions of the form:

f(g(h("foo"), i(3, "bar")))

Note that while it does understand strings and numbers, it doesn't do binary operators. We can do those via functions if needed, ala add(x, y), sub(x, y), etc.

Caveats: * Only handles ASCII.

For context on how this was written please watch:

https://www.youtube.com/watch?v=HxaD_trXwRE

Index

Constants

View Source
const (
	// MIN_STDDEV is the smallest standard deviation we will normalize, smaller
	// than this and we presume it's a standard deviation of zero.
	MIN_STDDEV = 0.001
)

Variables

This section is empty.

Functions

func AveFuncImpl

func AveFuncImpl(rows types.TraceSet) types.Trace

AveFuncImpl averages the values of all argument traces into a single trace.

func CountFuncImpl

func CountFuncImpl(rows types.TraceSet) types.Trace

func GeoFuncImpl

func GeoFuncImpl(rows types.TraceSet) types.Trace

GeoFuncImpl take the geometric mean of the values of all argument rows into a single trace.

func MaxFuncImpl

func MaxFuncImpl(rows types.TraceSet) types.Trace

MaxFuncImpl puts the max of the values of all argument traces into a single trace.

func MinFuncImpl

func MinFuncImpl(rows types.TraceSet) types.Trace

MinFuncImpl puts the min of the values of all argument traces into a single trace.

func StdDevFuncImpl

func StdDevFuncImpl(rows types.TraceSet) types.Trace

StdDevFuncImpl puts the std deviation of the values of all argument traces into a single trace.

func SumFuncImpl

func SumFuncImpl(rows types.TraceSet) types.Trace

SumFuncImpl sums the values of all argument rows into a single trace.

Types

type AveFunc

type AveFunc struct{}

func (AveFunc) Describe

func (AveFunc) Describe() string

func (AveFunc) Eval

func (AveFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

aveFunc implements Func and averages the values of all argument traces into a single trace.

vec32.MISSING_DATA_SENTINEL values are not included in the average. Note that if all the values at an index are vec32.MISSING_DATA_SENTINEL then the average will be vec32.MISSING_DATA_SENTINEL.

type Context

type Context struct {
	RowsFromQuery    RowsFromQuery
	RowsFromShortcut RowsFromShortcut
	Funcs            map[string]Func
	// contains filtered or unexported fields
}

Context stores all the info for a single parser.

A Context is not safe to call from multiple go routines.

func NewContext

func NewContext(rowsFromQuery RowsFromQuery, rowsFromShortcut RowsFromShortcut) *Context

NewContext create a new parsing context that includes the basic functions.

func (*Context) Eval

func (ctx *Context) Eval(exp string) (types.TraceSet, error)

Eval parses and evaluates the given string expression and returns the Traces, or an error.

type CountFunc

type CountFunc struct{}

CountFunc implements Func and counts the number of non-sentinel values in all argument rows.

vec32.MISSING_DATA_SENTINEL values are not included in the count. Note that if all the values at an index are vec32.MISSING_DATA_SENTINEL then the count will be 0.

func (CountFunc) Describe

func (CountFunc) Describe() string

func (CountFunc) Eval

func (CountFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

type FillFunc

type FillFunc struct{}

func (FillFunc) Describe

func (FillFunc) Describe() string

func (FillFunc) Eval

func (FillFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

fillFunc implements Func and fills in all the missing datapoints with nearby points.

Note that a Row with all vec32.MISSING_DATA_SENTINEL values will be filled with 0's.

type FilterFunc

type FilterFunc struct{}

func (FilterFunc) Describe

func (FilterFunc) Describe() string

func (FilterFunc) Eval

func (FilterFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

filterFunc is a Func that returns a filtered set of Rows in the Context.

It expects a single argument that is a string in URL query format, ala os=Ubuntu12&config=8888.

type Func

type Func interface {
	Eval(*Context, *Node) (types.TraceSet, error)
	Describe() string
}

Func defines a type for functions that can be used in the parser.

The traces returned will always have a Param of "id" that identifies the trace. See DESIGN.md for the Trace ID naming conventions.

type GeoFunc

type GeoFunc struct{}

func (GeoFunc) Describe

func (GeoFunc) Describe() string

func (GeoFunc) Eval

func (GeoFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

geoFunc implements Func and merges the values of all argument rows into a single trace with a geometric mean.

vec32.MISSING_DATA_SENTINEL and negative values are not included in the mean. Note that if all the values at an index are vec32.MISSING_DATA_SENTINEL or negative then the mean will be vec32.MISSING_DATA_SENTINEL.

type IQRRFunc

type IQRRFunc struct{}

IQRRFunc implements Func and computes a new trace that is has all outliers set to MISSING_DATA_SENTINEL based on the interquartile range.

vec32.MISSING_DATA_SENTINEL values are not taken into account when computing the outliers.

func (IQRRFunc) Describe

func (IQRRFunc) Describe() string

func (IQRRFunc) Eval

func (IQRRFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

type LogFunc

type LogFunc struct{}

func (LogFunc) Describe

func (LogFunc) Describe() string

func (LogFunc) Eval

func (LogFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

logFunc implements Func and transforms a row of x into a row of log10(x).

Values <= 0 are set to vec32.MISSING_DATA_SENTINEL. vec32.MISSING_DATA_SENTINEL values are left untouched.

type Node

type Node struct {
	Typ  NodeType
	Val  string
	Args []*Node
}

Node is a single node in the parse tree.

func (*Node) Eval

func (n *Node) Eval(ctx *Context) (types.TraceSet, error)

Evaluates a node. Only valid to call on Nodes of type NodeFunc.

type NodeType

type NodeType int
const (
	NodeError NodeType = iota
	NodeFunc
	NodeNum
	NodeString
)

type NormFunc

type NormFunc struct{}

func (NormFunc) Describe

func (NormFunc) Describe() string

func (NormFunc) Eval

func (NormFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

normFunc implements Func and normalizes the traces to a mean of 0 and a standard deviation of 1.0. If a second optional number is passed in to norm() then that is used as the minimum standard deviation that is normalized, otherwise it defaults to MIN_STDDEV.

type RatioFunc

type RatioFunc struct{}

func (RatioFunc) Describe

func (RatioFunc) Describe() string

func (RatioFunc) Eval

func (RatioFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

type RowsFromQuery

type RowsFromQuery func(q string) (types.TraceSet, error)

type RowsFromShortcut

type RowsFromShortcut func(id string) (types.TraceSet, error)

type ScaleByAveFunc

type ScaleByAveFunc struct{}

func (ScaleByAveFunc) Describe

func (ScaleByAveFunc) Describe() string

func (ScaleByAveFunc) Eval

func (ScaleByAveFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

ScaleByAveFunc implements Func and Computes a new trace that is scaled by 1/(average of all values in the trace).

vec32.MISSING_DATA_SENTINEL values are not taken into account for the ave. If the entire vector is vec32.MISSING_DATA_SENTINEL then the result is also all vec32.MISSING_DATA_SENTINEL.

type ShortcutFunc

type ShortcutFunc struct{}

func (ShortcutFunc) Describe

func (ShortcutFunc) Describe() string

func (ShortcutFunc) Eval

func (ShortcutFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

shortcutFunc is a Func that returns a set of Rows in the Context.

It expects a single argument that is a shortcut id.

type SumFunc

type SumFunc struct{}

func (SumFunc) Describe

func (SumFunc) Describe() string

func (SumFunc) Eval

func (SumFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

SumFunc implements Func and sums the values of all argument rows into a single trace.

vec32.MISSING_DATA_SENTINEL values are not included in the sum. Note that if all the values at an index are vec32.MISSING_DATA_SENTINEL then the sum will be vec32.MISSING_DATA_SENTINEL.

type TraceAveFunc

type TraceAveFunc struct{}

func (TraceAveFunc) Describe

func (TraceAveFunc) Describe() string

func (TraceAveFunc) Eval

func (TraceAveFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

traceAveFunc implements Func and Computes the mean for all the values in a trace and return a trace where every value is that mean.

vec32.MISSING_DATA_SENTINEL values are not taken into account for the ave. If the entire vector is vec32.MISSING_DATA_SENTINEL then the result is also all vec32.MISSING_DATA_SENTINEL.

type TraceCovFunc

type TraceCovFunc struct{}

func (TraceCovFunc) Describe

func (TraceCovFunc) Describe() string

func (TraceCovFunc) Eval

func (TraceCovFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

traceCovFunc implements Func and Computes the Coefficient of Variation (std dev)/mean for all the values in a trace and return a trace where every value is the CoV.

vec32.MISSING_DATA_SENTINEL values are not taken into account for the ave. If the entire vector is vec32.MISSING_DATA_SENTINEL then the result is also all vec32.MISSING_DATA_SENTINEL.

type TraceStdDevFunc

type TraceStdDevFunc struct{}

func (TraceStdDevFunc) Describe

func (TraceStdDevFunc) Describe() string

func (TraceStdDevFunc) Eval

func (TraceStdDevFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

traceStdDevFunc implements Func and Computes the std dev for all the values in a trace and return a trace where every value is that std dev.

vec32.MISSING_DATA_SENTINEL values are not taken into account for the ave. If the entire vector is vec32.MISSING_DATA_SENTINEL then the result is also all vec32.MISSING_DATA_SENTINEL.

type TraceStepFunc

type TraceStepFunc struct{}

func (TraceStepFunc) Describe

func (TraceStepFunc) Describe() string

func (TraceStepFunc) Eval

func (TraceStepFunc) Eval(ctx *Context, node *Node) (types.TraceSet, error)

TraceStepFunc implements Func and Computes the step function, i.e the ratio of the ave of the first half of the trace divided by the ave of the second half of the trace.

vec32.MISSING_DATA_SENTINEL values are not taken into account for the ave. If the entire vector is vec32.MISSING_DATA_SENTINEL then the result is also all vec32.MISSING_DATA_SENTINEL.

Jump to

Keyboard shortcuts

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