usts

package module
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2021 License: MIT Imports: 7 Imported by: 0

README

USTS ( Unevently Spaced Time Series )

Unevenly spaced time series library for Go and other time/slots/window helpers to deal with Unevenly spaced time events This library ia a partial Golang port for the python traces library , with a few time event handling additions.)

Go Report Card Release Version GoDoc License

About Unevently Spaced Time Series

You could find more info about this kind of time series in the following links

Example


package main

import (
	"fmt"
    "time"
    "github.com/datadope-io/usts"
)


func main() {

	ts := usts.NewUSTimeSerie(0)

	ts.Add(time.Date(2042, 2, 1, 6, 0, 0, 0, time.UTC), "NOK")
	ts.Add(time.Date(2042, 2, 1, 7, 45, 56, 0, time.UTC), "OK")
	ts.Add(time.Date(2042, 2, 1, 8, 51, 42, 0, time.UTC), "NOK")
	ts.Add(time.Date(2042, 2, 1, 12, 3, 56, 0, time.UTC), "OK")
	ts.Add(time.Date(2042, 2, 1, 12, 7, 13, 0, time.UTC), "NOK")

	t0 := time.Date(2042, 2, 1, 6, 0, 0, 0, time.UTC)
	t1 := time.Date(2042, 2, 1, 13, 0, 0, 0, time.UTC)

	m, total, err := ts.Distribution(t0, t1, nil)
	if err != nil {
		fmt.Errorf("Error: %s", err)
		return
    }
    
	totalsec := int64(total / time.Second)

	for k, v := range m {
		percent := float64(v/time.Second) * 100.0 / float64(totalsec)
		fmt.Printf("VALUE %v present for %s :  %.2f %%\n", k, v, percent)
	}

	// Unordered output:
	// VALUE NOK present for 5h50m57s :  83.56 %
	// VALUE OK present for 1h9m3s :  16.44 %
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetPreviousCronTime

func GetPreviousCronTime(sch cron.Schedule, start time.Time) time.Time

GetPreviousCronTime get Previous scheduled time from cron Scheduler this method will check if exist any previous sched value beggining in the past with different intervals 1ms,1s,1m,1h,6h,24h,7d,30d,365d before

func SetLogger

func SetLogger(l Logger)

SetLogger add custom logger enables custom format, output and level, if not set all logs will be writen ove Stdout with without level filtering (all logs will be shown)

Types

type AddSlotMode

type AddSlotMode int
const (
	Add    AddSlotMode = 0
	Remove AddSlotMode = 1
	And    AddSlotMode = 2
)

type IterateElementFunc added in v0.1.1

type IterateElementFunc func(time.Time, interface{}, int) bool

IterateElementFunc helps users to iterate over the time serie entries return true if would like continue the iteration process

type IteratePeriodFunc

type IteratePeriodFunc func(t0, t1 time.Time, value interface{}) bool

IteratePeriodFunc function helper to iterate over periods instead of iteration over elements the initial time t0 and end time t1 of the period will be sent to the funcion , also the value at this interval

type Logger

type Logger interface {
	Infof(format string, v ...interface{})
	Errorf(format string, v ...interface{})
	Warnf(format string, v ...interface{})
	Debugf(format string, v ...interface{})
	Tracef(format string, v ...interface{})
}

Logger interface is to abstract the logging from Usts. Gives control to the USTS users, choice of the logger.

type TimeSlot

type TimeSlot struct {
	ID string

	//-------------------------------------
	Scf string //Start Cron Format
	Ecf string //End Cron Format
	// contains filtered or unexported fields
}

TimeSlot this object give us ability to create periodic time intervals from some logic expresion

func NewTimeSlot

func NewTimeSlot(id, startexpr, endexpr string) (*TimeSlot, error)

NewTimeSlot create a periodic time slot from cron based expression for start and end time slot

func (*TimeSlot) GetClosestPreviousEvent

func (ts *TimeSlot) GetClosestPreviousEvent(start time.Time) (time.Time, bool)

GetClosestPreviousEvent helps to determine the default/first value

func (*TimeSlot) GetTimeEvents

func (ts *TimeSlot) GetTimeEvents(start, end time.Time, tz string) (*USTimeSerie, error)

GetTimeEvents get all initial(as true)/final(as false) slot events

func (*TimeSlot) RefreshCronTZ

func (ts *TimeSlot) RefreshCronTZ(tz string) error

RefreshCronTZ reload cron based expressions adding timezone info

type TimeWindow

type TimeWindow struct {
	ID       string
	Slots    []*TimeSlot
	SlotMode []AddSlotMode
	Calendar *cal.Calendar
	// contains filtered or unexported fields
}

func NewTimeWindow

func NewTimeWindow(id string) *TimeWindow

func (*TimeWindow) AddSlot

func (tw *TimeWindow) AddSlot(sl *TimeSlot, mode AddSlotMode)

func (*TimeWindow) GetTimeEvents

func (tw *TimeWindow) GetTimeEvents(start, end time.Time) (*USTimeSerie, error)

func (*TimeWindow) SetCalendar

func (tw *TimeWindow) SetCalendar(c *cal.Calendar)

func (*TimeWindow) SetTimeZone

func (tw *TimeWindow) SetTimeZone(tz string) (*time.Location, error)

type USTimeSerie

type USTimeSerie struct {
	// contains filtered or unexported fields
}

USTimeSerie the Unevently Spaced Time Serie

func CalendarWindowEvents

func CalendarWindowEvents(c *cal.Calendar, tz string, start, end time.Time) (*USTimeSerie, error)

func NewUSTimeSerie

func NewUSTimeSerie(size int) *USTimeSerie

NewUSTimeSerie creates a new USTimeSerie with and alocates memory for size elements

func (*USTimeSerie) Add

func (uts *USTimeSerie) Add(t time.Time, v interface{}) (int, bool)

Add insert a new element with time t and value v or updates it if existing yet in the same time t

func (*USTimeSerie) And

func (uts *USTimeSerie) And(other *USTimeSerie) (*USTimeSerie, error)

And Logical AND from this TimeSerie with the other TimeSerie the resulting TimeSerie will have the same time points in both previous series (max will be N+M if any repeated time) computing logical AND : this is `output[t] = x[t1] AND y[t1]` if no value in y[t1] it gets the previous value

func (*USTimeSerie) BatchDelete

func (uts *USTimeSerie) BatchDelete(start time.Time, end time.Time) (int, error)

BatchDelete removes items inside time series from start to end time both included if they exist

func (*USTimeSerie) Clone added in v0.1.2

func (uts *USTimeSerie) Clone() *USTimeSerie

Clone Return a new allocated TimeSeries with the exact data from the origin

func (*USTimeSerie) Combine

func (uts *USTimeSerie) Combine(other *USTimeSerie) (*USTimeSerie, error)

Combine merges this TimeSerie with other , The resulting TimeSerie will have the same time points in both previous series (max will be N+M if any repeated time) no logical operations done, it only merges data except on time concidence where logical OR will be placed. `output[t1] = x[t1] (if not exist y[t1])` `output[t2] = y[t2] (if not exist x[t2])` `output[t3] = x[t3] OR y[t3] ( if both series has values in t3)`

func (*USTimeSerie) Compact

func (uts *USTimeSerie) Compact() (bool, int)

Compact removes all repeated values maintaining only those who changes, it returs true and the number of reduced elements if could be compacted false and 0 if not

func (*USTimeSerie) CompactBoundaries added in v0.1.8

func (uts *USTimeSerie) CompactBoundaries(reversed bool) (bool, int)

CompactExtend removes all repeated values maintaining only those who changes, it returs true and the number of reduced elements if could be compacted false and 0 if not. The time boundaries on repeated values are chosen based on time values - true:min, false: max. This function only works with USTS with boolean values

func (*USTimeSerie) Delete

func (uts *USTimeSerie) Delete(key time.Time) bool

Delete Remove value in time key and return true if value found

func (*USTimeSerie) Distribution

func (uts *USTimeSerie) Distribution(start time.Time, end time.Time, mask *USTimeSerie) (map[interface{}]time.Duration, time.Duration, error)

Distribution computes distribution in time of values from start to end and only on these periods where the USTimeSerie mask will be true. False periods won't compute on time distribution mask = nil will be equivalent to a true always USTimeSerie mask ir returns a interfaced keyed map of durations and the complete computed time ( will be end - start if no mask)

func (*USTimeSerie) Dump

func (uts *USTimeSerie) Dump()

Dump Prints detailed info on the time series in stdout

func (*USTimeSerie) DumpBufferWithPrefix added in v0.1.5

func (uts *USTimeSerie) DumpBufferWithPrefix(prefix string) bytes.Buffer

DumpBufferWithPrefix Prints detailed info on the time series in stdout

func (*USTimeSerie) DumpInTimezone

func (uts *USTimeSerie) DumpInTimezone(tz string)

DumpInTimezone Prints detailed info on the time serie on certain Timezone

func (*USTimeSerie) Exist

func (uts *USTimeSerie) Exist(t time.Time) bool

Exist return true if one element exist at the exact time ( Synonym of Has )

func (*USTimeSerie) First

func (uts *USTimeSerie) First() (time.Time, interface{}, bool)

First return the first time.Time , value, and true/false if found/not found

func (*USTimeSerie) Get

func (uts *USTimeSerie) Get(t time.Time) (interface{}, bool)

Get return the value in the exact time or any previous existing and true if value in the exact time

func (*USTimeSerie) GetExact

func (uts *USTimeSerie) GetExact(t time.Time) (interface{}, bool)

GetExact returns the value in the time t and true if found a value in this exact time, nil/false if not found

func (*USTimeSerie) GetPrevious

func (uts *USTimeSerie) GetPrevious(t time.Time) (interface{}, bool)

GetPrevious returns the value in the time t or closest previous value in tim and true if found the default value and false will return if no value exist previous time t

func (*USTimeSerie) Has

func (uts *USTimeSerie) Has(key time.Time) bool

Has return true if one element exist at the exact time

func (*USTimeSerie) Insert

func (uts *USTimeSerie) Insert(t time.Time, value interface{}) (int, bool)

Insert inserts a new element on the time serie or updates and existing one in the exact time it returns the index at which has been inserted and true if the value has been updated, false if new

func (*USTimeSerie) Iterate

func (uts *USTimeSerie) Iterate(reversed bool, start, end time.Time, f IterateElementFunc) error

Iterate Initialize an Iterator process over timeseries in time reversed order if reversed = true

func (*USTimeSerie) IterateOnPeriods

func (uts *USTimeSerie) IterateOnPeriods(start, end time.Time, filter interface{}, f IteratePeriodFunc) error

IterateOnPeriods helps iterate over periods from start to end instead of iteration over elements. it will execute function f only on periods with value == filter and will stop iteration if iteration function returns false

func (*USTimeSerie) Keys

func (uts *USTimeSerie) Keys() []time.Time

Keys return and ordered array of times as TimeSerie Keys

func (*USTimeSerie) Last

func (uts *USTimeSerie) Last() (time.Time, interface{}, bool)

Last return the last time.Time , value, and true/false if found/not found

func (*USTimeSerie) Len

func (uts *USTimeSerie) Len() int

Len Return the number of elements in the Serie

func (*USTimeSerie) MarkPeriods added in v0.1.6

func (uts *USTimeSerie) MarkPeriods(other *USTimeSerie) (*USTimeSerie, error)

Mark periods on this TimeSerie with the other TimeSerie the resulting TimeSerie will have the same time points of this TimeSeries (max will be N+M if any repeated time) with all available periods of this TimeSerie and the other TimeSerie (max will be N+M periods) marking periods: this is `output[t] = x[t1..tM]` with M all available time periods combinations, if x[tx] has no value, it will retrieve the previous

func (*USTimeSerie) Not

func (uts *USTimeSerie) Not() (*USTimeSerie, error)

Not Logical negation from this TimeSerie. The resulting TimeSerie will have the same time points computing logical negation : this is `output[t] = NOT x[t1]`

func (*USTimeSerie) NumItems

func (uts *USTimeSerie) NumItems() int

NumItems return length of the time serie

func (*USTimeSerie) Or

func (uts *USTimeSerie) Or(other *USTimeSerie) (*USTimeSerie, error)

Or Logical OR from this TimeSerie with the other TimeSerie the resulting TimeSerie will have the same time points in both previous series (max will be N+M if any repeated time) computing logical OR : this is `output[t] = x[t1] OR y[t1]` if no value in y[t1] it gets the previous value

func (*USTimeSerie) Remove

func (uts *USTimeSerie) Remove(t time.Time) bool

Remove one element in the time serie ( Synonym of Delete)

func (*USTimeSerie) RemoveFromInterval

func (uts *USTimeSerie) RemoveFromInterval(start time.Time, end time.Time) (int, error)

RemoveFromInterval removes all items in interval from start to end ( Synonym of BatchDelete )

func (*USTimeSerie) SetDefault

func (uts *USTimeSerie) SetDefault(def interface{})

SetDefault set a value to use when no more info in the timeseries used basically as value tu return when get value from time before the first entry or when no entries yet in the timeseries

func (*USTimeSerie) SetInitialVal

func (uts *USTimeSerie) SetInitialVal(val interface{})

SetInitialVal the same behaviour than SetDefault

func (*USTimeSerie) SetIntervalValue

func (uts *USTimeSerie) SetIntervalValue(start, end time.Time, value interface{}) error

SetIntervalValue force a value in a interval at the end of the interval the value is forced to be the default value. In this operation all points in this interval will be removed

func (*USTimeSerie) Xor

func (uts *USTimeSerie) Xor(other *USTimeSerie) (*USTimeSerie, error)

Xor Logical XOR from this TimeSerie with the other TimeSerie the resulting TimeSerie will have the same time points in both previous series (max will be N+M if any repeated time) computing logical XOR : this is `output[t] = x[t1] XOR y[t1]` if no value in y[t1] it gets the previous value

Jump to

Keyboard shortcuts

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