diff --git a/satellite/console/consoleweb/consoleapi/api.gen.go b/satellite/console/consoleweb/consoleapi/api.gen.go index c5431dc4f..934132cba 100644 --- a/satellite/console/consoleweb/consoleapi/api.gen.go +++ b/satellite/console/consoleweb/consoleapi/api.gen.go @@ -25,6 +25,7 @@ var ErrProjectsAPI = errs.Class("consoleapi projects api") type ProjectManagementService interface { GenGetUsersProjects(context.Context) ([]console.Project, api.HTTPError) GenGetSingleBucketUsageRollup(context.Context, uuid.UUID, string, time.Time, time.Time) (*accounting.BucketUsageRollup, api.HTTPError) + GenGetBucketUsageRollups(context.Context, uuid.UUID, time.Time, time.Time) ([]accounting.BucketUsageRollup, api.HTTPError) } // Handler is an api handler that exposes all projects related functionality. @@ -44,35 +45,11 @@ func NewProjectManagement(log *zap.Logger, service ProjectManagementService, rou projectsRouter := router.PathPrefix("/api/v0/projects").Subrouter() projectsRouter.HandleFunc("/", handler.handleGenGetUsersProjects).Methods("GET") projectsRouter.HandleFunc("/bucket-rollup", handler.handleGenGetSingleBucketUsageRollup).Methods("GET") + projectsRouter.HandleFunc("/bucket-rollups", handler.handleGenGetBucketUsageRollups).Methods("GET") return handler } -func (h *Handler) handleGenGetUsersProjects(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() - var err error - defer mon.Task()(&ctx)(&err) - - w.Header().Set("Content-Type", "application/json") - - ctx, err = h.auth.IsAuthenticated(ctx, r) - if err != nil { - api.ServeError(h.log, w, http.StatusUnauthorized, err) - return - } - - retVal, httpErr := h.service.GenGetUsersProjects(ctx) - if httpErr.Err != nil { - api.ServeError(h.log, w, httpErr.Status, httpErr.Err) - return - } - - err = json.NewEncoder(w).Encode(retVal) - if err != nil { - h.log.Debug("failed to write json GenGetUsersProjects response", zap.Error(ErrProjectsAPI.Wrap(err))) - } -} - func (h *Handler) handleGenGetSingleBucketUsageRollup(w http.ResponseWriter, r *http.Request) { ctx := r.Context() var err error @@ -125,3 +102,75 @@ func (h *Handler) handleGenGetSingleBucketUsageRollup(w http.ResponseWriter, r * h.log.Debug("failed to write json GenGetSingleBucketUsageRollup response", zap.Error(ErrProjectsAPI.Wrap(err))) } } + +func (h *Handler) handleGenGetBucketUsageRollups(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + var err error + defer mon.Task()(&ctx)(&err) + + w.Header().Set("Content-Type", "application/json") + + ctx, err = h.auth.IsAuthenticated(ctx, r) + if err != nil { + api.ServeError(h.log, w, http.StatusUnauthorized, err) + return + } + + projectID, err := uuid.FromString(r.URL.Query().Get("projectID")) + if err != nil { + api.ServeError(h.log, w, http.StatusBadRequest, err) + return + } + + sinceStamp, err := strconv.ParseInt(r.URL.Query().Get("since"), 10, 64) + if err != nil { + api.ServeError(h.log, w, http.StatusBadRequest, err) + return + } + + since := time.Unix(sinceStamp, 0).UTC() + + beforeStamp, err := strconv.ParseInt(r.URL.Query().Get("before"), 10, 64) + if err != nil { + api.ServeError(h.log, w, http.StatusBadRequest, err) + return + } + + before := time.Unix(beforeStamp, 0).UTC() + + retVal, httpErr := h.service.GenGetBucketUsageRollups(ctx, projectID, since, before) + if httpErr.Err != nil { + api.ServeError(h.log, w, httpErr.Status, httpErr.Err) + return + } + + err = json.NewEncoder(w).Encode(retVal) + if err != nil { + h.log.Debug("failed to write json GenGetBucketUsageRollups response", zap.Error(ErrProjectsAPI.Wrap(err))) + } +} + +func (h *Handler) handleGenGetUsersProjects(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + var err error + defer mon.Task()(&ctx)(&err) + + w.Header().Set("Content-Type", "application/json") + + ctx, err = h.auth.IsAuthenticated(ctx, r) + if err != nil { + api.ServeError(h.log, w, http.StatusUnauthorized, err) + return + } + + retVal, httpErr := h.service.GenGetUsersProjects(ctx) + if httpErr.Err != nil { + api.ServeError(h.log, w, httpErr.Status, httpErr.Err) + return + } + + err = json.NewEncoder(w).Encode(retVal) + if err != nil { + h.log.Debug("failed to write json GenGetUsersProjects response", zap.Error(ErrProjectsAPI.Wrap(err))) + } +} diff --git a/satellite/console/consoleweb/consoleapi/gen/main.go b/satellite/console/consoleweb/consoleapi/gen/main.go index 975db2799..4d6950208 100644 --- a/satellite/console/consoleweb/consoleapi/gen/main.go +++ b/satellite/console/consoleweb/consoleapi/gen/main.go @@ -32,8 +32,8 @@ func main() { }) g.Get("/bucket-rollup", &apigen.Endpoint{ - Name: "Get Project's Bucket Usage", - Description: "Gets project's bucket usage by bucket ID", + Name: "Get Project's Single Bucket Usage", + Description: "Gets project's single bucket usage by bucket ID", MethodName: "GenGetSingleBucketUsageRollup", Response: &accounting.BucketUsageRollup{}, Params: []apigen.Param{ @@ -43,6 +43,18 @@ func main() { apigen.NewParam("before", time.Time{}), }, }) + + g.Get("/bucket-rollups", &apigen.Endpoint{ + Name: "Get Project's All Buckets Usage", + Description: "Gets project's all buckets usage", + MethodName: "GenGetBucketUsageRollups", + Response: []accounting.BucketUsageRollup{}, + Params: []apigen.Param{ + apigen.NewParam("projectID", uuid.UUID{}), + apigen.NewParam("since", time.Time{}), + apigen.NewParam("before", time.Time{}), + }, + }) } a.MustWrite("satellite/console/consoleweb/consoleapi/api.gen.go") diff --git a/satellite/console/service.go b/satellite/console/service.go index fcc1c2322..fd883674d 100644 --- a/satellite/console/service.go +++ b/satellite/console/service.go @@ -1735,6 +1735,38 @@ func (s *Service) GetBucketUsageRollups(ctx context.Context, projectID uuid.UUID return result, nil } +// GenGetBucketUsageRollups retrieves summed usage rollups for every bucket of particular project for a given period for generated api. +func (s *Service) GenGetBucketUsageRollups(ctx context.Context, projectID uuid.UUID, since, before time.Time) (rollups []accounting.BucketUsageRollup, httpError api.HTTPError) { + var err error + defer mon.Task()(&ctx)(&err) + + auth, err := s.getAuthAndAuditLog(ctx, "get bucket usage rollups", zap.String("projectID", projectID.String())) + if err != nil { + return nil, api.HTTPError{ + Status: http.StatusUnauthorized, + Err: Error.Wrap(err), + } + } + + _, err = s.isProjectMember(ctx, auth.User.ID, projectID) + if err != nil { + return nil, api.HTTPError{ + Status: http.StatusUnauthorized, + Err: Error.Wrap(err), + } + } + + rollups, err = s.projectAccounting.GetBucketUsageRollups(ctx, projectID, since, before) + if err != nil { + return nil, api.HTTPError{ + Status: http.StatusInternalServerError, + Err: Error.Wrap(err), + } + } + + return +} + // GenGetSingleBucketUsageRollup retrieves usage rollup for single bucket of particular project for a given period for generated api. func (s *Service) GenGetSingleBucketUsageRollup(ctx context.Context, projectID uuid.UUID, bucket string, since, before time.Time) (rollup *accounting.BucketUsageRollup, httpError api.HTTPError) { var err error