(rw http.ResponseWriter, r *http.Request)
| 1342 | } |
| 1343 | |
| 1344 | func (api *API) chatCostSummary(rw http.ResponseWriter, r *http.Request) { |
| 1345 | ctx := r.Context() |
| 1346 | apiKey := httpmw.APIKey(r) |
| 1347 | |
| 1348 | // Default date range: last 30 days. |
| 1349 | now := time.Now() |
| 1350 | defaultStart := now.AddDate(0, 0, -30) |
| 1351 | |
| 1352 | qp := r.URL.Query() |
| 1353 | p := httpapi.NewQueryParamParser() |
| 1354 | startDate := p.Time(qp, defaultStart, "start_date", time.RFC3339) |
| 1355 | endDate := p.Time(qp, now, "end_date", time.RFC3339) |
| 1356 | p.ErrorExcessParams(qp) |
| 1357 | if len(p.Errors) > 0 { |
| 1358 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1359 | Message: "Invalid query parameters.", |
| 1360 | Validations: p.Errors, |
| 1361 | }) |
| 1362 | return |
| 1363 | } |
| 1364 | |
| 1365 | targetUser := httpmw.UserParam(r) |
| 1366 | if targetUser.ID != apiKey.UserID && !api.Authorize(r, policy.ActionRead, rbac.ResourceChat.WithOwner(targetUser.ID.String())) { |
| 1367 | httpapi.Forbidden(rw) |
| 1368 | return |
| 1369 | } |
| 1370 | |
| 1371 | summary, err := api.Database.GetChatCostSummary(ctx, database.GetChatCostSummaryParams{ |
| 1372 | OwnerID: targetUser.ID, |
| 1373 | StartDate: startDate, |
| 1374 | EndDate: endDate, |
| 1375 | }) |
| 1376 | if err != nil { |
| 1377 | if dbauthz.IsNotAuthorizedError(err) { |
| 1378 | httpapi.Forbidden(rw) |
| 1379 | return |
| 1380 | } |
| 1381 | httpapi.InternalServerError(rw, err) |
| 1382 | return |
| 1383 | } |
| 1384 | |
| 1385 | byModel, err := api.Database.GetChatCostPerModel(ctx, database.GetChatCostPerModelParams{ |
| 1386 | OwnerID: targetUser.ID, |
| 1387 | StartDate: startDate, |
| 1388 | EndDate: endDate, |
| 1389 | }) |
| 1390 | if err != nil { |
| 1391 | if dbauthz.IsNotAuthorizedError(err) { |
| 1392 | httpapi.Forbidden(rw) |
| 1393 | return |
| 1394 | } |
| 1395 | httpapi.InternalServerError(rw, err) |
| 1396 | return |
| 1397 | } |
| 1398 | |
| 1399 | byChat, err := api.Database.GetChatCostPerChat(ctx, database.GetChatCostPerChatParams{ |
| 1400 | OwnerID: targetUser.ID, |
| 1401 | StartDate: startDate, |
nothing calls this directly
no test coverage detected