Skip to content

Commit

Permalink
Split out the rest of the app logic from the existing handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
John Wregglesworth committed May 29, 2024
1 parent 1eaf0b1 commit e5cdb92
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 147 deletions.
41 changes: 22 additions & 19 deletions app/quotas.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,23 @@ import (
"github.com/cyverse-de/subscriptions/errors"
)

func (a *App) AddQuotaHandler(subject, reply string, request *qms.AddQuotaRequest) {
func (a *App) addQuota(ctx context.Context, request *qms.AddQuotaRequest) *qms.QuotaResponse {
var err error

log := log.WithField("context", "add quota")

sendError := func(ctx context.Context, response *qms.QuotaResponse, err error) {
log.Error(err)
response.Error = errors.NatsError(ctx, err)
if err = a.client.Respond(ctx, reply, response); err != nil {
log.Error(err)
}
}

response := pbinit.NewQuotaResponse()

ctx, span := pbinit.InitQMSAddQuotaRequest(request, subject)
defer span.End()

subscriptionID := request.Quota.SubscriptionId

d := db.New(a.db)

if err = d.UpsertQuota(ctx, float64(request.Quota.Quota), request.Quota.ResourceType.Uuid, subscriptionID); err != nil {
sendError(ctx, response, err)
return
response.Error = errors.NatsError(ctx, err)
return response
}

value, _, err := d.GetCurrentQuota(ctx, request.Quota.ResourceType.Uuid, subscriptionID)
if err != nil {
sendError(ctx, response, err)
return
response.Error = errors.NatsError(ctx, err)
return response
}

response.Quota = &qms.Quota{
Expand All @@ -48,6 +34,23 @@ func (a *App) AddQuotaHandler(subject, reply string, request *qms.AddQuotaReques
SubscriptionId: subscriptionID,
}

return response
}

func (a *App) AddQuotaHandler(subject, reply string, request *qms.AddQuotaRequest) {
var err error

log := log.WithField("context", "add quota")

ctx, span := pbinit.InitQMSAddQuotaRequest(request, subject)
defer span.End()

response := a.addQuota(ctx, request)

if response.Error != nil {
log.Error(response.Error.Message)
}

if err = a.client.Respond(ctx, reply, response); err != nil {
log.Error(err)
}
Expand Down
62 changes: 24 additions & 38 deletions app/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,67 +87,53 @@ func (a *App) GetUserSummary(ctx context.Context, username string) (*qms.Subscri
return subscription.ToQMSSubscription(), nil
}

func (a *App) GetUserSummaryHandler(subject, reply string, request *qms.RequestByUsername) {
var err error

log := log.WithFields(logrus.Fields{"context": "user summary"})

func (a *App) getUserSummary(ctx context.Context, request *qms.RequestByUsername) *qms.SubscriptionResponse {
response := pbinit.NewSubscriptionResponse()

ctx, span := pbinit.InitQMSRequestByUsername(request, subject)
defer span.End()

sendError := func(ctx context.Context, response *qms.SubscriptionResponse, err error) {
log.Error(err)
response.Error = errors.NatsError(ctx, err)
if err = a.client.Respond(ctx, reply, response); err != nil {
log.Error(err)
}
}

username, err := a.FixUsername(request.Username)
if err != nil {
sendError(ctx, response, err)
return
response.Error = errors.NatsError(ctx, err)
return response
}

log = log.WithFields(logrus.Fields{"user": username})

subscription, err := a.GetUserSummary(ctx, username)
if err != nil {
sendError(ctx, response, err)
return
response.Error = errors.NatsError(ctx, err)
return response
}

log.Warnf("Reply: %s", reply)

response.Subscription = subscription

return response
}

func (a *App) GetUserSummaryHandler(subject, reply string, request *qms.RequestByUsername) {
var err error

log := log.WithFields(logrus.Fields{"context": "user summary"})

ctx, span := pbinit.InitQMSRequestByUsername(request, subject)
defer span.End()

response := a.getUserSummary(ctx, request)

if err = a.client.Respond(ctx, reply, response); err != nil {
log.Error(err)
}
}

func (a *App) GetUserSummaryHTTPHandler(c echo.Context) error {
var err error

log := log.WithFields(logrus.Fields{"context": "user summary http"})
ctx := c.Request().Context()
response := pbinit.NewSubscriptionResponse()

username := c.Param("user")

username, err = a.FixUsername(username)
if err != nil {
return err
request := &qms.RequestByUsername{
Username: c.Param("user"),
}

log = log.WithFields(logrus.Fields{"user": username})
response := a.getUserSummary(ctx, request)

subscription, err := a.GetUserSummary(ctx, username)
if err != nil {
return err
if response.Error != nil {
return c.JSON(int(response.Error.StatusCode), response)
}

response.Subscription = subscription
return c.JSON(http.StatusOK, response)
}
118 changes: 69 additions & 49 deletions app/usages.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,27 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"
)

func (a *App) GetUsagesHandler(subject, reply string, request *qms.GetUsages) {
var err error

log := log.WithFields(logrus.Fields{"context": "getting usages"})
func (a *App) getUsages(ctx context.Context, request *qms.GetUsages) *qms.UsageList {
response := pbinit.NewUsageList()
ctx, span := pbinit.InitGetUsages(request, subject)
defer span.End()

sendError := func(ctx context.Context, response *qms.UsageList, err error) {
log.Error(err)
response.Error = errors.NatsError(ctx, err)
if err = a.client.Respond(ctx, reply, response); err != nil {
log.Error(err)
}
}

username, err := a.FixUsername(request.Username)
if err != nil {
sendError(ctx, response, err)
return
response.Error = errors.NatsError(ctx, err)
return response
}

log = log.WithFields(logrus.Fields{"user": username})

d := db.New(a.db)

subscription, err := d.GetActiveSubscription(ctx, username)
if err != nil {
sendError(ctx, response, err)
return
response.Error = errors.NatsError(ctx, err)
return response
}

usages, err := d.SubscriptionUsages(ctx, subscription.ID)
if err != nil {
sendError(ctx, response, err)
return
response.Error = errors.NatsError(ctx, err)
return response
}

for _, usage := range usages {
Expand All @@ -66,59 +51,59 @@ func (a *App) GetUsagesHandler(subject, reply string, request *qms.GetUsages) {
})
}

log.Info("successfully found usages")
return response
}

func (a *App) GetUsagesHandler(subject, reply string, request *qms.GetUsages) {
var err error

log := log.WithFields(logrus.Fields{"context": "getting usages"})

ctx, span := pbinit.InitGetUsages(request, subject)
defer span.End()

response := a.getUsages(ctx, request)

if response.Error != nil {
log.Error(response.Error.Message)
}

if err = a.client.Respond(ctx, reply, response); err != nil {
log.Error(err)
}
}

func (a *App) AddUsageHandler(subject, reply string, request *qms.AddUsage) {
func (a *App) addUsage(ctx context.Context, request *qms.AddUsage) *qms.UsageResponse {
var (
err error
usage db.Usage
)

log := log.WithFields(logrus.Fields{"context": "adding usage information"})

log.Debugf("subject: %s; reply: %s", subject, reply)

response := pbinit.NewUsageResponse()
ctx, span := pbinit.InitAddUsage(request, subject)
defer span.End()

sendError := func(ctx context.Context, response *qms.UsageResponse, err error) {
log.Error(err)
response.Error = errors.NatsError(ctx, err)
if err = a.client.Respond(ctx, reply, response); err != nil {
log.Error(err)
}
}

username, err := a.FixUsername(request.Username)
if err != nil {
sendError(ctx, response, err)
return
response.Error = errors.NatsError(ctx, err)
return response
}

d := db.New(a.db)

subscription, err := d.GetActiveSubscription(ctx, username)
if err != nil {
sendError(ctx, response, err)
return
response.Error = errors.NatsError(ctx, err)
return response
}

// Validate update type.
if _, err = d.GetOperationID(ctx, request.UpdateType); err != nil {
sendError(ctx, response, err)
return
response.Error = errors.NatsError(ctx, err)
return response
}

resourceID, err := d.GetResourceTypeID(ctx, request.ResourceName, request.ResourceUnit)
if err != nil {
sendError(ctx, response, err)
return
response.Error = errors.NatsError(ctx, err)
return response
}

usage = db.Usage{
Expand All @@ -132,7 +117,42 @@ func (a *App) AddUsageHandler(subject, reply string, request *qms.AddUsage) {
}

if err = d.CalculateUsage(ctx, request.UpdateType, &usage); err != nil {
sendError(ctx, response, err)
return
response.Error = errors.NatsError(ctx, err)
return response
}

u, _, err := d.GetCurrentUsage(ctx, resourceID, subscription.ID)
if err != nil {
response.Error = errors.NatsError(ctx, err)
return response
}

response.Usage = &qms.Usage{
Usage: u,
SubscriptionId: subscription.ID,
ResourceType: &qms.ResourceType{
Uuid: resourceID,
Name: request.ResourceName,
Unit: request.ResourceUnit,
},
}

return response
}

func (a *App) AddUsageHandler(subject, reply string, request *qms.AddUsage) {
var err error

ctx, span := pbinit.InitAddUsage(request, subject)
defer span.End()

response := a.addUsage(ctx, request)

if response.Error != nil {
log.Error(response.Error.Message)
}

if err = a.client.Respond(ctx, reply, response); err != nil {
log.Error(err)
}
}
Loading

0 comments on commit e5cdb92

Please sign in to comment.