aggro

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

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

Go to latest
Published: Nov 1, 2017 License: MIT Imports: 8 Imported by: 0

README

aggro 😡

GoDoc Go Report Card

In memory dataset bucketing and metrics, inspired by Elasticsearch aggregations

Installation

go get -u github.com/snikch/aggro

Example

Given a dataset...

rows := []map[string]interface{}{
		{"location": "Auckland", "department": "Engineering", "salary": 120000, "start_date": "2016-01-31T22:00:00Z"},
		{"location": "Auckland", "department": "Engineering", "salary": 80000, "start_date": "2016-03-23T22:00:00Z"},
		{"location": "Auckland", "department": "Marketing", "salary": 90000, "start_date": "2016-01-31T22:00:00Z"},
		{"location": "Auckland", "department": "Marketing", "salary": 150000, "start_date": "2016-01-23T22:00:00Z"},
		{"location": "Wellington", "department": "Engineering", "salary": 120000, "start_date": "2016-01-23T22:00:00Z"},
		{"location": "Wellington", "department": "Engineering", "salary": 160000, "start_date": "2016-03-23T22:00:00Z"},
		{"location": "Wellington", "department": "Engineering", "salary": 120000, "start_date": "2016-02-02T22:00:00Z"},
	}

Initialize aggro, build aggregations and run...

// Build a dataset that contains a *Table representing your data.
dataset := &Dataset{
	Table: &Table{
		Fields: []Field{
			{"location", "string"},
			{"department", "string"},
			{"salary", "number"},
			{"start_date", "datetime"},
		},
	},
}

// Add our rows to our dataset.
err := dataset.AddRows(rows...)
if err != nil {
	return err
}

// Build our query specifying preferred metrics and bucket composition.
query := &Query{
    Metrics: []Metric{
        {Type: "max", Field: "salary"},
        {Type: "min", Field: "salary"},
    },
    Bucket: &Bucket{
        Field: &Field{
            Name: "location",
            Type: "string",
        },
        Sort: &SortOptions{
            Type: "alphabetical",
        },
        Bucket: &Bucket{
            Field: &Field{
                Name: "department",
                Type: "string",
            },
            Sort: &SortOptions{
                Type: "alphabetical",
            },
        },
    },
}

// Run it.
results, err := dataset.Run(query)
if err != nil {
	return err
}

Find a list of available measurers here

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTargetDepthTooLow     = fmt.Errorf("Tabulate: target depth should be 1 or above")
	ErrTargetDepthNotReached = fmt.Errorf("Tabulate: reached deepest bucket before hitting target depth")
)

Concrete errors.

View Source
var MetricDelimeter = ":"

MetricDelimeter is a string used to separate metric field's from names.

Functions

This section is empty.

Types

type AlphabeticalSortable

type AlphabeticalSortable bool

AlphabeticalSortable is a simple sorter that sorts alphabetically in the direction of the boolean, true meaning ascending and false being descending.

func (*AlphabeticalSortable) Less

func (sortable *AlphabeticalSortable) Less(a, b *ResultBucket) bool

Less implements Sortable by comparing the value field of each result.

type BooleanCell

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

BooleanCell implements the Cell{} interface.

func (*BooleanCell) FieldDefinition

func (cell *BooleanCell) FieldDefinition() *Field

FieldDefinition returns the field definition (name) representing the cell.

func (*BooleanCell) IsMetricable

func (cell *BooleanCell) IsMetricable(m measurer) bool

IsMetricable determines whether the measurer{} provided can be run by the cell.

func (*BooleanCell) MeasurableCell

func (cell *BooleanCell) MeasurableCell() MeasurableCell

MeasurableCell returns the cells MeasurableCell{}.

func (*BooleanCell) Value

func (cell *BooleanCell) Value() interface{}

Value returns the cell value.

type Bucket

type Bucket struct {
	Bucket          *Bucket
	Field           *Field
	DatetimeOptions *DatetimeBucketOptions
	Sort            *SortOptions
	RangeOptions    *RangeBucketOptions
}

Bucket defines how to compare and group data which is then aggregated on.

type Cell

type Cell interface {
	FieldDefinition() *Field
	IsMetricable(m measurer) bool
	MeasurableCell() MeasurableCell
}

Cell represents data and configuration for each of our *Table.Fields.

type Dataset

type Dataset struct {
	Table *Table
	Rows  []map[string]Cell
}

Dataset holds our *Table representation of *Fields and its matching Cell data.

func (*Dataset) AddRows

func (set *Dataset) AddRows(rows ...map[string]interface{}) error

AddRows creates a Cell{} for each of our Table.Fields and ensures the cells data meets the cells defined format.

func (*Dataset) Run

func (set *Dataset) Run(query *Query) (*Resultset, error)

Run executes the query against the dataset.

type DatetimeBucketOptions

type DatetimeBucketOptions struct {
	// Start will, if provided, ensure buckets start at this date.
	Start *time.Time
	// End will, if provided, ensure buckets continue to this date.
	End *time.Time
	// What interval period are the results to be bucketed at.
	Period DatetimePeriod
	// Datetimes should be bucketed based on the date in this location.
	Location *time.Location
}

DatetimeBucketOptions provides additional configuration for datetime bucketing.

