{"record":{"id":"06bf65afc154b76f","repo":"Wei-Shaw/sub2api","slug":"failed-to-get-model-statistics","errorCode":null,"errorMessage":"failed to get model statistics","messagePattern":"failed to get model statistics","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"backend/internal/handler/admin/dashboard_snapshot_v2_handler.go","lineNumber":214,"sourceCode":"\t}\n\n\tif includeModels {\n\t\tmodels, _, err := h.getModelStatsCached(\n\t\t\tctx,\n\t\t\tstartTime,\n\t\t\tendTime,\n\t\t\tfilters.UserID,\n\t\t\tfilters.APIKeyID,\n\t\t\tfilters.AccountID,\n\t\t\tfilters.GroupID,\n\t\t\tusagestats.ModelSourceRequested,\n\t\t\tfilters.RequestType,\n\t\t\tfilters.Stream,\n\t\t\tfilters.BillingType,\n\t\t\tfilters.UpstreamModelMismatch,\n\t\t)\n\t\tif err != nil {\n\t\t\treturn nil, errors.New(\"failed to get model statistics\")\n\t\t}\n\t\tresp.Models = models\n\t}\n\n\tif includeGroups {\n\t\tgroups, _, err := h.getGroupStatsCached(\n\t\t\tctx,\n\t\t\tstartTime,\n\t\t\tendTime,\n\t\t\tfilters.UserID,\n\t\t\tfilters.APIKeyID,\n\t\t\tfilters.AccountID,\n\t\t\tfilters.GroupID,\n\t\t\tfilters.RequestType,\n\t\t\tfilters.Stream,\n\t\t\tfilters.BillingType,\n\t\t\tfilters.UpstreamModelMismatch,\n\t\t)","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/Wei-Shaw/sub2api/blob/073e92d17178a1ccdb0a27017f572f10c9c7ab62/backend/internal/handler/admin/dashboard_snapshot_v2_handler.go#L196-L232","documentation":"Returned while assembling the v2 dashboard snapshot when includeModels is true and h.getModelStatsCached(...) fails. The model statistics query uses usagestats.ModelSourceRequested plus the snapshot filters; the original error is masked by this generic message. Infrastructure-side failure.","triggerScenarios":"GET dashboard snapshot v2 with the models section included while the model-stats aggregation over the requested window fails (DB error/timeout).","commonSituations":"Deployments with many distinct model names making the model-stats GROUP BY expensive; database contention from concurrent snapshot builds; missing usage-stats indexes after migration.","solutions":["Retry the request — transient DB failures are the most common cause","Shorten the date range or reduce filter cardinality (add model/user filters)","Inspect backend logs for the underlying model-stats error","Exclude the models section from the snapshot while investigating"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"// TS\ntry { models = (await getSnapshotV2({ includeModels: true, start, end })).models; }\ncatch (e) {\n  if (e.message === 'failed to get model statistics') {\n    await sleep(2000);\n    models = (await getSnapshotV2({ includeModels: true, start: startPlus30d, end })).models; // narrower window\n  } else throw e;\n}","preventionTips":["Add filters (user, key, model) to shrink model-stats aggregations","Retry transient failures with backoff","Alert on repeated model-stats failures to catch missing indexes early"],"tags":["dashboard","model-stats","database","admin"],"backgroundTag":null,"analyzedSha":"073e92d17178a1ccdb0a27017f572f10c9c7ab62","analyzedAt":"2026-08-15T14:33:00.750Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}