timeseries

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2020 License: MIT Imports: 6 Imported by: 0

README

timeseries

threadsafe timeseries package for gophers

GoDoc Build Status codecov

Benchmark:

goos: windows
goarch: amd64
pkg: github.com/axamon/timeseries
BenchmarkAddNewPoint-4   	 5000000	       364 ns/op	      70 B/op	       0 allocs/op
PASS
ok  	github.com/axamon/timeseries	2.443s
Success: Benchmarks passed.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Point

type Point struct {
	X int64
	Y float64
}

Point rappresent a point present in a time serie.

type Timeseries

type Timeseries struct {
	XY map[int64]float64

	sync.Mutex
	// contains filtered or unexported fields
}

Timeseries is the type for time series.

func FromSlice

func FromSlice(start time.Time, step time.Duration, s []float64) (ts *Timeseries, err error)

FromSlice returns a new timeserie created with the data in the slice passed as argument.

Example
package main

import (
	"time"

	"github.com/axamon/timeseries"
)

func main() {

	s := []float64{0, 2.1, 3.2, 5.4, 6.001, 3.4}
	beginning, _ := time.Parse(time.RFC3339, "2019-09-02T10:15:00Z")

	ts, _ := timeseries.FromSlice(beginning, time.Duration(5*time.Hour), s)

	ts.Print()
}
Output:

0 	 1567419300000000000 	 0
1 	 1567437300000000000 	 2.1
2 	 1567455300000000000 	 3.2
3 	 1567473300000000000 	 5.4
4 	 1567491300000000000 	 6.001
5 	 1567509300000000000 	 3.4

func New

func New() *Timeseries

New creates a timeserie

func (*Timeseries) AddNewPoint

func (ts *Timeseries) AddNewPoint(v float64, x interface{}) error

AddNewPoint adds a point to the time serie.

func (*Timeseries) AddNewPointKeepLen added in v0.1.2

func (ts *Timeseries) AddNewPointKeepLen(v float64, x interface{}) error

AddNewPointKeepLen adds a point to the time serie and keep a certain number of points.

Example
package main

import (
	"github.com/axamon/timeseries"
)

func main() {

	ts := timeseries.New()

	ts.AddNewPoint(0.48, int64(8))
	ts.AddNewPoint(0.49, int64(9))
	ts.AddNewPoint(0.410, int64(10))
	ts.AddNewPoint(0.50002, int64(11))
	ts.AddNewPointKeepLen(0.8, int64(12))
	ts.AddNewPointKeepLen(0.9, int64(13))
	ts.AddNewPointKeepLen(1.1, int64(13))
	// fmt.Printf("%v",ts)

	ts.Print()
}
Output:

0 	 10 	 0.41
1 	 11 	 0.50002
2 	 12 	 0.8
3 	 13 	 1.1

func (*Timeseries) AddValueToIndex

func (ts *Timeseries) AddValueToIndex(v float64, i int64)

AddValueToIndex adds the value v at the value present at the index specified.

func (*Timeseries) AddValueToTime

func (ts *Timeseries) AddValueToTime(v float64, t time.Time)

AddValueToTime adds the value v at the value present at the timestamp specified.

func (*Timeseries) FindNextPoint

func (ts *Timeseries) FindNextPoint(i int64) Point

FindNextPoint returns the next point in the timeserie based on the intt64 index passed as argument. If not available it will return a Point with 0,0

func (*Timeseries) FindPreviousPoint

func (ts *Timeseries) FindPreviousPoint(i int64) Point

FindPreviousPoint returns the previous point in the timeserie based on the intt64 index passed as argument. If not available it will return a Point with 0,0

func (*Timeseries) FirstX

func (ts *Timeseries) FirstX() int64

FirstX returns the beginning timestamp of the serie.

Example
package main

import (
	"fmt"
	"log"
	"math/rand"
	"time"

	"github.com/axamon/timeseries"
)

func main() {
	rand.Seed(1234567)
	ts := timeseries.New()
	beginning, err := time.Parse(time.RFC3339, "2019-09-02T10:15:00Z")
	if err != nil {
		log.Fatal(err)
	}

	for i := 0; i < 10; i++ {

		ts.AddNewPoint(float64(rand.Int63n(50)), beginning.Add(time.Duration(i*3)*time.Minute))
	}

	fmt.Println(ts.FirstX())
	fmt.Println(time.Unix(ts.FirstX()/1000000000, 0).UTC().Format(time.RFC3339))
}
Output:

1567419300000000000
2019-09-02T10:15:00Z

func (*Timeseries) FirstY

func (ts *Timeseries) FirstY() float64

FirstY returns the beginning value of the serie.

func (*Timeseries) GetOrderedIndex added in v0.1.4

func (ts *Timeseries) GetOrderedIndex() []int64

GetOrderedIndex returns the orderd slice of X from map XY.

Example
package main

import (
	"fmt"

	"github.com/axamon/timeseries"
)

