Skip to content

[usage] Persist credits used for each instance in the usage store #11406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 15, 2022

Conversation

andrew-farries
Copy link
Contributor

@andrew-farries andrew-farries commented Jul 15, 2022

Description

Calculate and persist credits used for each instance in the usage store.

On each usage reconciliation run, we store the reconciled usage in the database. This information should include the number of credits consumed by each instance.

Previously, this value was set to 0 for all instances.

Related Issue(s)

Part of #10323

How to test

Unit tests.

To test manually, run the usage component locally against the database from a preview environment that has had at least one workspace started in it. When the reconciler runs, see that the value for creditsUsed is populated in the d_b_workspace_instance_usage table:

image

Release Notes

NONE

Documentation

Werft options:

  • /werft with-preview

@andrew-farries andrew-farries requested a review from a team July 15, 2022 08:29
@github-actions github-actions bot added the team: webapp Issue belongs to the WebApp team label Jul 15, 2022
@werft-gitpod-dev-com
Copy link

started the job as gitpod-build-af-persist-credits-used-in-usage-store.1 because the annotations in the pull request description changed
(with .werft/ from main)

@@ -247,7 +248,7 @@ func usageReportToUsageRecords(report UsageReport) []db.WorkspaceInstanceUsage {
AttributionID: attributionId,
StartedAt: instance.CreationTime.Time(),
StoppedAt: stoppedAt,
CreditsUsed: 0,
CreditsUsed: float64(pricer.CreditsUsedByInstance(&instance, now)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

about the float64: I faintly recall we report credits as ints to stripe, right? If yes, we probably should do the same here, and adjust the DB/gRPC model if they don't agree.

@andrew-farries WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The designs here internal show credits displayed with fractions.

But I think you are right in that we should be consistent in allowing the credits per minute for a workspace class be fractional but always bill and display whole numbers of credits.

I'll update the types in a follow-up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding was that we have to report total usage to Stripe as an int, but we should collect usage per session in fractional units to avoid rounding errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As explained here, @jldec is right in thinking that this should in fact remain a float.

Copy link
Member

@geropl geropl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Really cool to see the piece snap together 🙃

/hold because of float vs int question. We can also do that as a follwo-up 👍

@geropl geropl self-assigned this Jul 15, 2022
@andrew-farries
Copy link
Contributor Author

/unhold

@roboquat roboquat merged commit 0945a99 into main Jul 15, 2022
@roboquat roboquat deleted the af/persist-credits-used-in-usage-store branch July 15, 2022 14:29
@roboquat roboquat added deployed: webapp Meta team change is running in production deployed Change is completely running in production labels Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deployed: webapp Meta team change is running in production deployed Change is completely running in production release-note-none size/M team: webapp Issue belongs to the WebApp team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants