Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make stddev work #2354

Merged
merged 5 commits into from
Apr 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions cmd/influxd/server_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,14 +581,41 @@ func runTestsData(t *testing.T, testName string, nodes Cluster, database, retent
// Aggregations
{
reset: true,
name: "large mean",
name: "stddev with just one point",
write: `{"database" : "%DB%", "retentionPolicy" : "%RP%", "points": [
{"name": "cpu", "timestamp": "2015-04-20T14:27:41Z", "fields": {"value": 45}}
]}`,
query: `SELECT stddev(value) FROM cpu`,
queryDb: "%DB%",
expected: `{"results":[{"series":[{"name":"cpu","columns":["time","stddev"],"values":[["1970-01-01T00:00:00Z",null]]}]}]}`,
},
{
reset: true,
name: "large mean and stddev",
write: `{"database" : "%DB%", "retentionPolicy" : "%RP%", "points": [
{"name": "cpu", "timestamp": "2015-04-20T14:27:40Z", "fields": {"value": ` + string(maxFloat64) + `}},
{"name": "cpu", "timestamp": "2015-04-20T14:27:41Z", "fields": {"value": ` + string(maxFloat64) + `}}
]}`,
query: `SELECT mean(value) FROM cpu`,
query: `SELECT mean(value), stddev(value) FROM cpu`,
queryDb: "%DB%",
expected: `{"results":[{"series":[{"name":"cpu","columns":["time","mean","stddev"],"values":[["1970-01-01T00:00:00Z",` + string(maxFloat64) + `,0]]}]}]}`,
},
{
reset: true,
name: "mean and stddev",
write: `{"database" : "%DB%", "retentionPolicy" : "%RP%", "points": [
{"name": "cpu", "timestamp": "2000-01-01T00:00:00Z", "fields": {"value": 2}},
{"name": "cpu", "timestamp": "2000-01-01T00:00:10Z", "fields": {"value": 4}},
{"name": "cpu", "timestamp": "2000-01-01T00:00:20Z", "fields": {"value": 4}},
{"name": "cpu", "timestamp": "2000-01-01T00:00:30Z", "fields": {"value": 4}},
{"name": "cpu", "timestamp": "2000-01-01T00:00:40Z", "fields": {"value": 5}},
{"name": "cpu", "timestamp": "2000-01-01T00:00:50Z", "fields": {"value": 5}},
{"name": "cpu", "timestamp": "2000-01-01T00:01:00Z", "fields": {"value": 7}},
{"name": "cpu", "timestamp": "2000-01-01T00:01:10Z", "fields": {"value": 9}}
]}`,
query: `SELECT mean(value), stddev(value) FROM cpu WHERE time >= '2000-01-01' AND time < '2000-01-01T00:02:00Z' GROUP BY time(10m)`,
queryDb: "%DB%",
expected: `{"results":[{"series":[{"name":"cpu","columns":["time","mean"],"values":[["1970-01-01T00:00:00Z",` + string(maxFloat64) + `]]}]}]}`,
expected: `{"results":[{"series":[{"name":"cpu","columns":["time","mean","stddev"],"values":[["2000-01-01T00:00:00Z",5,2.138089935299395]]}]}]}`,
},
{
reset: true,
Expand Down
20 changes: 13 additions & 7 deletions influxql/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ func InitializeUnmarshaller(c *Call) (UnmarshalFunc, error) {
err := json.Unmarshal(b, &o)
return &o, err
}, nil
case "stddev":
return func(b []byte) (interface{}, error) {
val := make([]float64, 0)
err := json.Unmarshal(b, &val)
return val, err
}, nil
default:
return func(b []byte) (interface{}, error) {
var val interface{}
Expand Down Expand Up @@ -386,7 +392,7 @@ func MapStddev(itr Iterator) interface{} {
values = append(values, v.(float64))
}

return nil
return values
}

// ReduceStddev computes the stddev of values.
Expand All @@ -405,21 +411,21 @@ func ReduceStddev(values []interface{}) interface{} {
return nil
}

// Get the sum
var sum float64
// Get the mean
var mean float64
var count int
for _, v := range data {
sum += v
count++
mean += (v - mean) / float64(count)
}
// Get the mean
mean := sum / float64(len(data))
// Get the variance
var variance float64
for _, v := range data {
dif := v - mean
sq := math.Pow(dif, 2)
variance += sq
}
variance = variance / float64(len(data)-1)
variance = variance / float64(count-1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should check for count == 1 or this'll be bad

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look at line 410

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it probably wouldn't be a bad idea to add a test for count < 2 though. i can work on that if you want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, no, that's fine, didn't see it.

stddev := math.Sqrt(variance)

return stddev
Expand Down