Skip to content

Commit

Permalink
Fix edge case for costs and usage timestamps (#4610)
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAraujo authored May 6, 2024
1 parent e86c804 commit c2a9b8f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
3 changes: 2 additions & 1 deletion dashboard/src/main/home/app-dashboard/apps/Apps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import {
useDeploymentTargetList,
type DeploymentTarget,
} from "lib/hooks/useDeploymentTarget";
import { checkIfProjectHasPayment, useCustomerPlan } from "lib/hooks/useStripe";
import { useCustomerPlan } from "lib/hooks/useMetronome";
import { checkIfProjectHasPayment } from "lib/hooks/useStripe";

import api from "shared/api";
import { Context } from "shared/Context";
Expand Down
40 changes: 35 additions & 5 deletions internal/billing/metronome.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,16 @@ func (m MetronomeClient) ListCustomerUsage(ctx context.Context, customerID uuid.

path := "usage/groups"

startingOnTimestamp, endingBeforeTimestamp, err := parseAndCheckTimestamps(startingOn, endingBefore)
if err != nil {
return nil, telemetry.Error(ctx, span, err, err.Error())
}

baseReq := types.ListCustomerUsageRequest{
CustomerID: customerID,
WindowSize: windowsSize,
StartingOn: startingOn,
EndingBefore: endingBefore,
StartingOn: startingOnTimestamp,
EndingBefore: endingBeforeTimestamp,
CurrentPeriod: currentPeriod,
}

Expand Down Expand Up @@ -371,7 +376,12 @@ func (m MetronomeClient) ListCustomerCosts(ctx context.Context, customerID uuid.
Data []types.Cost `json:"data"`
}

queryParams := fmt.Sprintf("starting_on=%s&ending_before=%s&limit=%d", startingOn, endingBefore, limit)
startingOnTimestamp, endingBeforeTimestamp, err := parseAndCheckTimestamps(startingOn, endingBefore)
if err != nil {
return nil, telemetry.Error(ctx, span, err, err.Error())
}

queryParams := fmt.Sprintf("starting_on=%s&ending_before=%s&limit=%d", startingOnTimestamp, endingBeforeTimestamp, limit)

_, err = m.do(http.MethodGet, path, queryParams, nil, &result)
if err != nil {
Expand All @@ -380,8 +390,8 @@ func (m MetronomeClient) ListCustomerCosts(ctx context.Context, customerID uuid.

for _, customerCost := range result.Data {
formattedCost := types.FormattedCost{
StartTimestamp: customerCost.StartTimestamp,
EndTimestamp: customerCost.EndTimestamp,
StartTimestamp: startingOnTimestamp,
EndTimestamp: endingBeforeTimestamp,
}
for _, creditType := range customerCost.CreditTypes {
formattedCost.Cost += creditType.Cost
Expand Down Expand Up @@ -490,6 +500,26 @@ func (m MetronomeClient) getCreditTypeID(ctx context.Context, currencyCode strin
return creditTypeID, telemetry.Error(ctx, span, fmt.Errorf("credit type not found for currency code %s", currencyCode), "failed to find credit type")
}

// Utility function to parse and adjust times
func parseAndCheckTimestamps(startingOn string, endingBefore string) (startingOnTimestamp string, endingBeforeTimestamp string, err error) {
startingOnTime, err := time.Parse(time.RFC3339, startingOn)
if err != nil {
return startingOnTimestamp, endingBeforeTimestamp, fmt.Errorf("failed to parse starting on time: %w", err)
}

endingBeforeTime, err := time.Parse(time.RFC3339, endingBefore)
if err != nil {
return startingOnTimestamp, endingBeforeTimestamp, fmt.Errorf("failed to parse ending before time: %w", err)
}

if startingOnTime.Equal(endingBeforeTime) {
// If starting and ending timestamps are the same, change the ending timestamp to be one day in the future
endingBeforeTime = endingBeforeTime.Add(24 * time.Hour)
}

return startingOnTime.Format(time.RFC3339), endingBeforeTime.Format(time.RFC3339), nil
}

func (m MetronomeClient) do(method string, path string, queryParams string, body interface{}, data interface{}) (statusCode int, err error) {
client := http.Client{}
endpoint, err := url.JoinPath(metronomeBaseUrl, path)
Expand Down

0 comments on commit c2a9b8f

Please sign in to comment.