Skip to content

Commit

Permalink
Remove embedded dashboard, improve billing hooks (#4561)
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAraujo authored Apr 18, 2024
1 parent 62a5d5b commit 1d5496c
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 331 deletions.
55 changes: 0 additions & 55 deletions api/server/handlers/billing/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,61 +104,6 @@ func (c *ListCreditsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c.WriteResult(w, r, credits)
}

// GetUsageDashboardHandler returns an embeddable dashboard to display information related to customer usage.
type GetUsageDashboardHandler struct {
handlers.PorterHandlerReadWriter
}

// NewGetUsageDashboardHandler returns a new GetUsageDashboardHandler
func NewGetUsageDashboardHandler(
config *config.Config,
decoderValidator shared.RequestDecoderValidator,
writer shared.ResultWriter,
) *GetUsageDashboardHandler {
return &GetUsageDashboardHandler{
PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
}
}

func (c *GetUsageDashboardHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, span := telemetry.NewSpan(r.Context(), "serve-usage-dashboard")
defer span.End()

proj, _ := ctx.Value(types.ProjectScope).(*models.Project)

if !c.Config().BillingManager.MetronomeEnabled || !proj.GetFeatureFlag(models.MetronomeEnabled, c.Config().LaunchDarklyClient) {
c.WriteResult(w, r, "")

telemetry.WithAttributes(span,
telemetry.AttributeKV{Key: "metronome-config-exists", Value: c.Config().BillingManager.MetronomeEnabled},
telemetry.AttributeKV{Key: "metronome-enabled", Value: proj.GetFeatureFlag(models.MetronomeEnabled, c.Config().LaunchDarklyClient)},
)
return
}

telemetry.WithAttributes(span,
telemetry.AttributeKV{Key: "metronome-enabled", Value: true},
telemetry.AttributeKV{Key: "usage-id", Value: proj.UsageID},
)

request := &types.EmbeddableDashboardRequest{}

if ok := c.DecodeAndValidate(w, r, request); !ok {
err := telemetry.Error(ctx, span, nil, "error decoding embeddable usage dashboard request")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
return
}

credits, err := c.Config().BillingManager.MetronomeClient.GetCustomerDashboard(ctx, proj.UsageID, request.DashboardType, request.Options, request.ColorOverrides)
if err != nil {
err := telemetry.Error(ctx, span, err, "error getting customer dashboard")
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}

c.WriteResult(w, r, credits)
}

// ListCustomerUsageHandler returns customer usage aggregations like CPU and RAM hours.
type ListCustomerUsageHandler struct {
handlers.PorterHandlerReadWriter
Expand Down
28 changes: 0 additions & 28 deletions api/server/router/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,34 +397,6 @@ func getProjectRoutes(
Router: r,
})

// POST /api/projects/{project_id}/billing/dashboard -> project.NewGetUsageDashboardHandler
getUsageDashboardEndpoint := factory.NewAPIEndpoint(
&types.APIRequestMetadata{
Verb: types.APIVerbCreate,
Method: types.HTTPVerbPost,
Path: &types.Path{
Parent: basePath,
RelativePath: relPath + "/billing/dashboard",
},
Scopes: []types.PermissionScope{
types.UserScope,
types.ProjectScope,
},
},
)

getUsageDashboardHandler := billing.NewGetUsageDashboardHandler(
config,
factory.GetDecoderValidator(),
factory.GetResultWriter(),
)

routes = append(routes, &router.Route{
Endpoint: getUsageDashboardEndpoint,
Handler: getUsageDashboardHandler,
Router: r,
})

// POST /api/projects/{project_id}/billing/usage -> project.NewListCustomerUsageHandler
listCustomerUsageEndpoint := factory.NewAPIEndpoint(
&types.APIRequestMetadata{
Expand Down
Loading

0 comments on commit 1d5496c

Please sign in to comment.