GithubHelp home page GithubHelp logo

go-timeseries's Introduction

go-timeseries

Golang Timeseries Data Bucket

Get Package

go get github.com/sangwonl/go-timeseries

Import Package

import "github.com/sangwonl/go-timeseries"

Create a TimeSeries Struct

ts := timeseries.NewTimeSeries(<PrimitiveFactory>, <ResolutionList>)

Add Data with Time

i := Integer(1)
ts.Add(&i, time.Now())

Extract Data in Time Range

rangeVals = ts.Range(resolutionIdx, beginTime, endTime)
firstBucketVal = rangeVals[0].(*Integer).Value()
secondBucketVal = rangeVals[1].(*Integer).Value()

Example

import (
    "time"
    "github.com/sangwonl/go-timeseries"
)

func TestTimeSeries() {
    primitiveCreator := timeseries.NewInteger
    resolutions := []time.Duration{
        timeseries.ResolutionOneSecond,
        timeseries.ResolutionTenSeconds,
    }

    ts := timeseries.NewTimeSeries(primitiveCreator, resolutions)
    assertNotNil(t, ts)
    assertEqual(t, len(ts.dataStreams), 2)
    assertEqual(t, ts.dataStreams[0].NumBuckets(), 0)

    i := Integer(1)
    ts.Add(&i, asTime(1))
    ts.Add(&i, asTime(1))
    ts.Add(&i, asTime(1))
    ts.Add(&i, asTime(2))
    ts.Add(&i, asTime(2))
    ts.Add(&i, asTime(3))
    ts.Add(&i, asTime(3))
    ts.Add(&i, asTime(3))
    ts.Add(&i, asTime(3))
    ts.Add(&i, asTime(3))
    assertEqual(t, ts.dataStreams[0].NumBuckets(), 3)

    total := ts.Total().(*Integer)
    assertEqual(t, total.Value(), 10)

    resolutionIdx := 0
    var rangeVals []timeseries.Primitive
    var bucketVal int

    rangeVals = ts.Range(resolutionIdx, asTime(1), asTime(1))
    assertEqual(t, len(rangeVals), 1)

    bucketVal = rangeVals[0].(*Integer).Value()
    assertEqual(t, bucketVal, 3)

    rangeVals = ts.Range(resolutionIdx, asTime(1), asTime(2))
    assertEqual(t, len(rangeVals), 2)

    bucketVal = rangeVals[0].(*Integer).Value()
    bucketVal += rangeVals[1].(*Integer).Value()
    assertEqual(t, bucketVal, 5)

    rangeVals = ts.Range(resolutionIdx, asTime(2), asTime(2))
    assertEqual(t, len(rangeVals), 1)

    bucketVal = rangeVals[0].(*Integer).Value()
    assertEqual(t, bucketVal, 2)

    rangeVals = ts.Range(resolutionIdx, asTime(2), asTime(3))
    assertEqual(t, len(rangeVals), 2)

    bucketVal = rangeVals[0].(*Integer).Value()
    bucketVal += rangeVals[1].(*Integer).Value()
    assertEqual(t, bucketVal, 7)

    resolutionIdx = 1
    rangeVals = ts.Range(resolutionIdx, asTime(1), asTime(3))
    assertEqual(t, len(rangeVals), 1)

    bucketVal = rangeVals[0].(*Integer).Value()
    assertEqual(t, bucketVal, 10)
}

go-timeseries's People

Contributors

sangwonl avatar

Watchers

Priyabrata Dash avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.