started stats on fields.

This commit is contained in:
didier.amyot committed 2014-05-20 09:13:24 -04:00
1 parent 2add8284e6
commit 37209f6779
11 files changed
+122 -11

No files matched your search

+41
View File
@@ -131,6 +131,33 @@ func (this *VultureBackend) wrapDocumentWithMetadata(doc interface{}, query *mgo
return ret, nil
}
func (this *VultureBackend) GetStat(key string) (interface{}, error) {
s := NewStatAggregator()
query := bson.M{key: bson.M{"$exists": true}}
iter := this.Collection.Find(query).Select(bson.M{key: 1}).Iter()
result := make(map[string]interface{})
in := make(chan float64)
out := make(chan *Stats)
go s.AddStats(in, out)
for iter.Next(&result) {
if val, ok := result[key]; ok {
if fval, isFloat := val.(float64); isFloat {
in <- fval
} else if ival, isInt := val.(int); isInt {
in <- float64(ival)
}
}
}
close(in)
if err := iter.Close(); err != nil {
return nil, err
}
return <-out, nil
}
func (this *VultureBackend) GetAllDocuments(query interface{}) (interface{}, error) {
var result []map[string]interface{}
@@ -241,3 +268,17 @@ func getDocumentByQueryAndIndex(w http.ResponseWriter, request *http.Request) (i
}
return vb.GetDocumentByIndex(index, query)
}
func getStats(w http.ResponseWriter, request *http.Request) (interface{}, error) {
vb, err := GetVultureBackend(request)
if err != nil {
return nil, err
}
vars := mux.Vars(request)
key, ok := vars["key"]
if !ok {
return nil, errors.New("key field not specified")
}
return vb.GetStat(key)
}