satellite/console/server: fix usage rollup report (#3047)

This commit is contained in:
Yaroslav Vorobiov 2019-09-13 18:38:29 +03:00 committed by GitHub
parent 8b54e329f3
commit 9d1e19cd92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -190,68 +190,61 @@ func (server *Server) bucketUsageReportHandler(w http.ResponseWriter, r *http.Re
var err error var err error
defer mon.Task()(&ctx)(&err) defer mon.Task()(&ctx)(&err)
var projectID *uuid.UUID host, _, err := net.SplitHostPort(r.Host)
var since, before time.Time
tokenCookie, err := r.Cookie("tokenKey")
if err != nil { if err != nil {
server.log.Error("bucket usage report error", zap.Error(err)) server.log.Error("bucket usage report error", zap.Error(err))
server.serveError(w, r, http.StatusInternalServerError)
return
}
// TODO: use http.StatusUnauthorized status when appropriate page will be created tokenCookie, err := r.Cookie(host + "_tokenKey")
server.serveError(w, r, http.StatusNotFound) if err != nil {
server.serveError(w, r, http.StatusUnauthorized)
return return
} }
auth, err := server.service.Authorize(auth.WithAPIKey(ctx, []byte(tokenCookie.Value))) auth, err := server.service.Authorize(auth.WithAPIKey(ctx, []byte(tokenCookie.Value)))
if err != nil { if err != nil {
server.log.Error("bucket usage report error", zap.Error(err)) server.serveError(w, r, http.StatusUnauthorized)
//TODO: when new error pages will be created - change http.StatusNotFound on http.StatusUnauthorized
server.serveError(w, r, http.StatusNotFound)
return return
} }
defer func() { ctx = console.WithAuth(ctx, auth)
if err != nil {
server.log.Error("bucket usage report error", zap.Error(err))
server.serveError(w, r, http.StatusNotFound)
return
}
}()
// parse query params // parse query params
projectID, err = uuid.Parse(r.URL.Query().Get("projectID")) projectID, err := uuid.Parse(r.URL.Query().Get("projectID"))
if err != nil { if err != nil {
server.serveError(w, r, http.StatusBadRequest)
return return
} }
sinceStamp, err := strconv.ParseInt(r.URL.Query().Get("since"), 10, 64) sinceStamp, err := strconv.ParseInt(r.URL.Query().Get("since"), 10, 64)
if err != nil { if err != nil {
server.serveError(w, r, http.StatusBadRequest)
return return
} }
beforeStamp, err := strconv.ParseInt(r.URL.Query().Get("before"), 10, 64) beforeStamp, err := strconv.ParseInt(r.URL.Query().Get("before"), 10, 64)
if err != nil { if err != nil {
server.serveError(w, r, http.StatusBadRequest)
return return
} }
since = time.Unix(sinceStamp, 0) since := time.Unix(sinceStamp, 0)
before = time.Unix(beforeStamp, 0) before := time.Unix(beforeStamp, 0)
server.log.Debug("querying bucket usage report", server.log.Debug("querying bucket usage report",
zap.Stringer("projectID", projectID), zap.Stringer("projectID", projectID),
zap.Stringer("since", since), zap.Stringer("since", since),
zap.Stringer("before", before)) zap.Stringer("before", before))
ctx = console.WithAuth(ctx, auth)
bucketRollups, err := server.service.GetBucketUsageRollups(ctx, *projectID, since, before) bucketRollups, err := server.service.GetBucketUsageRollups(ctx, *projectID, since, before)
if err != nil { if err != nil {
server.log.Error("bucket usage report error", zap.Error(err))
server.serveError(w, r, http.StatusInternalServerError)
return return
} }
if err = server.templates.usageReport.Execute(w, bucketRollups); err != nil { if err = server.templates.usageReport.Execute(w, bucketRollups); err != nil {
server.log.Error("satellite/console/server: usage report template could not be executed", zap.Error(err)) server.log.Error("bucket usage report error", zap.Error(err))
server.serveError(w, r, http.StatusNotFound)
return
} }
} }