Documentation
¶
Index ¶
- type DynamicSchmittTrigger
- type MovingAverage
- type SchmittTrigger
- type State
- type StateTracker
- type StepFunc
- type TimeValue
- type Timeseries
- func (t *Timeseries) Add(value float64)
- func (t *Timeseries) AddWithTime(value float64, when time.Time) error
- func (t *Timeseries) Avg(from time.Time, to time.Time) (value float64, ok bool)
- func (t *Timeseries) Get(time time.Time) (tv TimeValue, ok bool)
- func (t *Timeseries) Last() (tv TimeValue, ok bool)
- func (t *Timeseries) LinearRegression(from time.Time, to time.Time) (alpha float64, beta float64, rsquared float64)
- func (t *Timeseries) Pos(time time.Time) (i1 int, i2 int, ok bool)
- func (t *Timeseries) Reset()
- func (t *Timeseries) Size() int
- func (t *Timeseries) StdDev(from time.Time, to time.Time) (std float64, mean float64)
- func (t *Timeseries) ValuesRange(from time.Time, to time.Time) (timeValues []TimeValue, values []float64)
- type TimeseriesCounterRate
- func (t *TimeseriesCounterRate) Inc(value float64) error
- func (t *TimeseriesCounterRate) Rate(timeSpan time.Duration) (float64, bool)
- func (t *TimeseriesCounterRate) RateOverTime(rateLen time.Duration, timeseriesSpan time.Duration) (ts Timeseries, ok bool)
- func (t *TimeseriesCounterRate) RateRange(from time.Time, to time.Time) (float64, bool)
- func (t *TimeseriesCounterRate) Set(value float64) error
- type Worker
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DynamicSchmittTrigger ¶
type DynamicSchmittTrigger struct {
// contains filtered or unexported fields
}
DynamicSchmittTrigger adjusts its internal lower and upper limits according to a moving average on observed values Only initialize this with NewDynamicSchmittTriggerTimeWindow(..)
func NewDynamicSchmittTriggerTimeWindow ¶
func NewDynamicSchmittTriggerTimeWindow(minMaxMovingAverageTime time.Duration, maxMovingAverageSamples int, groupByMinMaxSamples int, ignoreSamplesTooDifferentRatio float64, minMaxUpperLowerRatio float64, upperRange bool) (DynamicSchmittTrigger, error)
NewDynamicSchmittTriggerTimeWindow new schmitt trigger creation minMaxMovingAverageSamples defines the time window of the moving average that defines min/max values for schmitt trigger maxMovingAverageSamples - max number of samples in minmax averager. if a too high rate of samples are set, some may be ignored groupByMinMaxSamples - number of signal samples to use to calculate each min/max sampling ignoreSamplesTooHighRatio - if SetCurrentValue sets a value that is too high or too low according to min/max moving average, ignore it minMaxUpperLowerRatio - 1.0 indicates the lower and upper limits will be placed just like the min/max moving average, which is not too practical. A number between 0.3 and 0.7 is good here.
func (*DynamicSchmittTrigger) GetLowerUpperLimits ¶
func (s *DynamicSchmittTrigger) GetLowerUpperLimits() (float64, float64)
GetLowerUpperLimits get current lower and upper limits for this schmtt trigger
func (*DynamicSchmittTrigger) IsUpperRange ¶
func (s *DynamicSchmittTrigger) IsUpperRange() bool
IsUpperRange returns if this trigger is in upper or low range
func (*DynamicSchmittTrigger) SetCurrentValue ¶
func (s *DynamicSchmittTrigger) SetCurrentValue(value float64) (bool, float64)
SetCurrentValue set current value and calculate if it is in upper or lower range returns 1-true or false if value was accepted by internal moving averager (rate not too high)
2-how much the current value is distant from the lower limit (if it is in 'upperRange' state) or distant from the upper limit (if in 'lowerRange' state) for a new change to occur in trigger. a ratio in relation to max-min range will be returned
type MovingAverage ¶
MovingAverage running moving averager Only initialize this with NewMovingAverage(..)
func NewMovingAverage ¶
func NewMovingAverage(size int) MovingAverage
NewMovingAverage creates a new moving averager with a fixed size
func NewMovingAverageTimeWindow ¶
func NewMovingAverageTimeWindow(samplesDuration time.Duration, maxSamples int) MovingAverage
NewMovingAverageTimeWindow creates a new moving averager that will average samples no older than 'samplesDuration', limiting the number of samples to 'maxSamples' in time window. If two consecutive samples are added to the averager in a period less than duration/maxSamples, it will be ignored.
func (*MovingAverage) AddSample ¶
func (m *MovingAverage) AddSample(value float64) bool
AddSample adds a new sample to the moving average. If there is more than 'size' samples, the oldest sample will be removed. If this is a timed window averager and the last sample was added in less than sampleDurate/maxSamples time, it will be ignored.
func (*MovingAverage) AddSampleIfNearAverage ¶
func (m *MovingAverage) AddSampleIfNearAverage(value float64, avgDiff float64) bool
AddSampleIfNearAverage Add sample only if its value is near current average to avoid espurious samples to be added to the average. *avgDiff* 1 means samples between [-currentAvg, +currentAvg] will be accepted. Returns true if sample was accepted
func (*MovingAverage) Average ¶
func (m *MovingAverage) Average() float64
Average computes average with current samples in fixed length list
func (*MovingAverage) AverageMinMax ¶
func (m *MovingAverage) AverageMinMax(groupBySamples int) (float64, float64)
AverageMinMax - returns the min/max values in current window Group min/max each 'groupBySamples' and perform average over theses samples for min and max values
type SchmittTrigger ¶
type SchmittTrigger struct {
LowerLimit float64
UpperLimit float64
UpperRange bool
// contains filtered or unexported fields
}
SchmittTrigger utility Only initialize this with NewSchmittTrigger(..)
func NewSchmittTrigger ¶
func NewSchmittTrigger(lowerLimit float64, upperLimit float64, upperRange bool) (SchmittTrigger, error)
NewSchmittTrigger new schmitt trigger creation
func (*SchmittTrigger) IsUpperRange ¶
func (s *SchmittTrigger) IsUpperRange() bool
IsUpperRange returns whatever it is in upper range or not
func (*SchmittTrigger) SetCurrentValue ¶
func (s *SchmittTrigger) SetCurrentValue(value float64)
SetCurrentValue set current value and calculate if it is in upper or lower range
func (*SchmittTrigger) UpdateLowerUpperLimits ¶
func (s *SchmittTrigger) UpdateLowerUpperLimits(lowerLimit float64, upperLimit float64)
UpdateLowerUpperLimits changes current lower/upper limits for schmitt trigger
type State ¶ added in v1.3.0
type State struct {
Name string
Start time.Time
Stop *time.Time
Data interface{}
Level *float64
HighestLevel *float64
HighestTime *time.Time
HighestData interface{}
}
State event struct
type StateTracker ¶
type StateTracker struct {
CurrentState *State
CandidateState string
CandidateCount int
// contains filtered or unexported fields
}
StateTracker state transition tracker
func NewStateTracker ¶
func NewStateTracker(initialState string, changeConfirmations int, onChange func(*State, *State), unchangedTimer time.Duration, onUnchanged func(*State), resetHighestOnunchanged bool) *StateTracker
NewStateTracker new state transition tracker instantiation initialState - states are simply strings. a different string denotes a new state changeConfirmations - number of sequential state samples with a different state before transitioning onChange - listener function that will be called on state transition. ex.: func(newState, previousState) {}. nil value disables this unchangedTimer - after this time without changing state, 'onUnchanged' func will be invoked recurrently//. current highest sample will be calculated based on this time slice onUnchanged - listener function to be invoked if state is not changed after unchangedStateCount. onUnchanged(state). nil value disables this feature resetHighestOnunchanged - calculate highest level according to whole state duration (false) or only during the onChanged recurrent timer
func (*StateTracker) Close ¶
func (s *StateTracker) Close()
Close closes the internal timers for notifying unchanged
func (*StateTracker) SetTransientState ¶
func (s *StateTracker) SetTransientState(stateName string) (*State, error)
SetTransientState sets a transient state to tracker so that it can find possible transitions if this state gets recurrent returns the time this state started
func (*StateTracker) SetTransientStateWithData ¶
func (s *StateTracker) SetTransientStateWithData(stateName string, level float64, data interface{}) (*State, error)
SetTransientStateWithData sets a transient state to tracker so that it can find possible transitions if this state gets recurrent data is any type that will be sent to listener function returns current state count
type StepFunc ¶ added in v1.5.0
type StepFunc func() error
StepFunc function interface for the application that will be called in a loop
type Timeseries ¶ added in v1.3.0
type Timeseries struct {
TimeseriesSpan time.Duration
Values []TimeValue
// contains filtered or unexported fields
}
Timeseries utility Only initialize this with NewTimeseries(..)
func NewTimeseries ¶ added in v1.3.0
func NewTimeseries(maxTimeseriesSpan time.Duration) Timeseries
NewTimeseries create a new timeseries with a limited size in time. After that limit older values will be deleted from time to time to avoid too much memory usage
func (*Timeseries) Add ¶ added in v1.7.0
func (t *Timeseries) Add(value float64)
Add add a new sample to this timeseries using time.Now()
func (*Timeseries) AddWithTime ¶ added in v1.9.0
func (t *Timeseries) AddWithTime(value float64, when time.Time) error
AddWithTime adds ad new sample to the head of this timeseries 'when' must be after the last element (no middle insertions allowed)
func (*Timeseries) Avg ¶ added in v1.7.0
Avg calculates the average value of points compreended between time 'from' and 'to' No interpolation is used here
func (*Timeseries) Get ¶ added in v1.7.0
func (t *Timeseries) Get(time time.Time) (tv TimeValue, ok bool)
Get get value in a specific time in timeseries. If time is between two points inside timeseries, the value will be interpolated according to the requested time and neighboring values If time is before or after timeseries points, ok is false
func (*Timeseries) Last ¶ added in v1.7.0
func (t *Timeseries) Last() (tv TimeValue, ok bool)
Last get last point in time element, the head element
func (*Timeseries) LinearRegression ¶ added in v1.8.0
func (t *Timeseries) LinearRegression(from time.Time, to time.Time) (alpha float64, beta float64, rsquared float64)
LinearRegression calculates the linear regression coeficients for the time range x is in range of time.UnixNano() returns alpha and beta as for y = alpha + beta*x and rsquared with fit from 0-1
func (*Timeseries) Pos ¶ added in v1.7.0
Pos searches for which two point indexes are between the desired time Find the time is exacly the same as a point time, the two returned indexes will be equal
func (*Timeseries) Reset ¶ added in v1.3.0
func (t *Timeseries) Reset()
Reset remove all elements from this timeseries
func (*Timeseries) Size ¶ added in v1.3.0
func (t *Timeseries) Size() int
Size current number of elements in this timeseries
func (*Timeseries) StdDev ¶ added in v1.8.0
StdDev calculates the standard deviation and mean for the time range returns standard deviation and mean value
func (*Timeseries) ValuesRange ¶ added in v1.8.0
func (t *Timeseries) ValuesRange(from time.Time, to time.Time) (timeValues []TimeValue, values []float64)
ValuesRange get values in time range returns an array of TimeValue and and array with just the float values
type TimeseriesCounterRate ¶ added in v1.3.0
type TimeseriesCounterRate struct {
Timeseries Timeseries
// contains filtered or unexported fields
}
TimeseriesCounterRate this is a utility for storing counter values in time while enabling the measurement of rates in various time spans with without having to perform average over all points. The optimization strategy here is based on the fact that this timeseries contains a counter, so that averages between two times are calculated by just averaging the first and last points, not all the points between. Very useful for metrics monitoring. See more at https://prometheus.io/docs/concepts/metric_types/#counter Only initialize this with NewTimeseries(..)
func NewTimeseriesCounterRate ¶ added in v1.3.0
func NewTimeseriesCounterRate(timeseriesSpan time.Duration) TimeseriesCounterRate
NewTimeseriesCounterRate creates a time timeseries with max time span of timeseriesSpan
func (*TimeseriesCounterRate) Inc ¶ added in v1.3.0
func (t *TimeseriesCounterRate) Inc(value float64) error
Inc increments the last value from the timeseries by 'value' and sets add the new point with time.Now() time
func (*TimeseriesCounterRate) Rate ¶ added in v1.3.0
func (t *TimeseriesCounterRate) Rate(timeSpan time.Duration) (float64, bool)
Rate calculates the rate of change between the last point in time of this timeseries and the time in past, specified by timeSpan
func (*TimeseriesCounterRate) RateOverTime ¶ added in v1.9.0
func (t *TimeseriesCounterRate) RateOverTime(rateLen time.Duration, timeseriesSpan time.Duration) (ts Timeseries, ok bool)
RateOverTime calculates a new Timeseries containing rate over time which each value is a rate over 'rateLen' The timeseries total length will be of 'timeseriesSpan' For each point in the counter timeseries, there will be calculated the counter rate and put to the resulting new Timeseries
func (*TimeseriesCounterRate) RateRange ¶ added in v1.9.0
RateRange calculate the rate of change in the date range
func (*TimeseriesCounterRate) Set ¶ added in v1.4.0
func (t *TimeseriesCounterRate) Set(value float64) error
Set sets the absolute value at time time.Now(). The value cannot be less then last value from the timeseries as this must be a counter
type Worker ¶ added in v1.5.0
type Worker struct {
CurrentFreq float64
CurrentStepTime time.Duration
// contains filtered or unexported fields
}
Worker utility for launching Go routines that will loop over a function the max frequency of calls to this function is limited and the actual frequency is measured
func StartWorker ¶ added in v1.5.0
func StartWorker(ctx context.Context, name string, step StepFunc, minFreq float64, maxFreq float64, stopOnErr bool) *Worker
StartWorker launches a Go routine looping in this step function limiting by maxFreq if the function is being run in a frequency less than minFreq, a logrus.Debug log will show this this situation happens when the function is too slow