godruid/post_aggregations.go

96 строки
2.5 KiB
Go
Исходник Постоянная ссылка Обычный вид История

2014-10-10 06:21:36 +04:00
package godruid
import (
2015-04-17 09:56:39 +03:00
"encoding/json"
)
2014-10-10 06:21:36 +04:00
type PostAggregation struct {
2015-04-17 09:56:39 +03:00
Type string `json:"type"`
Name string `json:"name,omitempty"`
Value interface{} `json:"value,omitempty"`
Fn string `json:"fn,omitempty"`
Fields []PostAggregation `json:"fields,omitempty"`
FieldName string `json:"fieldName,omitempty"`
FieldNames []string `json:"fieldNames,omitempty"`
Function string `json:"function,omitempty"`
2014-10-10 06:21:36 +04:00
}
// The agg reference.
type AggRefer struct {
2015-04-17 09:56:39 +03:00
Name string
Refer string // The refer of Name, empty means Name has no refer.
}
// Return the aggregations or post aggregations which this post aggregation used.
// It could be helpful while automatically filling the aggregations or post aggregations base on this.
func (pa PostAggregation) GetReferAggs(parentName ...string) (refers []AggRefer) {
2015-04-17 09:56:39 +03:00
switch pa.Type {
case "arithmetic":
if len(parentName) != 0 {
refers = append(refers, AggRefer{parentName[0], pa.Name})
} else {
refers = append(refers, AggRefer{pa.Name, ""})
}
for _, spa := range pa.Fields {
refers = append(refers, spa.GetReferAggs(pa.Name)...)
}
case "fieldAccess":
refers = append(refers, AggRefer{parentName[0], pa.FieldName})
case "constant":
// no need refers.
case "javascript":
for _, f := range pa.FieldNames {
refers = append(refers, AggRefer{pa.Name, f})
}
case "hyperUniqueCardinality":
refers = append(refers, AggRefer{parentName[0], pa.FieldName})
}
return
}
func PostAggRawJson(rawJson string) PostAggregation {
2015-04-17 09:56:39 +03:00
pa := &PostAggregation{}
json.Unmarshal([]byte(rawJson), pa)
return *pa
2014-10-10 06:21:36 +04:00
}
func PostAggArithmetic(name, fn string, fields []PostAggregation) PostAggregation {
2015-04-17 09:56:39 +03:00
return PostAggregation{
Type: "arithmetic",
Name: name,
Fn: fn,
Fields: fields,
}
2014-10-10 06:21:36 +04:00
}
func PostAggFieldAccessor(fieldName string) PostAggregation {
2015-04-17 09:56:39 +03:00
return PostAggregation{
Type: "fieldAccess",
FieldName: fieldName,
}
2014-10-10 06:21:36 +04:00
}
func PostAggConstant(name string, value interface{}) PostAggregation {
2015-04-17 09:56:39 +03:00
return PostAggregation{
Type: "constant",
Name: name,
Value: value,
}
2014-10-10 06:21:36 +04:00
}
func PostAggJavaScript(name, function string, fieldNames []string) PostAggregation {
2015-04-17 09:56:39 +03:00
return PostAggregation{
Type: "javascript",
Name: name,
FieldNames: fieldNames,
Function: function,
}
2014-10-10 06:21:36 +04:00
}
func PostAggFieldHyperUnique(fieldName string) PostAggregation {
2015-04-17 09:56:39 +03:00
return PostAggregation{
Type: "hyperUniqueCardinality",
FieldName: fieldName,
}
2014-10-10 06:21:36 +04:00
}