aggregator

package module
v0.0.0-...-6cfdf1e Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2016 License: MIT Imports: 11 Imported by: 0

README

go-time-aggregator GoDoc Build Status

Examples

Adding to an aggrergator the number of hours that an employee works in 2016~2017 working days but August

yearLog, _ := aggregator.NewTimeAggregator(aggregator.Year, aggregator.YearDay)
startingDay := time.Date(2016, 1, 1, 0, 0, 0, 1, time.UTC)
for i := 0; i < 365*2; i++ {
    day := startingDay.Add(time.Duration(i*24) * time.Hour)
    if day.Weekday() > 0 && day.Weekday() < 6 && day.Month() != 8 {
        yearLog.Add(day, int64(8))
    }
}

Printing a representation of the days worked

fmt.Println(yearLog))

Output:

Year: 2016	▁█▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁██▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁  yeardays
Year: 2017	▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁█████▁▁▁  yeardays

License

MIT, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	InvalidOrderError = errors.New("Invalid order of time units")
	UnitsMismatch     = errors.New("Units mismatch")
)

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Period map[string]uint64
	Value  int64
}

type Period

type Period uint64

func (Period) Map

func (p Period) Map() (map[string]uint64, error)

ToMap returns a map representation of this period

func (Period) String

func (p Period) String() string

func (Period) Units

func (p Period) Units() []Unit

Units returns a slice of the units of the period

type Periods

type Periods []Period

func (Periods) Len

func (p Periods) Len() int

func (Periods) Less

func (p Periods) Less(i, j int) bool

func (Periods) Swap

func (p Periods) Swap(i, j int)

type TimeAggregator

type TimeAggregator struct {
	Values map[Period]*aggregator
	// contains filtered or unexported fields
}

func NewTimeAggregator

func NewTimeAggregator(units ...Unit) (*TimeAggregator, error)

NewTimeAggregator returns a new TimeAggregator configured with the given units, InvalidOrderError is return if the units are not sorted

func (*TimeAggregator) Add

func (a *TimeAggregator) Add(date time.Time, value int64)

Add sum a value to the correct count using date

func (*TimeAggregator) Entries

func (a *TimeAggregator) Entries() ([]Entry, error)

Entries return a list of Entry structs with non-zero values

func (*TimeAggregator) Get

func (a *TimeAggregator) Get(date time.Time) int64

Get returns the count for the given date

func (*TimeAggregator) GetBSON

func (a *TimeAggregator) GetBSON() (interface{}, error)

GetBSON implements bson.Setter, marshall the a TimeAggregator to a string ussing TimeAggregator.Marshal

func (*TimeAggregator) GobDecode

func (a *TimeAggregator) GobDecode(body []byte) error

func (*TimeAggregator) GobEncode

func (a *TimeAggregator) GobEncode() ([]byte, error)

func (*TimeAggregator) Marshal

func (a *TimeAggregator) Marshal() []byte

Marshal returns a binary representation of TimeAggregator

func (*TimeAggregator) MarshalJSON

func (a *TimeAggregator) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler, creates a JSON representation of the aggregator.

func (*TimeAggregator) SetBSON

func (a *TimeAggregator) SetBSON(raw bson.Raw) error

SetBSON implements bson.Setter, unmarshals a bson.Raw into a TimeAggregator

func (*TimeAggregator) String

func (a *TimeAggregator) String() string

func (*TimeAggregator) Sum

func (a *TimeAggregator) Sum(b *TimeAggregator) error

Sum sum a TimeAggregator to other, if the units are diferent UnitsMismatch error is returned

func (*TimeAggregator) Unmarshal

func (a *TimeAggregator) Unmarshal(v []byte) error

Unmarshal a binary string into a TimeAggregator, units and values are restored

type Unit

type Unit int64
const (
	Year Unit = 1 << iota
	Month
	Week
	Day
	YearDay
	Weekday
	Hour
	Minute
	Second
)

func (Unit) String

func (u Unit) String() string

Jump to

Keyboard shortcuts

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