Add unit tests for dashboard repository and enhance BSON handling
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -149,24 +149,20 @@ func (r *repository) Summary(ctx context.Context, closingWindowSec int64) (*Summ
|
||||
}
|
||||
|
||||
func facetCount(facet bson.M, key string) int64 {
|
||||
rows, ok := facet[key].(bson.A)
|
||||
if !ok || len(rows) == 0 {
|
||||
rows := facetRows(facet, key)
|
||||
if len(rows) == 0 {
|
||||
return 0
|
||||
}
|
||||
row, ok := rows[0].(bson.M)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
return aggregateCount(row, "count")
|
||||
return aggregateCount(asBSONMap(rows[0]), "count")
|
||||
}
|
||||
|
||||
func facetCurrencyValue(facet bson.M) (string, float64) {
|
||||
rows, ok := facet["currency_value"].(bson.A)
|
||||
if !ok || len(rows) == 0 {
|
||||
rows := facetRows(facet, "currency_value")
|
||||
if len(rows) == 0 {
|
||||
return defaultValueCurrency, 0
|
||||
}
|
||||
row, ok := rows[0].(bson.M)
|
||||
if !ok {
|
||||
row := asBSONMap(rows[0])
|
||||
if row == nil {
|
||||
return defaultValueCurrency, 0
|
||||
}
|
||||
|
||||
@@ -177,6 +173,38 @@ func facetCurrencyValue(facet bson.M) (string, float64) {
|
||||
return currency, toFloat64(row["total"])
|
||||
}
|
||||
|
||||
func facetRows(facet bson.M, key string) []interface{} {
|
||||
raw, ok := facet[key]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
switch rows := raw.(type) {
|
||||
case bson.A:
|
||||
return []interface{}(rows)
|
||||
case []interface{}:
|
||||
return rows
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func asBSONMap(v interface{}) bson.M {
|
||||
switch doc := v.(type) {
|
||||
case bson.M:
|
||||
return doc
|
||||
case bson.D:
|
||||
out := make(bson.M, len(doc))
|
||||
for _, elem := range doc {
|
||||
out[elem.Key] = elem.Value
|
||||
}
|
||||
return out
|
||||
case map[string]interface{}:
|
||||
return doc
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r *repository) Trend(ctx context.Context, days int, metric string, startUnix int64) (map[string]int64, error) {
|
||||
field := trendTimestampField(metric)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user