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) }
Output:
Index ¶
- type Datasetdeprecated
- func (d *Dataset) Accumulate()
- func (d *Dataset) Add(timestamp time.Time, column string, value float64)
- func (d *Dataset) AddColumn(column string, processor func(values map[string]float64) float64)
- func (d *Dataset) Copy() (clone *Dataset)
- func (d *Dataset) FilterByRange(from, to time.Time)
- func (d *Dataset) GenerateTableResponse() (response *query.TableResponse)
- func (d *Dataset) GetColumns() (columns []string)
- func (d *Dataset) GetTimestamps() (timestamps []time.Time)
- func (d *Dataset) GetValues(column string) (values []float64, ok bool)
- func (d *Dataset) Size() int
- type Indexer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dataset
deprecated
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.
Deprecated: use data.Table instead
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 ¶
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 ¶
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) FilterByRange ¶
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 ¶
GetColumns returns the (sorted) list of column names.
func (*Dataset) GetTimestamps ¶
GetTimestamps returns the (sorted) list of timestamps 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 (*Indexer[T]) Add ¶
Add adds a new value to the Indexer. It returns the index of that value and whether the value was actually added.