{"record":{"id":"fc59b240682c2786","repo":"crowdsecurity/crowdsec","slug":"count-all-decisions-with-filters-w","errorCode":null,"errorMessage":"count all decisions with filters: %w","messagePattern":"count all decisions with filters: %w","errorType":"exception","errorClass":"QueryFail","httpStatus":null,"severity":"error","filePath":"pkg/database/decisions.go","lineNumber":111,"sourceCode":"\n\tdata, err := query.All(ctx)\n\tif err != nil {\n\t\tc.Log.Warningf(\"QueryExpiredDecisionsWithFilters : %s\", err)\n\t\treturn []*ent.Decision{}, fmt.Errorf(\"expired decisions: %w\", QueryFail)\n\t}\n\n\treturn data, nil\n}\n\nfunc (c *Client) QueryDecisionCountByScenario(ctx context.Context) ([]*DecisionsByScenario, error) {\n\tquery := c.Ent.Decision.Query().Where(\n\t\tdecision.UntilGT(time.Now().UTC()),\n\t)\n\n\tquery, err := applyDecisionFilter(query, make(map[string][]string))\n\tif err != nil {\n\t\tc.Log.Warningf(\"QueryDecisionCountByScenario : %s\", err)\n\t\treturn nil, fmt.Errorf(\"count all decisions with filters: %w\", QueryFail)\n\t}\n\n\tvar r []*DecisionsByScenario\n\n\terr = query.GroupBy(decision.FieldScenario, decision.FieldOrigin, decision.FieldType).Aggregate(ent.Count()).Scan(ctx, &r)\n\tif err != nil {\n\t\tc.Log.Warningf(\"QueryDecisionCountByScenario : %s\", err)\n\t\treturn nil, fmt.Errorf(\"count all decisions with filters: %w\", QueryFail)\n\t}\n\n\treturn r, nil\n}\n\nfunc (c *Client) QueryDecisionWithFilter(ctx context.Context, filter map[string][]string) ([]*ent.Decision, error) {\n\tvar (\n\t\terr  error\n\t\tdata []*ent.Decision\n\t)","sourceCodeStart":93,"sourceCodeEnd":129,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/database/decisions.go#L93-L129","documentation":"Returned by QueryDecisionCountByScenario when applyDecisionFilter fails on the (effectively empty) filter map. Wraps QueryFail (\"unable to query\"); the underlying error is logged as 'QueryDecisionCountByScenario : <err>'.","triggerScenarios":"Rare: the built-in empty filter map cannot be translated, typically indicating an internal invariant problem or a corrupted/absent ent schema at runtime rather than caller input.","commonSituations":"Seen during dashboard/metrics aggregation (cscli metrics / console enrollment) when the DB layer is in a broken state — e.g. partially applied migration leaving the decisions table invalid.","solutions":["Check logs for the 'QueryDecisionCountByScenario :' warning to see the real error","Re-run migrations / restart crowdsec to ensure the decisions table schema is complete","If migrations fail, back up and inspect the decisions table for leftover foreign keys/columns","Report as a bug with the log line if it reproduces with an empty filter"],"exampleFix":"// before\n# ignore and keep querying with broken schema\ncscli metrics\n// after\nsystemctl stop crowdsec && cp crowdsec.db crowdsec.db.bak\ncscli db migrate && systemctl start crowdsec","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"counts, err := client.QueryDecisionCountByScenario(ctx)\nif err != nil {\n    log.Warnf(\"decision counts unavailable: %v\", err)\n    counts = []*database.DecisionsByScenario{} // degrade metrics, don't crash\n}","preventionTips":["Don't manually alter the decisions table schema","Verify schema integrity after restores from backup","Report reproducible failures with the underlying log line to CrowdSec"],"tags":["database","crowdsec","metrics","query"],"backgroundTag":"database-query-failed","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}