2021-06-24 16:49:15 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package consoleapi
|
|
|
|
|
|
|
|
import (
|
2023-06-28 14:06:32 +01:00
|
|
|
"context"
|
2023-10-05 15:24:58 +01:00
|
|
|
"encoding/csv"
|
2021-06-24 16:49:15 +01:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2021-12-07 14:41:39 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2021-06-24 16:49:15 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/uuid"
|
2022-11-21 18:58:42 +00:00
|
|
|
"storj.io/storj/private/web"
|
2021-06-24 16:49:15 +01:00
|
|
|
"storj.io/storj/satellite/accounting"
|
|
|
|
"storj.io/storj/satellite/console"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrUsageLimitsAPI - console usage and limits api error type.
|
|
|
|
ErrUsageLimitsAPI = errs.Class("console usage and limits")
|
|
|
|
)
|
|
|
|
|
|
|
|
// UsageLimits is an api controller that exposes all usage and limits related functionality.
|
|
|
|
type UsageLimits struct {
|
|
|
|
log *zap.Logger
|
|
|
|
service *console.Service
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUsageLimits is a constructor for api usage and limits controller.
|
|
|
|
func NewUsageLimits(log *zap.Logger, service *console.Service) *UsageLimits {
|
|
|
|
return &UsageLimits{
|
|
|
|
log: log,
|
|
|
|
service: service,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectUsageLimits returns usage and limits by project ID.
|
|
|
|
func (ul *UsageLimits) ProjectUsageLimits(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
var idParam string
|
|
|
|
|
|
|
|
if idParam, ok = mux.Vars(r)["id"]; !ok {
|
2023-06-28 14:06:32 +01:00
|
|
|
ul.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("missing project id route param"))
|
2021-06-24 16:49:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
projectID, err := uuid.FromString(idParam)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
ul.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("invalid project id: %v", err))
|
2021-06-24 16:49:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
usageLimits, err := ul.service.GetProjectUsageLimits(ctx, projectID)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
2023-08-21 23:50:02 +01:00
|
|
|
case console.ErrUnauthorized.Has(err) || console.ErrNoMembership.Has(err):
|
2023-06-28 14:06:32 +01:00
|
|
|
ul.serveJSONError(ctx, w, http.StatusUnauthorized, err)
|
2021-06-24 16:49:15 +01:00
|
|
|
return
|
|
|
|
case accounting.ErrInvalidArgument.Has(err):
|
2023-06-28 14:06:32 +01:00
|
|
|
ul.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
2021-06-24 16:49:15 +01:00
|
|
|
return
|
|
|
|
default:
|
2023-06-28 14:06:32 +01:00
|
|
|
ul.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
2021-06-24 16:49:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(usageLimits)
|
|
|
|
if err != nil {
|
|
|
|
ul.log.Error("error encoding project usage limits", zap.Error(ErrUsageLimitsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TotalUsageLimits returns total usage and limits for all the projects that user owns.
|
|
|
|
func (ul *UsageLimits) TotalUsageLimits(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
usageLimits, err := ul.service.GetTotalUsageLimits(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
2023-06-28 14:06:32 +01:00
|
|
|
ul.serveJSONError(ctx, w, http.StatusUnauthorized, err)
|
2021-06-24 16:49:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-28 14:06:32 +01:00
|
|
|
ul.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
2021-06-24 16:49:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(usageLimits)
|
|
|
|
if err != nil {
|
|
|
|
ul.log.Error("error encoding project usage limits", zap.Error(ErrUsageLimitsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-05 15:24:58 +01:00
|
|
|
// TotalUsageReport returns total usage report for all the projects that user owns.
|
|
|
|
func (ul *UsageLimits) TotalUsageReport(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
sinceStamp, err := strconv.ParseInt(r.URL.Query().Get("since"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
ul.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
beforeStamp, err := strconv.ParseInt(r.URL.Query().Get("before"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
ul.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
since := time.Unix(sinceStamp, 0).UTC()
|
|
|
|
before := time.Unix(beforeStamp, 0).UTC()
|
|
|
|
|
|
|
|
usage, err := ul.service.GetTotalUsageReport(ctx, since, before)
|
|
|
|
if err != nil {
|
|
|
|
if console.ErrUnauthorized.Has(err) {
|
|
|
|
ul.serveJSONError(ctx, w, http.StatusUnauthorized, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ul.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := "storj-report-" + since.Format("2006-01-02") + "-to-" + before.Format("2006-01-02") + ".csv"
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/csv")
|
|
|
|
w.Header().Set("Content-Disposition", "attachment;filename="+fileName)
|
|
|
|
|
|
|
|
wr := csv.NewWriter(w)
|
|
|
|
|
|
|
|
csvHeaders := []string{"ProjectID", "BucketName", "TotalStoredData GB-hour", "TotalSegments GB-hour", "ObjectCount GB-hour", "MetadataSize GB-hour", "RepairEgress GB", "GetEgress GB", "AuditEgress GB", "Since", "Before"}
|
|
|
|
|
|
|
|
err = wr.Write(csvHeaders)
|
|
|
|
if err != nil {
|
|
|
|
ul.serveJSONError(ctx, w, http.StatusInternalServerError, errs.New("Error writing CSV data"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, u := range usage {
|
|
|
|
err = wr.Write(u.ToStringSlice())
|
|
|
|
if err != nil {
|
|
|
|
ul.serveJSONError(ctx, w, http.StatusInternalServerError, errs.New("Error writing CSV data"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wr.Flush()
|
|
|
|
}
|
|
|
|
|
2021-12-07 14:41:39 +00:00
|
|
|
// DailyUsage returns daily usage by project ID.
|
|
|
|
func (ul *UsageLimits) DailyUsage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
var idParam string
|
|
|
|
|
|
|
|
if idParam, ok = mux.Vars(r)["id"]; !ok {
|
2023-06-28 14:06:32 +01:00
|
|
|
ul.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("missing project id route param"))
|
2021-12-07 14:41:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
projectID, err := uuid.FromString(idParam)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
ul.serveJSONError(ctx, w, http.StatusBadRequest, errs.New("invalid project id: %v", err))
|
2021-12-07 14:41:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sinceStamp, err := strconv.ParseInt(r.URL.Query().Get("from"), 10, 64)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
ul.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
2021-12-07 14:41:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
beforeStamp, err := strconv.ParseInt(r.URL.Query().Get("to"), 10, 64)
|
|
|
|
if err != nil {
|
2023-06-28 14:06:32 +01:00
|
|
|
ul.serveJSONError(ctx, w, http.StatusBadRequest, err)
|
2021-12-07 14:41:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-16 16:47:47 +01:00
|
|
|
since := time.Unix(sinceStamp, 0)
|
|
|
|
before := time.Unix(beforeStamp, 0)
|
2021-12-07 14:41:39 +00:00
|
|
|
|
|
|
|
dailyUsage, err := ul.service.GetDailyProjectUsage(ctx, projectID, since, before)
|
|
|
|
if err != nil {
|
2023-08-21 23:50:02 +01:00
|
|
|
if console.ErrUnauthorized.Has(err) || console.ErrNoMembership.Has(err) {
|
2023-06-28 14:06:32 +01:00
|
|
|
ul.serveJSONError(ctx, w, http.StatusUnauthorized, err)
|
2021-12-07 14:41:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-28 14:06:32 +01:00
|
|
|
ul.serveJSONError(ctx, w, http.StatusInternalServerError, err)
|
2021-12-07 14:41:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.NewEncoder(w).Encode(dailyUsage)
|
|
|
|
if err != nil {
|
|
|
|
ul.log.Error("error encoding daily project usage", zap.Error(ErrUsageLimitsAPI.Wrap(err)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 16:49:15 +01:00
|
|
|
// serveJSONError writes JSON error to response output stream.
|
2023-06-28 14:06:32 +01:00
|
|
|
func (ul *UsageLimits) serveJSONError(ctx context.Context, w http.ResponseWriter, status int, err error) {
|
|
|
|
web.ServeJSONError(ctx, ul.log, w, status, err)
|
2021-06-24 16:49:15 +01:00
|
|
|
}
|