github.com/tobgu/qframe@v0.4.0/config/rolling/config.go (about)

     1  package rolling
     2  
     3  import "github.com/tobgu/qframe/qerrors"
     4  
     5  // DataValue can be any of int/float/*string/bool, eg. any type that a column may take.
     6  type DataValue = interface{}
     7  
     8  // IntervalFunc is a function taking two parameters of the same DataValue and returning boolean stating if
     9  // the two values are part of the same interval or not.
    10  //
    11  // For example, x and y within one unit from each other (with x assumed to be <= y):
    12  type IntervalFunc = interface{}
    13  
    14  // It should be considered a private implementation detail and should never be
    15  // referenced or used directly outside of the QFrame code. To manipulate it
    16  // use the functions returning ConfigFunc below.
    17  type Config struct {
    18  	PadValue        DataValue
    19  	IntervalColName string
    20  	IntervalFunc    IntervalFunc
    21  	WindowSize      int
    22  	Position        string // center/start/end
    23  }
    24  
    25  // ConfigFunc is a function that operates on a Config object.
    26  type ConfigFunc func(c *Config)
    27  
    28  func NewConfig(ff []ConfigFunc) (Config, error) {
    29  	c := Config{
    30  		WindowSize: 1,
    31  		Position:   "center",
    32  	}
    33  
    34  	for _, fn := range ff {
    35  		fn(&c)
    36  	}
    37  
    38  	if c.WindowSize <= 0 {
    39  		return c, qerrors.New("Rolling config", "Window size must be positive, was %d", c.WindowSize)
    40  	}
    41  
    42  	if c.Position != "center" && c.Position != "start" && c.Position != "end" {
    43  		return c, qerrors.New("Rolling config", "Position must be center/start/end, was %s", c.Position)
    44  	}
    45  
    46  	if c.IntervalFunc != nil && c.WindowSize != 1 {
    47  		return c, qerrors.New("Rolling config", "Cannot set both interval function and window size")
    48  	}
    49  
    50  	return c, nil
    51  }
    52  
    53  // PadValue can be used to set the value to use in the beginning and/or end of the column to fill out any values
    54  // where fewer than WindowSize values are available.
    55  func PadValue(v DataValue) ConfigFunc {
    56  	return func(c *Config) {
    57  		c.PadValue = v
    58  	}
    59  }
    60  
    61  // IntervalFunction can be used to set a dynamic interval based on the content of another column.
    62  // QFrame will include all rows from the start row of the window until (but not including) the first row that is
    63  // not part of the interval according to 'fn'. The first parameter passed to 'fn' is always the value at the start
    64  // of the window.
    65  //
    66  // For example, lets say that you have a time series with millisecond resolution integer timestamps in column 'ts'
    67  // and values in column 'value' that you would like to compute a rolling average over a minute for.
    68  //
    69  // In this case:
    70  // col = "ts", fn = func(tsStart, tsEnd int) bool { return tsEnd < tsStart + int(time.Minute / time.Millisecond)}
    71  func IntervalFunction(colName string, fn IntervalFunc) ConfigFunc {
    72  	return func(c *Config) {
    73  		c.IntervalColName = colName
    74  		c.IntervalFunc = fn
    75  	}
    76  }
    77  
    78  // WindowSize is used to set the size of the Window. By default this is 1.
    79  func WindowSize(s int) ConfigFunc {
    80  	return func(c *Config) {
    81  		c.WindowSize = s
    82  	}
    83  }
    84  
    85  // Position is used to set where in window the resulting value should be inserted.
    86  // Valid values: start/center/end
    87  // Default value: center
    88  func Position(p string) ConfigFunc {
    89  	return func(c *Config) {
    90  		c.Position = p
    91  	}
    92  }