Add unit tests for dashboard repository and enhance BSON handling
continuous-integration/drone/push Build is passing

This commit is contained in:
Mazyar
2026-06-21 10:03:17 +03:30
parent 45cfa24a72
commit e31bccced6
5 changed files with 114 additions and 28 deletions
+39 -11
View File
@@ -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)