8
8
"context"
9
9
"encoding/json"
10
10
"fmt"
11
+ "github.com/gitpod-io/gitpod/usage/pkg/db"
11
12
"os"
12
13
"strings"
13
14
@@ -17,8 +18,8 @@ import (
17
18
)
18
19
19
20
const (
20
- ReportIDMetadataKey = "reportId"
21
- TeamIDMetadataKey = "teamId "
21
+ ReportIDMetadataKey = "reportId"
22
+ AttributionIDMetadataKey = "attributionId "
22
23
)
23
24
24
25
type Client struct {
@@ -70,12 +71,12 @@ type CreditSummary struct {
70
71
71
72
// UpdateUsage updates teams' Stripe subscriptions with usage data
72
73
// `usageForTeam` is a map from team name to total workspace seconds used within a billing period.
73
- func (c * Client ) UpdateUsage (ctx context.Context , creditsPerTeam map [string ]CreditSummary ) error {
74
- teamIds := make ([]string , 0 , len (creditsPerTeam ))
75
- for k := range creditsPerTeam {
76
- teamIds = append (teamIds , k )
74
+ func (c * Client ) UpdateUsage (ctx context.Context , creditsPerAttributionID map [db. AttributionID ]CreditSummary ) error {
75
+ attributionIDs := make ([]db. AttributionID , 0 , len (creditsPerAttributionID ))
76
+ for k := range creditsPerAttributionID {
77
+ attributionIDs = append (attributionIDs , k )
77
78
}
78
- queries := queriesForCustomersWithTeamIds ( teamIds )
79
+ queries := queriesForCustomersWithAttributionIDs ( attributionIDs )
79
80
80
81
for _ , query := range queries {
81
82
log .Infof ("Searching customers in Stripe with query: %q" , query )
@@ -86,14 +87,21 @@ func (c *Client) UpdateUsage(ctx context.Context, creditsPerTeam map[string]Cred
86
87
}
87
88
88
89
for _ , customer := range customers {
89
- teamID := customer .Metadata ["teamId" ]
90
- log .Infof ("Found customer %q for teamId %q" , customer .Name , teamID )
90
+ attributionIDRaw := customer .Metadata [AttributionIDMetadataKey ]
91
+ log .Infof ("Found customer %q for attribution ID %q" , customer .Name , attributionIDRaw )
91
92
92
- _ , err := c .updateUsageForCustomer (ctx , customer , creditsPerTeam [teamID ])
93
+ attributionID , err := db .ParseAttributionID (attributionIDRaw )
94
+ if err != nil {
95
+ log .WithError (err ).Error ("Failed to parse attribution ID from Stripe metadata." )
96
+ continue
97
+ }
98
+
99
+ _ , err = c .updateUsageForCustomer (ctx , customer , creditsPerAttributionID [attributionID ])
93
100
if err != nil {
94
101
log .WithField ("customer_id" , customer .ID ).
95
102
WithField ("customer_name" , customer .Name ).
96
103
WithField ("subscriptions" , customer .Subscriptions ).
104
+ WithField ("attribution_id" , attributionID ).
97
105
WithError (err ).
98
106
Errorf ("Failed to update usage." )
99
107
@@ -246,19 +254,19 @@ func (c *Client) GetInvoice(ctx context.Context, invoiceID string) (*stripe.Invo
246
254
return invoice , nil
247
255
}
248
256
249
- // queriesForCustomersWithTeamIds constructs Stripe query strings to find the Stripe Customer for each teamId
257
+ // queriesForCustomersWithAttributionIDs constructs Stripe query strings to find the Stripe Customer for each teamId
250
258
// It returns multiple queries, each being a big disjunction of subclauses so that we can process multiple teamIds in one query.
251
259
// `clausesPerQuery` is a limit enforced by the Stripe API.
252
- func queriesForCustomersWithTeamIds ( teamIds []string ) []string {
260
+ func queriesForCustomersWithAttributionIDs ( attributionIDs []db. AttributionID ) []string {
253
261
const clausesPerQuery = 10
254
262
var queries []string
255
263
sb := strings.Builder {}
256
264
257
- for i := 0 ; i < len (teamIds ); i += clausesPerQuery {
265
+ for i := 0 ; i < len (attributionIDs ); i += clausesPerQuery {
258
266
sb .Reset ()
259
- for j := 0 ; j < clausesPerQuery && i + j < len (teamIds ); j ++ {
260
- sb .WriteString (fmt .Sprintf ("metadata['%s']:'%s'" , TeamIDMetadataKey , teamIds [i + j ]))
261
- if j < clausesPerQuery - 1 && i + j < len (teamIds )- 1 {
267
+ for j := 0 ; j < clausesPerQuery && i + j < len (attributionIDs ); j ++ {
268
+ sb .WriteString (fmt .Sprintf ("metadata['%s']:'%s'" , AttributionIDMetadataKey , attributionIDs [i + j ]))
269
+ if j < clausesPerQuery - 1 && i + j < len (attributionIDs )- 1 {
262
270
sb .WriteString (" OR " )
263
271
}
264
272
}
0 commit comments