Skip to content
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

[usage] Introduce GetBalance API #13214

Merged
merged 1 commit into from
Sep 23, 2022
Merged

[usage] Introduce GetBalance API #13214

merged 1 commit into from
Sep 23, 2022

Conversation

svenefftinge
Copy link
Member

@svenefftinge svenefftinge commented Sep 22, 2022

Description

Introduces a dedicated getBalance API that we can use to check against the spending limit in the billing-service. We currently use balance from ListUsage, which i a follow up change will be changed to only sum up the usage for the given range.

Related Issue(s)

Related #13155

How to test

Release Notes

NONE

Documentation

Werft options:

  • /werft with-local-preview
    If enabled this will build install/preview
  • /werft with-preview
  • /werft with-payment
  • /werft with-integration-tests=all
    Valid options are all, workspace, webapp, ide

@svenefftinge svenefftinge requested a review from a team September 22, 2022 14:54
@github-actions github-actions bot added the team: webapp Issue belongs to the WebApp team label Sep 22, 2022
@werft-gitpod-dev-com
Copy link

started the job as gitpod-build-se-get-balance.1 because the annotations in the pull request description changed
(with .werft/ from main)

Copy link
Member

@easyCZ easyCZ left a comment

Choose a reason for hiding this comment

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

LGTM

/hold for test & if you want to follow the suggestion on db query

@@ -322,3 +322,37 @@ func TestListBalance(t *testing.T) {
CreditCents: -50,
})
}

func TestGetBalance(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Please add a test for when there are no usage records for an attributionID and it should return a 0

Copy link
Member Author

Choose a reason for hiding this comment

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

Added it here

Comment on lines +211 to +231
rows, err := conn.WithContext(ctx).
Model(&Usage{}).
Select("sum(creditCents) as balance").
Where("attributionId = ?", string(attributionId)).
Group("attributionId").
Rows()
if err != nil {
return 0, fmt.Errorf("failed to get rows for list balance query: %w", err)
}
defer rows.Close()

if !rows.Next() {
return 0, nil
}

var balance CreditCents
err = conn.ScanRows(rows, &balance)
if err != nil {
return 0, fmt.Errorf("failed to scan row: %w", err)
}
return balance, nil
Copy link
Member

Choose a reason for hiding this comment

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

It should be possible to do it this way, if you wanted

var balance Balance
	tx := conn.WithContext(ctx).
		Table((&Usage{}).TableName()).
		Select("attributionId, sum(creditCents) as creditCents").
		Where("attributionId = ?", string(attributionId)).
		Group("attributionId").
		First(&balance)
	if tx.Error != nil {
		if tx.Error == gorm.ErrRecordNotFound {
			return 0, nil
		}
		
		return 0, fmt.Errorf("failed to get rows for list balance query: %w", tx.Error)
	}

	return balance.CreditCents, nil

Copy link
Member Author

Choose a reason for hiding this comment

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

Why would you prefer that? btw. I realized I unnecessarily added a group by clause. Will remove that and add the suggested test in a follow-up PR.

Copy link
Member

Choose a reason for hiding this comment

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

Just because it removes the need for the manual iteration over rows, which can hurt readability for those not familiar with gorm. It also handles the not found case a bit more explicitly which helps readability. But I don't mind, just wanted to offer an alternative in case you wanted to go with it.

@roboquat roboquat merged commit 1b995ef into main Sep 23, 2022
@roboquat roboquat deleted the se/get-balance branch September 23, 2022 05:34
@easyCZ
Copy link
Member

easyCZ commented Sep 23, 2022

Ugh, why did this get merged? A race between the approve and hold?

@roboquat roboquat added deployed: webapp Meta team change is running in production deployed Change is completely running in production labels Sep 23, 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 do-not-merge/hold release-note-none size/L team: webapp Issue belongs to the WebApp team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants