mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2025-12-06 15:22:47 +01:00
Add Dashboards (#41)
This commit is contained in:
@@ -2,7 +2,9 @@ package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/SecurityBrewery/catalyst/caql"
|
||||
"github.com/SecurityBrewery/catalyst/database/busdb"
|
||||
"github.com/SecurityBrewery/catalyst/generated/model"
|
||||
)
|
||||
@@ -41,3 +43,49 @@ func (db *Database) Statistics(ctx context.Context) (*model.Statistics, error) {
|
||||
|
||||
return &statistics, nil
|
||||
}
|
||||
|
||||
func (db *Database) WidgetData(ctx context.Context, aggregation string, filter *string) (map[string]interface{}, error) {
|
||||
parser := &caql.Parser{Searcher: db.Index, Prefix: "d."}
|
||||
|
||||
queryTree, err := parser.Parse(aggregation)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid aggregation query (%s): syntax error\n", aggregation)
|
||||
}
|
||||
aggregationString, err := queryTree.String()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid widget aggregation query (%s): %w", aggregation, err)
|
||||
}
|
||||
aggregation = aggregationString
|
||||
|
||||
filterQ := ""
|
||||
if filter != nil && *filter != "" {
|
||||
queryTree, err := parser.Parse(*filter)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid filter query (%s): syntax error\n", *filter)
|
||||
}
|
||||
filterString, err := queryTree.String()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid widget filter query (%s): %w", *filter, err)
|
||||
}
|
||||
|
||||
filterQ = "FILTER " + filterString
|
||||
}
|
||||
|
||||
query := `RETURN MERGE(FOR d in tickets
|
||||
` + filterQ + `
|
||||
COLLECT field = ` + aggregation + ` WITH COUNT INTO count
|
||||
RETURN ZIP([field], [count]))`
|
||||
|
||||
cur, _, err := db.Query(ctx, query, nil, busdb.ReadOperation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cur.Close()
|
||||
|
||||
statistics := map[string]interface{}{}
|
||||
if _, err := cur.ReadDocument(ctx, &statistics); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return statistics, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user