timeseries

package module
v0.0.0-...-bb8cc15 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2017 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

timeseries Build Status GoDoc

This is a Go package for encoding and decoding time-series data point in similar way to Facebook Gorilla time-series database. This package has not been tested thoroughly and may have bugs.

License

MIT

Documentation

Overview

Package timeseries implements encoder and decoder for time-series data point in similar to Facebook Gorilla time-series database. The spec is described in the paper "Gorilla: A Fast, Scalable, In-Memory Time Series Database" http://www.vldb.org/pvldb/vol8/p1816-teller.pdf

This implementation is based on a third party Java implementation https://github.com/burmanm/gorilla-tsc/ However this Go package removes the enhancements in this Java version.

  • The precision of timestamps is one second (not a milisecond).
  • The data point value type is flaot64 only.
  • The first timestamp delta is sized at 14 bits. This size span a bit more than 4 hours (16,384 seconds).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(t0 uint32, points []Point) ([]byte, error)

Marshal encodes a block timestamp and data points to bytes.

Example
package main

import (
	"encoding/hex"
	"fmt"
	"time"

	"github.com/hnakamur/timeseries"
)

func main() {
	t0 := uint32(time.Date(2015, 3, 24, 2, 0, 0, 0, time.UTC).Unix())
	points := []timeseries.Point{
		{
			Timestamp: uint32(time.Date(2015, 3, 24, 2, 1, 2, 0, time.UTC).Unix()),
			Value:     12.0,
		},
		{
			Timestamp: uint32(time.Date(2015, 3, 24, 2, 2, 2, 0, time.UTC).Unix()),
			Value:     12.0,
		},
		{
			Timestamp: uint32(time.Date(2015, 3, 24, 2, 3, 2, 0, time.UTC).Unix()),
			Value:     24.0,
		},
	}

	buf, err := timeseries.Marshal(t0, points)
	if err != nil {
		fmt.Printf("failed to marshal points: err=%+v\n", err)
		return
	}
	fmt.Println(hex.EncodeToString(buf))

}
Output:
5510c52000f900a0000000000002fc6b07ffffffffe0

Types

type Decoder

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

Decoder decodes bytes data to a block timestamp and data points.

Example
package main

import (
	"bytes"
	"encoding/hex"
	"fmt"
	"io"
	"time"

	"github.com/hnakamur/timeseries"
)

func main() {
	input, err := hex.DecodeString("5510c52000f900a0000000000002fc6b07ffffffffe0")
	if err != nil {
		fmt.Printf("failed to decode hex string: err=%+v\n", err)
		return
	}

	b := bytes.NewBuffer(input)
	dec := timeseries.NewDecoder(b)

	t0, err := dec.DecodeHeader()
	if err != nil {
		fmt.Printf("failed to decode time series header: err=%+v\n", err)
		return
	}

	var points []timeseries.Point
	for {
		p, err := dec.DecodePoint()
		if err == io.EOF {
			break
		} else if err != nil {
			fmt.Printf("failed to decode time series point: err=%+v\n", err)
			return
		}
		points = append(points, p)
	}

	fmt.Printf("block timestamp=%v\n", time.Unix(int64(t0), 0).UTC())
	for _, p := range points {
		fmt.Printf("timestamp=%v, value=%f\n", time.Unix(int64(p.Timestamp), 0).UTC(), p.Value)
	}

}
Output:
block timestamp=2015-03-24 02:00:00 +0000 UTC
timestamp=2015-03-24 02:01:02 +0000 UTC, value=12.000000
timestamp=2015-03-24 02:02:02 +0000 UTC, value=12.000000
timestamp=2015-03-24 02:03:02 +0000 UTC, value=24.000000

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder creates a decoder.

func (*Decoder) DecodeHeader

func (d *Decoder) DecodeHeader() (t0 uint32, err error)

DecodeHeader decodes header to the block timestamp.

func (*Decoder) DecodePoint

