storj/satellite/console/consoleweb/consoleapi/abtesting.go
Wilfred Asomani 4ee647a951 satellite: add request id to requests
This change adds request IDs to requests, logs them as part of audit
logs and sends to the client on error. This is to improve debugging
of customer issues.

Issue: https://github.com/storj/storj/issues/5898

Change-Id: I801514b547d28d810552d91aa7c8502051e552bf
2023-07-13 09:22:43 +00:00

90 lines
2.2 KiB
Go

// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package consoleapi
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/storj/private/web"
"storj.io/storj/satellite/abtesting"
"storj.io/storj/satellite/console"
)
// ErrABAPI - console ab testing api error type.
var ErrABAPI = errs.Class("consoleapi ab testing error")
// ABTesting is an api controller that exposes all ab testing functionality.
type ABTesting struct {
log *zap.Logger
service abtesting.ABService
}
// NewABTesting is a constructor for AB testing controller.
func NewABTesting(log *zap.Logger, service abtesting.ABService) *ABTesting {
return &ABTesting{
log: log,
service: service,
}
}
// GetABValues gets AB test values for a specific user.
func (a *ABTesting) GetABValues(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
defer mon.Task()(&ctx)(&err)
user, err := console.GetUser(ctx)
if err != nil {
web.ServeJSONError(ctx, a.log, w, http.StatusUnauthorized, err)
return
}
values, err := a.service.GetABValues(ctx, *user)
if err != nil {
web.ServeJSONError(ctx, a.log, w, http.StatusInternalServerError, err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(values)
if err != nil {
a.log.Error("Could not encode AB values", zap.Error(ErrABAPI.Wrap(err)))
}
}
// SendHit sends an event to flagship.
func (a *ABTesting) SendHit(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var err error
defer mon.Task()(&ctx)(&err)
action := mux.Vars(r)["action"]
if action == "" {
web.ServeJSONError(ctx, a.log, w, http.StatusBadRequest, errs.New("parameter 'action' can't be empty"))
return
}
user, err := console.GetUser(ctx)
if err != nil {
web.ServeJSONError(ctx, a.log, w, http.StatusUnauthorized, err)
return
}
a.service.SendHit(ctx, *user, action)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(map[string]string{
"message": "Upgrade hit acknowledged",
})
if err != nil {
a.log.Error("failed to write json error response", zap.Error(ErrABAPI.Wrap(err)))
}
}