dataset

package
v3.4.2 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2022 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package dataset makes it easier to produce time-based responses when dealing with data that may not necessarily be sequential.

A dataset holds a table of rows for each timestamp that is added to the dataset. When adding new columns, empty cells are automatically added to the table for existing rows:

d := dataset.New()          // creates an empty dataset
d.Add(time.Now(), "A", 1)   // dataset has one row with a single cell, set to 1
d.Add(time.Now(), "B", 2)   // dataset now has two rows. First row is 1, 0. Second row is 0, 2

Furthermore, dataset allows to add a new column, calculated from the values of the other columns:

d := dataset.New()
// add rows with values for columns "A" and "B"

d.AddColumn("C", func(values map[string]float64) float64 {
  return values["A"] + values["B"]
})
// dataset now has a column "C", with the sum of columns "A" and "B"
Example
package main

import (
	"fmt"
	"github.com/clambin/simplejson/v3/dataset"
	"time"
)

func main() {
	d := dataset.New()

	for day := 1; day < 5; day++ {
		d.Add(time.Date(2022, time.January, 5-day, 0, 0, 0, 0, time.UTC), "A", float64(5-day))
	}

	d.AddColumn("B", func(values map[string]float64) float64 {
		return values["A"] * 2
	})

	response := d.GenerateTableResponse()

	fmt.Printf("%v\n", response)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dataset

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

Dataset is a convenience data structure to construct a SimpleJSON table response. Use this when you're adding data for a range of (possibly out of order) timestamps.

func New

func New() *Dataset

New creates a new Dataset

func (*Dataset) Accumulate

func (d *Dataset) Accumulate()

Accumulate accumulates the values for each column by time. E.g. if the values were 1, 1, 1, 1, the result would be 1, 2, 3, 4.

func (*Dataset) Add

func (d *Dataset) Add(timestamp time.Time, column string, value float64)

Add adds a value for a specified timestamp and column to the dataset. If there is already a value for that timestamp and column, the specified value is added to the existing value.

func (*Dataset) AddColumn

func (d *Dataset) AddColumn(column string, processor func(values map[string]float64) float64)

AddColumn adds a new column to the dataset. For each timestamp, processor is called with the values for the existing columns. Processor's return value is then added for the new column.

func (Dataset) Copy

func (d Dataset) Copy() (clone *Dataset)

Copy returns a copy of the dataset

func (*Dataset) FilterByRange

func (d *Dataset) FilterByRange(from, to time.Time)

FilterByRange removes any rows in the dataset that are outside the specified from/to time range. If from/to is zero, it is ignored.

func (Dataset) GenerateTableResponse

func (d Dataset) GenerateTableResponse() (response *query.TableResponse)

GenerateTableResponse creates a TableResponse for the dataset

func (Dataset) GetColumns

func (d Dataset) GetColumns() (columns []string)

GetColumns returns the (sorted) list of column names.

func (Dataset) GetTimestamps

func (d Dataset) GetTimestamps() (timestamps []time.Time)

GetTimestamps returns the (sorted) list of timestamps in the dataset.

func (Dataset) GetValues

func (d Dataset) GetValues(column string) (values []float64, ok bool)

GetValues returns the value for the specified column for each timestamp in the dataset. The values are sorted by timestamp.

func (Dataset) Size

func (d Dataset) Size() int

Size returns the number of rows in the dataset.

type Indexer

type Indexer[T ordered] struct {
	// contains filtered or unexported fields
}

Indexer holds a unique set of values, and records the order in which they were added. Currently, it supports string and time.Time data.

func MakeIndexer

func MakeIndexer[T ordered]() *Indexer[T]

MakeIndexer returns a new indexer

func (*Indexer[T]) Add

func (idx *Indexer[T]) Add(value T) (index int, added bool)

Add adds a new value to the Indexer. It returns the index of that value and whether the value was actually added.

func (Indexer[T]) Copy

func (idx Indexer[T]) Copy() (clone *Indexer[T])

Copy returns a copy of the Indexer

func (Indexer[T]) Count

func (idx Indexer[T]) Count() int

Count returns the number of values in the Indexer

func (Indexer[T]) GetIndex

func (idx Indexer[T]) GetIndex(value T) (index int, found bool)

GetIndex returns the index of a value (i.e. when that value was added)

func (*Indexer[T]) List

func (idx *Indexer[T]) List() (values []T)

List returns the (sorted) values in the Indexer

Jump to

Keyboard shortcuts

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