tsmgo

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

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

Go to latest
Published: Aug 27, 2018 License: MIT Imports: 4 Imported by: 0

README

Build Status Coverage Status Go Report Card GoDoc

tsmgo

#golang library which makes intuitive to work with time-series data in Mongo DB.

The schema design is inspired by this article.

Using tsmgo

import "github.com/danielfireman/tsmgo"

New tsmgo.Session

tsSession := tsmgo.NewSession(mgoSession)
c, _ := tsSession.C(dbName, colName)

or

tsSession, err := tsmgo.Dial(mongoURL)
c, _ := tsSession.C(dbName, colName)

Where mgoSession is a github.com/globalsign/mgo#Session.

Adding timeseries records

c.Upsert(myField, TSRecord{time.Now(), 1})

or

t1 := time.Now()
t2 := t1.Add(10 * time.Second)
c.Upsert(myField, TSRecord{t1, 1}, TSRecord{t2, 2})

Retrieving last item inserted

last, _ := tsmgoC.Last(myField)

Retrieving all itens inserted in the last 24 hours

now := time.Now()
items, _ := tsmgoC.Interval(myField, now.Add(-24*time.Hour), now)

Contributing

  1. Install dep
  2. dep ensure
  3. go test -v

Either if you're fixing a bug or adding a new feature, please add a test to cover it.

If all tests passes, please you're ready to send the PR.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

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

Collection represents a timeseries collection in a mongo database.

Example
mgoSession = mongoDB.Session()
defer mongoDB.Wipe()
defer mgoSession.Close()

s := NewSession(mgoSession)
defer s.Close()

myField := "myfield"
t1 := time.Now()
t2 := t1.Add(10 * time.Second)

tsmgoC, _ := s.C(dbName, colName)
tsmgoC.Upsert(myField, TSRecord{t1, 1}, TSRecord{t2, 2})

last, _ := tsmgoC.Last(myField)
fmt.Println(last.Value)

records, _ := tsmgoC.Interval(myField, t1, t2)
sort.Sort(InverseChronologicalOrdering(records))
fmt.Println(records[0].Value, records[1].Value)
Output:

2
2 1

func (*Collection) Interval

func (c *Collection) Interval(field string, start time.Time, finish time.Time) ([]TSRecord, error)

Interval fetches all records from timeseries mongo within the specified (closed) interval. If no records are found, an empty slice is returned.

func (*Collection) Last

func (c *Collection) Last(field string) (TSRecord, error)

Last returns the last element in the timeseries, if any.

func (*Collection) Upsert

func (c *Collection) Upsert(field string, val ...TSRecord) (UpsertResult, error)

Upsert bulk-inserts the given data into the timeseries database overriding the data if necessary.

type InverseChronologicalOrdering

type InverseChronologicalOrdering []TSRecord

InverseChronologicalOrdering sorts TSRecords by timestamp (inverse )

func (InverseChronologicalOrdering) Len

func (InverseChronologicalOrdering) Less

func (s InverseChronologicalOrdering) Less(i, j int) bool

func (InverseChronologicalOrdering) Swap

func (s InverseChronologicalOrdering) Swap(i, j int)

type Session

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

Session represents a connection to a mongo timeseries collection.

func Dial

func Dial(uri string) (*Session, error)

Dial sets up a connection to the specified timeseries database specified by the passed-in URI.

func NewSession

func NewSession(s *mgo.Session) *Session

NewSession creates a new Session instance based on a copy of the passed-in instance. This returned allows the communication to the underlying timeseries mongo database.

func (*Session) C

func (s *Session) C(db, coll string) (*Collection, error)

C creates a new collection with the given name.

func (*Session) Close

func (s *Session) Close()

Close terminates the session. It's a runtime error to use a session after it has been closed.

func (*Session) Copy

func (s *Session) Copy() *Session

Copy works just like New, but preserves the database and any authentication information from the original session.

type TSRecord

type TSRecord struct {
	Timestamp time.Time   `bson:"timestamp,omitempty"` // bson name should match timestampIndexField.
	Value     interface{} `bson:"value,omitempty"`
}

TSRecord represents a value to be added to timeseries database.

type UpsertResult

type UpsertResult struct {
	Matched  int
	Modified int
}

UpsertResult holds the results for a timeseries upsert operation.

Jump to

Keyboard shortcuts

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