Skip to content

Commit

Permalink
basic usage display (#4634)
Browse files Browse the repository at this point in the history
Co-authored-by: Mauricio Araujo <[email protected]>
  • Loading branch information
jusrhee and MauAraujo authored May 15, 2024
1 parent 7799df5 commit 3bfe751
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 203 deletions.
2 changes: 1 addition & 1 deletion api/server/handlers/billing/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (c *ListCustomerUsageHandler) ServeHTTP(w http.ResponseWriter, r *http.Requ
telemetry.AttributeKV{Key: "subscription_id", Value: plan.ID},
)

usage, err := c.Config().BillingManager.LagoClient.ListCustomerUsage(ctx, plan.CustomerID, plan.ID, req.CurrentPeriod)
usage, err := c.Config().BillingManager.LagoClient.ListCustomerUsage(ctx, plan.CustomerID, plan.ID, req.CurrentPeriod, req.PreviousPeriods)
if err != nil {
err := telemetry.Error(ctx, span, err, "error listing customer usage")
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
Expand Down
3 changes: 3 additions & 0 deletions api/types/billing_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type ListCreditGrantsResponse struct {

// ListCustomerUsageRequest is the request to list usage for a customer
type ListCustomerUsageRequest struct {
// PreviousPeriods is the number of previous periods to include in the response.
PreviousPeriods int `json:"previous_periods,omitempty"`
// CurrentPeriod is whether to return only usage for the current billing period.
CurrentPeriod bool `json:"current_period,omitempty"`
}

Expand Down
20 changes: 7 additions & 13 deletions dashboard/src/lib/hooks/useLago.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type TGetInvoices = {
};

type TGetUsage = {
usage: Usage | null;
usageList: Usage[] | null;
};

type TGetReferralDetails = {
Expand Down Expand Up @@ -108,16 +108,15 @@ export const useCustomerPlan = (): TGetPlan => {
};

export const useCustomerUsage = (
startingOn: Date | null,
endingBefore: Date | null,
previousPeriods: number,
currentPeriod: boolean
): TGetUsage => {
const { currentProject } = useContext(Context);

// Fetch customer usage
const usageReq = useQuery(
["listCustomerUsage", currentProject?.id],
async (): Promise<Usage | null> => {
["listCustomerUsage", currentProject?.id, previousPeriods, currentPeriod],
async (): Promise<Usage[] | null> => {
if (!currentProject?.metronome_enabled) {
return null;
}
Expand All @@ -126,23 +125,18 @@ export const useCustomerUsage = (
return null;
}

if (startingOn === null || endingBefore === null) {
return null;
}

try {
const res = await api.getCustomerUsage(
"<token>",
{
starting_on: startingOn.toISOString(),
ending_before: endingBefore.toISOString(),
previous_periods: previousPeriods,
current_period: currentPeriod,
},
{
project_id: currentProject?.id,
}
);
const usage = UsageValidator.parse(res.data);
const usage = UsageValidator.array().parse(res.data);
return usage;
} catch (error) {
return null;
Expand All @@ -151,7 +145,7 @@ export const useCustomerUsage = (
);

return {
usage: usageReq.data ?? null,
usageList: usageReq.data ?? null,
};
};

Expand Down
18 changes: 9 additions & 9 deletions dashboard/src/main/home/project-settings/ProjectSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ function ProjectSettings(props: any) {
});
}

// if (
// currentProject?.billing_enabled &&
// currentProject?.metronome_enabled
// ) {
// tabOpts.push({
// value: "usage",
// label: "Usage",
// });
// }
if (
currentProject?.billing_enabled &&
currentProject?.metronome_enabled
) {
tabOpts.push({
value: "usage",
label: "Usage",
});
}

tabOpts.push({
value: "additional-settings",
Expand Down
Loading

0 comments on commit 3bfe751

Please sign in to comment.