type DatetimeCell

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

DatetimeCell implements the Cell interface.

func (*DatetimeCell) FieldDefinition

func (cell *DatetimeCell) FieldDefinition() *Field

FieldDefinition returns the field definition (name) representing the cell.

func (*DatetimeCell) IsMetricable

func (cell *DatetimeCell) IsMetricable(m measurer) bool

IsMetricable determines whether the measurer{} provided can be run by the cell.

func (*DatetimeCell) MeasurableCell

func (cell *DatetimeCell) MeasurableCell() MeasurableCell

MeasurableCell returns the cells MeasurableCell{}.

func (*DatetimeCell) Value

func (cell *DatetimeCell) Value() interface{}

Value returns the cell value.

func (*DatetimeCell) ValueForPeriod

func (cell *DatetimeCell) ValueForPeriod(period DatetimePeriod, location *time.Location) (string, error)

ValueForPeriod returns the start of a given period.

type DatetimePeriod

type DatetimePeriod string

DatetimePeriod provides a string type to represent a date bucketing period.

const (
	Year    DatetimePeriod = "year"
	Quarter DatetimePeriod = "quarter"
	Month   DatetimePeriod = "month"
	Week    DatetimePeriod = "week"
	Day     DatetimePeriod = "hour"
)

Helper constants representing acceptable DatetimePeriods.

type Field

type Field struct {
	Name string
	Type string
}

Field represents an individual Field within our Dataset.Table.

type MeasurableCell

type MeasurableCell interface {
	Value() interface{}
}

MeasurableCell returns the cells MeasurableCell{}.

type Metric

type Metric struct {
	Type  string
	Field string
}

Metric represents a type of measurement to be applied to our dataset.

type NumberCell

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

NumberCell implements the Cell interface.

func (*NumberCell) FieldDefinition

func (cell *NumberCell) FieldDefinition() *Field

FieldDefinition returns the field definition (name) representing the cell.

func (*NumberCell) IsMetricable

func (cell *NumberCell) IsMetricable(m measurer) bool

IsMetricable determines whether the measurer{} provided can be run by the cell.

func (*NumberCell) MeasurableCell

func (cell *NumberCell) MeasurableCell() MeasurableCell

MeasurableCell returns the cells MeasurableCell{}.

func (*NumberCell) Value

func (cell *NumberCell) Value() interface{}

Value returns the cell value.

func (*NumberCell) ValueForPeriod

func (cell *NumberCell) ValueForPeriod(period []interface{}) (string, error)

ValueForPeriod returns the start of a given period.

type NumericalSortable

type NumericalSortable bool

NumericalSortable is a simple sorter that sorts numerically in the direction of the boolean, true meaning ascending and false being descending.

func (*NumericalSortable) Less

func (sortable *NumericalSortable) Less(a, b *ResultBucket) bool

Less implements Sortable by comparing the value field of each result.

type Query

type Query struct {
	Bucket  *Bucket
	Metrics []Metric
}

Query represents the bucketing and aggregate metrics that should be run.

type RangeBucketOptions

type RangeBucketOptions struct {
	Period []interface{}
}

RangeBucketOptions provides additional configuration for custom range bucketing.

type ResultBucket

type ResultBucket struct {
	Value   string                 `json:"value"`
	Metrics map[string]interface{} `json:"metrics"`
	Buckets []*ResultBucket        `json:"buckets"`
	// contains filtered or unexported fields
}

ResultBucket represents recursively built metrics for our tablular data.

type ResultTable

type ResultTable struct {
	Rows         [][]map[string]interface{} `json:"rows"`
	RowTitles    [][]string                 `json:"row_titles"`
	ColumnTitles [][]string                 `json:"column_titles"`
}

ResultTable represents a Resultset split into row / columns at a depth.

func Tabulate

func Tabulate(results *Resultset, depth int) (*ResultTable, error)

Tabulate takes a Resultset and converts it to tabular data.

type Resultset

type Resultset struct {
	Errors      []error         `json:"errors"`
	Buckets     []*ResultBucket `json:"buckets"`
	Composition []interface{}   `json:"-"`
}

Resultset represents a complete set of result buckets and any associated errors.

type SortOptions

type SortOptions struct {
	Type   string
	Metric string
	Desc   bool
}

SortOptions represent how this Bucket should be sorted.

type Sortable

type Sortable interface {
	Less(a, b *ResultBucket) bool
}

Sortable provides an interface that various sorters can implement to compare two result buckets. This enables access to both the value and any metrics that the query may have contained.

type StringCell

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

StringCell implements the Cell{} interface.

func (*StringCell) FieldDefinition

func (cell *StringCell) FieldDefinition() *Field

FieldDefinition returns the field definition (name) representing the cell.

func (*StringCell) IsMetricable

func (cell *StringCell) IsMetricable(m measurer) bool

IsMetricable determines whether the measurer{} provided can be run by the cell.

func (*StringCell) MeasurableCell

func (cell *StringCell) MeasurableCell() MeasurableCell

MeasurableCell returns the cells MeasurableCell{}.

func (*StringCell) Value

func (cell *StringCell) Value() interface{}

Value returns the cell value.

type Table

type Table struct {
	Fields []Field
}

Jump to

Keyboard shortcuts

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