Skip to content

Commit 98438ff

Browse files
authored
fix: Update usage limit message (#1415)
Updates the usage limit message to also account for the case where the free quota limit is reached, and give users a clear action to perform.
1 parent 0d43ffd commit 98438ff

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

premium/monitor.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@ package premium
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"time"
78
)
89

9-
var ErrNoQuota = errors.New("no remaining quota for the month, please increase your usage limit if you want to continue syncing this plugin")
10+
type ErrNoQuota struct {
11+
team string
12+
}
13+
14+
func (e ErrNoQuota) Error() string {
15+
return fmt.Sprintf("You have reached this plugin's usage limit for the month, please visit https://cloudquery.io/teams/%s/billing to upgrade your plan or increase the limit.", e.team)
16+
}
1017

1118
const DefaultQuotaCheckInterval = 30 * time.Second
1219
const DefaultMaxQuotaFailures = 10 // 5 minutes
@@ -60,7 +67,7 @@ func (qc quotaChecker) checkInitialQuota(ctx context.Context) error {
6067
}
6168

6269
if !hasQuota {
63-
return ErrNoQuota
70+
return ErrNoQuota{team: qc.qm.TeamName()}
6471
}
6572

6673
return nil
@@ -90,7 +97,7 @@ func (qc quotaChecker) startQuotaMonitor(ctx context.Context) context.Context {
9097
consecutiveFailures = 0
9198
hasQuotaErrors = nil
9299
if !hasQuota {
93-
cancelWithCause(ErrNoQuota)
100+
cancelWithCause(ErrNoQuota{team: qc.qm.TeamName()})
94101
return
95102
}
96103
}

premium/monitor_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ func (f *fakeQuotaMonitor) HasQuota(_ context.Context) (bool, error) {
3131
return resp.hasQuota, resp.err
3232
}
3333

34+
func (*fakeQuotaMonitor) TeamName() string {
35+
return "test"
36+
}
37+
3438
func TestWithCancelOnQuotaExceeded_NoInitialQuota(t *testing.T) {
3539
ctx := context.Background()
3640

@@ -54,7 +58,7 @@ func TestWithCancelOnQuotaExceeded_NoQuota(t *testing.T) {
5458

5559
<-ctx.Done()
5660
cause := context.Cause(ctx)
57-
require.Equal(t, ErrNoQuota, cause)
61+
require.ErrorIs(t, ErrNoQuota{team: "test"}, cause)
5862
}
5963

6064
func TestWithCancelOnQuotaCheckConsecutiveFailures(t *testing.T) {

premium/tables.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import "github.com/cloudquery/plugin-sdk/v4/schema"
44

55
// ContainsPaidTables returns true if any of the tables are paid
66
func ContainsPaidTables(tables schema.Tables) bool {
7+
if tables == nil {
8+
return false
9+
}
710
for _, t := range tables {
8-
if t.IsPaid {
11+
if t.IsPaid || ContainsPaidTables(t.Relations) {
912
return true
1013
}
1114
}

premium/usage.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ type TokenClient interface {
3333
}
3434

3535
type QuotaMonitor interface {
36+
// TeamName returns the team name
37+
TeamName() string
3638
// HasQuota returns true if the quota has not been exceeded
3739
HasQuota(context.Context) (bool, error)
3840
}
@@ -241,6 +243,10 @@ func (u *BatchUpdater) Increase(rows uint32) error {
241243
return nil
242244
}
243245

246+
func (u *BatchUpdater) TeamName() string {
247+
return u.teamName
248+
}
249+
244250
func (u *BatchUpdater) HasQuota(ctx context.Context) (bool, error) {
245251
u.logger.Debug().Str("url", u.url).Str("team", u.teamName).Str("pluginTeam", u.pluginTeam).Str("pluginKind", string(u.pluginKind)).Str("pluginName", u.pluginName).Msg("checking quota")
246252
usage, err := u.apiClient.GetTeamPluginUsageWithResponse(ctx, u.teamName, u.pluginTeam, u.pluginKind, u.pluginName)
@@ -250,6 +256,7 @@ func (u *BatchUpdater) HasQuota(ctx context.Context) (bool, error) {
250256
if usage.StatusCode() != http.StatusOK {
251257
return false, fmt.Errorf("failed to get usage: %s", usage.Status())
252258
}
259+
253260
hasQuota := usage.JSON200.RemainingRows == nil || *usage.JSON200.RemainingRows > 0
254261
return hasQuota, nil
255262
}

0 commit comments

Comments
 (0)