func (d *Decoder) DecodePoint() (p Point, err error)

DecodePoint decodes a data point. It returns io.EOF when it see the finish marker or it got EOF from the underlying reader.

type Encoder

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

Encoder encodes time series data in similar way to Facebook Gorilla in-memory time series database.

Example
package main

import (
	"bytes"
	"encoding/hex"
	"fmt"
	"time"

	"github.com/hnakamur/timeseries"
)

func main() {
	t0 := uint32(time.Date(2015, 3, 24, 2, 0, 0, 0, time.UTC).Unix())
	points := []timeseries.Point{
		{
			Timestamp: uint32(time.Date(2015, 3, 24, 2, 1, 2, 0, time.UTC).Unix()),
			Value:     12.0,
		},
		{
			Timestamp: uint32(time.Date(2015, 3, 24, 2, 2, 2, 0, time.UTC).Unix()),
			Value:     12.0,
		},
		{
			Timestamp: uint32(time.Date(2015, 3, 24, 2, 3, 2, 0, time.UTC).Unix()),
			Value:     24.0,
		},
	}

	var b bytes.Buffer
	enc := timeseries.NewEncoder(&b)
	err := enc.EncodeHeader(t0)
	if err != nil {
		fmt.Printf("failed to encode time series header: err=%+v\n", err)
		return
	}

	for _, p := range points {
		err := enc.EncodePoint(p)
		if err != nil {
			fmt.Printf("failed to encode time series point: err=%+v\n", err)
			return
		}
	}

	err = enc.Finish()
	if err != nil {
		fmt.Printf("failed to encode time series finish marker: err=%+v\n", err)
		return
	}

	fmt.Println(hex.EncodeToString(b.Bytes()))

}
Output:
5510c52000f900a0000000000002fc6b07ffffffffe0

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder creates a new encoder.

func (*Encoder) EncodeHeader

func (e *Encoder) EncodeHeader(t0 uint32) error

EncodeHeader encodes the block timestamp to the header bits.

func (*Encoder) EncodePoint

func (e *Encoder) EncodePoint(p Point) error

EncodePoint encodes a data point.

func (*Encoder) Finish

func (e *Encoder) Finish() error

Finish encodes the finish marker and flush bits with zero bits padding for byte-align.

type Point

type Point struct {
	// Timestamp represents the seconds since 1970-01-01 00:00:00 +0000 UTC.
	// The max value is 2106-02-07 06:28:15 +0000 UTC.
	// You can verify the max value at https://play.golang.org/p/XgHveabiUd
	Timestamp uint32

	// Value represents the data point value.
	Value float64
}

Point is a time-series data point.

func Unmarshal

func Unmarshal(data []byte) (t0 uint32, points []Point, err error)

Unmarshal decodes bytes to a block timestamp and data points.

Example
package main

import (
	"encoding/hex"
	"fmt"
	"time"

	"github.com/hnakamur/timeseries"
)

func main() {
	input, err := hex.DecodeString("5510c52000f900a0000000000002fc6b07ffffffffe0")
	if err != nil {
		fmt.Printf("failed to decode hex string: err=%+v\n", err)
		return
	}

	t0, points, err := timeseries.Unmarshal(input)
	if err != nil {
		fmt.Printf("failed to unmarshal time series: err=%+v\n", err)
		return
	}
	fmt.Printf("block timestamp=%v\n", time.Unix(int64(t0), 0).UTC())
	for _, p := range points {
		fmt.Printf("timestamp=%v, value=%f\n", time.Unix(int64(p.Timestamp), 0).UTC(), p.Value)
	}

}
Output:
block timestamp=2015-03-24 02:00:00 +0000 UTC
timestamp=2015-03-24 02:01:02 +0000 UTC, value=12.000000
timestamp=2015-03-24 02:02:02 +0000 UTC, value=12.000000
timestamp=2015-03-24 02:03:02 +0000 UTC, value=24.000000

Jump to

Keyboard shortcuts

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