Skip to content

Commit

Permalink
Basic percentile aggregator tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
toddboom committed Oct 14, 2013
1 parent bac95f1 commit 3f9b109
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/engine/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func init() {
registeredAggregators["count"] = NewCountAggregator
registeredAggregators["max"] = NewMaxAggregator
registeredAggregators["min"] = NewMinAggregator
registeredAggregators["percentile"] = NewPercentileAggregator
registeredAggregators["__timestamp_aggregator"] = NewTimestampAggregator
}

Expand Down Expand Up @@ -60,6 +61,44 @@ func NewCountAggregator(*parser.Query, *parser.Value) (Aggregator, error) {
return &CountAggregator{make(map[string]map[interface{}]int32)}, nil
}

type PercentileAggregator struct {
percentiles map[string]map[interface{}]int32
}

func (self *PercentileAggregator) AggregatePoint(series string, group interface{}, p *protocol.Point) error {
percentiles := self.percentiles[series]
if percentiles == nil {
percentiles = make(map[interface{}]int32)
self.percentiles[series] = percentiles
}
percentiles[group]++
return nil
}

func (self *PercentileAggregator) ColumnName() string {
return "percentile"
}

func (self *PercentileAggregator) ColumnType() protocol.FieldDefinition_Type {
return protocol.FieldDefinition_DOUBLE
}

func (self *PercentileAggregator) GetValue(series string, group interface{}) *protocol.FieldValue {
value := self.percentiles[series][group]
return &protocol.FieldValue{IntValue: &value}
}

func (self *PercentileAggregator) InitializeFieldsMetadata(series *protocol.Series) error { return nil }

func NewPercentileAggregator(_ *parser.Query, value *parser.Value) (Aggregator, error) {
if len(value.Elems) != 2 {
return nil, common.NewQueryError(common.WrongNumberOfArguments, "function percentile(...) requires exactly two arguments")
}

fmt.Printf("CREATING NEW PercentileAggregator")
return &PercentileAggregator{make(map[string]map[interface{}]int32)}, nil
}

type TimestampAggregator struct {
duration *time.Duration
timestamps map[string]map[interface{}]int64
Expand Down
6 changes: 6 additions & 0 deletions src/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -964,3 +964,9 @@ func (self *EngineSuite) TestCountQueryWithGroupByTimeInvalidArgument(c *C) {
engine := createEngine(c, `[]`)
runQueryRunError(engine, "select count(*) from foo group by time(foobar);", c, err)
}

func (self *EngineSuite) TestPercentileQueryWithInvalidNumberOfArguments(c *C) {
err := common.NewQueryError(common.WrongNumberOfArguments, "function percentile(...) requires exactly two arguments")
engine := createEngine(c, `[]`)
runQueryRunError(engine, "select percentile(95) from foo group by time(1m);", c, err)
}

0 comments on commit 3f9b109

Please sign in to comment.