boltrrd

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

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

Go to latest
Published: Aug 18, 2015 License: GPL-2.0 Imports: 8 Imported by: 0

README

BoltRRD

RRD like tool for storage graph data in BoltDB.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnectionError

type ConnectionError struct {
	Text string
	Err  error
}

ConnectionError Problems with connection to db

func (*ConnectionError) Error

func (e *ConnectionError) Error() string

type DataError

type DataError struct {
	Text string
	Err  error
}

DataError Bad format of data in storage

func (*DataError) Error

func (e *DataError) Error() string

type Dataset

type Dataset struct {
	Label string    `json:"label"`
	Data  []float64 `json:"data"`
}

Dataset Part of json data structure. Represents values for every data storage

type ParamsError

type ParamsError struct {
	Text string
	Err  error
}

ParamsError Wrong params in call of interface functions

func (*ParamsError) Error

func (e *ParamsError) Error() string

type RRDBucket

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

RRDBucket Struct to represent single bucket with stored data

func (*RRDBucket) GetStats

func (rrd *RRDBucket) GetStats(from, to time.Time, step int) (stat *Stat, err error)

GetStats Return data for the specified time interval and scaled with step

func (*RRDBucket) Save

func (rrd *RRDBucket) Save(params ...float64) error

Save Save dataset to bucket whith current time. Number of params must be equal to number of data storages in bucket

type Stat

type Stat struct {
	Labels   []string  `json:"labels"`
	Datasets []Dataset `json:"datasets"`
}

Stat Structure to represent values stored in db in format which suitable for js charting libraries

type Storage

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

Storage Main struct for bolt database file, contains methods for connecting to db and for storages access

func NewStorage

func NewStorage(dbpath string) (*Storage, error)

NewStorage Open file with db and create connection with it, also load info about all buckets to Storage struct

Example
package main

import (
	"fmt"
	"os"
	"time"

	"github.com/geekbros/SHM-Backend/boltrrd"
)

func main() {
	// Structure of storage:
	// file_bolt.db
	// |-bucket
	// | |-entry_1
	// | | |-data_storage1 - value
	// | | |-data_storage2 - value
	// | |-entry_2
	// | | |-data_storage1 - value
	// | | |-data_storage2 - value
	// | |-info_entry

	//Create bolt db file
	stor, err := boltrrd.NewStorage("bolt.db")
	defer func() {
		stor.Close()
	}()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	//Create new rrd storage-bucket
	buckName := "Data"              //Name of bucket
	buckStep := 1                   //Step in seconds
	buckCapacity := 10              //Number of steps which can store in bucket
	DSs := []string{"Val1", "Val2"} //Data storages

	stor.NewRRD(buckName, buckStep, buckCapacity, DSs...)
	stor.NewRRD("Network", 10, 6, "in", "out")

	//Save data to storage
	for i := 0; i < 10; i++ {
		err := stor.RRD(buckName).Save(float64(i), float64(i*2)) // Number of parameters for .save() must be equal to number of data storages
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		time.Sleep(time.Duration(buckStep) * time.Second) // Every entry saves with parameter time.Now() so wait a second
	}

	//Get saved data
	stat, err := stor.RRD(buckName).GetStats(time.Now().Add(-10*time.Second), time.Now(), buckStep)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println(stat)

}
Output:

&{[{Val1 [0 1 2 3 4 5 6 7 8 9]} {Val2 [0 2 4 6 8 10 12 14 16 18]}]}

func (*Storage) Close

func (s *Storage) Close() error

Close Close connection to db file

func (*Storage) ListRRD

func (s *Storage) ListRRD() (rrds []string, err error)

ListRRD Returns list of all buckets in storage

func (*Storage) NewRRD

func (s *Storage) NewRRD(name string, step int, capacity int, DSs ...string) (*RRDBucket, error)

NewRRD Create RRD bucket with given name. Step is time in seconds and capacity is number of steps which stored in bucket. Other parameters is a list of all data storages

func (*Storage) RRD

func (s *Storage) RRD(name string) *RRDBucket

RRD Provide access to bucket by its name

Jump to

Keyboard shortcuts

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