Skip to content

Commit 2ec4500

Browse files
author
Andrew Farries
committed
Query Stripe API to find customers for each teamId
For each teamId in the usage report, query the Stripe API to find Stripe Customer records that correspond to those teamIds.
1 parent 99e1695 commit 2ec4500

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

components/usage/pkg/controller/reconciler.go

+18
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"github.com/gitpod-io/gitpod/common-go/log"
1212
"github.com/gitpod-io/gitpod/usage/pkg/db"
13+
"github.com/gitpod-io/gitpod/usage/pkg/stripe"
1314
"github.com/google/uuid"
1415
"gorm.io/gorm"
1516
"io/ioutil"
@@ -125,9 +126,26 @@ func (u *UsageReconciler) ReconcileTimeRange(ctx context.Context, from, to time.
125126
}
126127
status.Report = report
127128

129+
submitUsageReport(status.Report)
130+
128131
return status, nil
129132
}
130133

134+
func submitUsageReport(report []TeamUsage) {
135+
var teamIdSet = make(map[string]bool)
136+
137+
// Convert the usage report into a set of teamIds occurring in the report
138+
for _, usageEntry := range report {
139+
teamIdSet[usageEntry.TeamID] = true
140+
}
141+
teamIds := make([]string, 0, len(teamIdSet))
142+
for k := range teamIdSet {
143+
teamIds = append(teamIds, k)
144+
}
145+
146+
stripe.FindCustomersForTeamIds(teamIds)
147+
}
148+
131149
func generateUsageReport(teams []teamWithWorkspaces, maxStopTime time.Time) ([]TeamUsage, error) {
132150
var report []TeamUsage
133151
for _, team := range teams {

components/usage/pkg/stripe/stripe.go

+17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010
"os"
1111
"strings"
1212

13+
"github.com/gitpod-io/gitpod/common-go/log"
1314
"github.com/stripe/stripe-go/v72"
15+
"github.com/stripe/stripe-go/v72/customer"
1416
)
1517

1618
type stripeKeys struct {
@@ -34,6 +36,21 @@ func Authenticate(apiKeyFile string) error {
3436
return nil
3537
}
3638

39+
// FindCustomersForTeamIds queries the stripe API to find all customers with a teamId in `teamIds`.
40+
func FindCustomersForTeamIds(teamIds []string) {
41+
queries := queriesForCustomersWithTeamIds(teamIds)
42+
43+
for _, query := range queries {
44+
log.Infof("about to make query %q", query)
45+
params := &stripe.CustomerSearchParams{SearchParams: stripe.SearchParams{Query: query}}
46+
iter := customer.Search(params)
47+
for iter.Next() {
48+
customer := iter.Customer()
49+
log.Infof("found customer %q for teamId %q", customer.Name, customer.Metadata["teamId"])
50+
}
51+
}
52+
}
53+
3754
// queriesForCustomersWithTeamIds constructs Stripe query strings to find the Stripe Customer for each teamId
3855
// It returns multiple queries, each being a big disjunction of subclauses so that we can process multiple teamIds in one query.
3956
// `clausesPerQuery` is a limit enforced by the Stripe API.

0 commit comments

Comments
 (0)