func main() {

	ts := timeseries.New()

	ts.AddNewPoint(0.48, int64(12))
	ts.AddNewPoint(0.49, int64(13))
	ts.AddNewPoint(0.410, int64(15))
	ts.AddNewPoint(0.50002, int64(14))
	ts.AddNewPoint(0.48, int64(8))
	ts.AddNewPoint(0.49, int64(9))
	ts.AddNewPoint(0.410, int64(10))
	ts.AddNewPoint(0.50002, int64(11))

	index := ts.GetOrderedIndex()

	for _, i := range index {
		fmt.Println(i)
	}
}
Output:

8
9
10
11
12
13
14
15

func (*Timeseries) GetPoint added in v0.1.4

func (ts *Timeseries) GetPoint(x int64) Point

GetPoint retrievs a point of the timeserie.

Example
package main

import (
	"fmt"

	"github.com/axamon/timeseries"
)

func main() {
	ts := timeseries.New()

	ts.AddNewPoint(0.48, int64(12))
	ts.AddNewPoint(0.49, int64(13))
	ts.AddNewPoint(0.410, int64(15))
	ts.AddNewPoint(0.50002, int64(14))
	ts.AddNewPoint(0.48, int64(8))
	ts.AddNewPoint(0.49, int64(9))
	ts.AddNewPoint(0.410, int64(10))
	ts.AddNewPoint(0.50002, int64(11))

	p := ts.GetPoint(9)

	fmt.Println(p.X, p.Y)
}
Output:

9 0.49

func (*Timeseries) GetPreviousPoint added in v0.1.4

func (ts *Timeseries) GetPreviousPoint(x int64) Point

GetPreviousPoint retrievs the previous point.

Example
package main

import (
	"fmt"

	"github.com/axamon/timeseries"
)

func main() {
	ts := timeseries.New()

	ts.AddNewPoint(0.48, int64(12))
	ts.AddNewPoint(0.49, int64(13))
	ts.AddNewPoint(0.410, int64(15))
	ts.AddNewPoint(0.50002, int64(14))
	ts.AddNewPoint(0.48, int64(8))
	ts.AddNewPoint(0.49, int64(9))
	ts.AddNewPoint(0.410, int64(10))
	ts.AddNewPoint(0.50002, int64(11))

	p := ts.GetPreviousPoint(9)

	fmt.Println(p.X, p.Y)
}
Output:

8 0.48

func (*Timeseries) LastX

func (ts *Timeseries) LastX() int64

LastX returns the ending timestamp of the serie.

Example
package main

import (
	"fmt"
	"log"
	"math/rand"
	"time"

	"github.com/axamon/timeseries"
)

func main() {
	rand.Seed(1234567)
	ts := timeseries.New()
	beginning, err := time.Parse(time.RFC3339, "2019-09-02T10:15:00Z")
	if err != nil {
		log.Fatal(err)
	}

	for i := 0; i < 10; i++ {

		ts.AddNewPoint(float64(rand.Int63n(50)), beginning.Add(time.Duration(i*3)*time.Minute))
	}

	fmt.Println(ts.LastX())
	fmt.Println(time.Unix(ts.LastX()/1000000000, 0).UTC().Format(time.RFC3339))
}
Output:

1567420920000000000
2019-09-02T10:42:00Z

func (*Timeseries) LastY

func (ts *Timeseries) LastY() float64

LastY returns the ending value of the serie.

func (*Timeseries) Len

func (ts *Timeseries) Len() int

Len returns the length of the timeserie.

func (*Timeseries) Print

func (ts *Timeseries) Print()

Print prints all the points in the timeserie.

Example
package main

import (
	"github.com/axamon/timeseries"
)

func main() {

	ts := timeseries.New()

	ts.AddNewPoint(0.43, int64(10))
	ts.AddNewPoint(0.50002, int64(11))

	ts.Print()
}
Output:

0 	 10 	 0.43
1 	 11 	 0.50002

func (*Timeseries) PrintFormattedTime

func (ts *Timeseries) PrintFormattedTime()

PrintFormattedTime prints all the points in the timeserie, with times formatted as RFC339

func (*Timeseries) ToSlice

func (ts *Timeseries) ToSlice() []float64

ToSlice creates a slice with the values of the time serie.

Example
package main

import (
	"fmt"
	"log"
	"math/rand"
	"time"

	"github.com/axamon/timeseries"
)

func main() {
	rand.Seed(1234567)
	ts := timeseries.New()
	beginning, err := time.Parse(time.RFC3339, "2019-09-02T10:15:00Z")
	if err != nil {
		log.Fatal(err)
	}

	for i := 0; i < 10; i++ {

		ts.AddNewPoint(float64(rand.Int63n(50)), beginning.Add(time.Duration(i*3)*time.Minute))
	}

	s := ts.ToSlice()

	fmt.Println(s)
}
Output:

[26 29 37 41 42 11 36 30 20 18]

func (*Timeseries) XtoSliceFloat64 added in v0.1.1

func (ts *Timeseries) XtoSliceFloat64() []float64

XtoSliceFloat64 creates a slice []float64 with the timetamps of the timeserie.

func (*Timeseries) XtoSliceInt64 added in v0.1.1

func (ts *Timeseries) XtoSliceInt64() []int64

XtoSliceInt64 creates a slice []int64 with the timetamps of the timeserie.

Jump to

Keyboard shortcuts

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