Skip to content

Commit

Permalink
Fix rate limit custom number
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Jan 6, 2025
1 parent 92addb5 commit 7ab03d6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ some/submodule/name = new project name
| api_key | Your wakatime api key. | _string_ | |
| api_key_vault_cmd | A command to get your api key, perhaps from some sort of secure vault. Actually a space-separated list of an executable and its arguments. Executables in PATH can be referred to by their basenames. Shell syntax not supported. | _string_ | |
| api_url | The WakaTime API base url. | _string_ | <https://api.wakatime.com/api/v1> |
| heartbeat_rate_limit_seconds | Rate limit sending heartbeats to the API once per duration. Set to 0 to disable rate limiting. | _int_ | `120` |
| heartbeat_rate_limit_seconds | Rate limit sending heartbeats to the API once per duration. Set to -1 to disable rate limiting. | _int_ | `120` |
| hide_file_names | Obfuscate filenames. Will not send file names to api. | _bool_;_list_ | `false` |
| hide_project_names | Obfuscate project names. When a project folder is detected instead of using the folder name as the project, a `.wakatime-project file` is created with a random project name. | _bool_;_list_ | `false` |
| hide_branch_names | Obfuscate branch names. Will not send revision control branch names to api. | _bool_;_list_ | `false` |
Expand Down
20 changes: 15 additions & 5 deletions cmd/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/wakatime/wakatime-cli/pkg/heartbeat"
"github.com/wakatime/wakatime-cli/pkg/ini"
"github.com/wakatime/wakatime-cli/pkg/log"
"github.com/wakatime/wakatime-cli/pkg/offline"
"github.com/wakatime/wakatime-cli/pkg/output"
"github.com/wakatime/wakatime-cli/pkg/project"
"github.com/wakatime/wakatime-cli/pkg/regex"
Expand Down Expand Up @@ -658,11 +659,20 @@ func LoadOfflineParams(ctx context.Context, v *viper.Viper) Offline {

logger := log.Extract(ctx)

rateLimit, _ := vipertools.FirstNonEmptyInt(v, "heartbeat-rate-limit-seconds", "settings.heartbeat_rate_limit_seconds")
if rateLimit < 0 {
logger.Warnf("argument --heartbeat-rate-limit-seconds must be zero or a positive integer number, got %d", rateLimit)
rateLimit := offline.RateLimitDefaultSeconds

rateLimit = 0
if rateLimitSecs, ok := vipertools.FirstNonEmptyInt(v,
"heartbeat-rate-limit-seconds",
"settings.heartbeat_rate_limit_seconds"); ok {
rateLimit = rateLimitSecs

if rateLimit < 0 {
logger.Warnf(
"argument --heartbeat-rate-limit-seconds must be zero or a positive integer number, got %d",
rateLimit,
)
rateLimit = 0
}
}

syncMax := v.GetInt("sync-offline-activity")
Expand Down Expand Up @@ -1100,7 +1110,7 @@ func (p Offline) String() string {
}

return fmt.Sprintf(
"disabled: %t, last sent at: '%s', print max: %d, num rate limit: %d, num sync max: %d",
"disabled: %t, last sent at: '%s', print max: %d, rate limit: %s, num sync max: %d",
p.Disabled,
lastSentAt,
p.PrintMax,
Expand Down
19 changes: 10 additions & 9 deletions cmd/params/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
"github.com/wakatime/wakatime-cli/pkg/heartbeat"
inipkg "github.com/wakatime/wakatime-cli/pkg/ini"
"github.com/wakatime/wakatime-cli/pkg/log"
"github.com/wakatime/wakatime-cli/pkg/offline"
"github.com/wakatime/wakatime-cli/pkg/output"
"github.com/wakatime/wakatime-cli/pkg/project"
"github.com/wakatime/wakatime-cli/pkg/regex"
"gopkg.in/ini.v1"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/ini.v1"
)

func TestLoadHeartbeatParams_AlternateProject(t *testing.T) {
Expand Down Expand Up @@ -1830,9 +1831,9 @@ func TestLoadOfflineParams_RateLimit_FromConfig(t *testing.T) {
assert.Equal(t, time.Duration(10)*time.Second, params.RateLimit)
}

func TestLoadOfflineParams_RateLimit_Zero(t *testing.T) {
func TestLoadOfflineParams_RateLimit_Disabled(t *testing.T) {
v := viper.New()
v.Set("heartbeat-rate-limit-seconds", "0")
v.Set("heartbeat-rate-limit-seconds", "-1")

params := cmdparams.LoadOfflineParams(context.Background(), v)

Expand All @@ -1841,11 +1842,11 @@ func TestLoadOfflineParams_RateLimit_Zero(t *testing.T) {

func TestLoadOfflineParams_RateLimit_Default(t *testing.T) {
v := viper.New()
v.SetDefault("heartbeat-rate-limit-seconds", 20)
v.SetDefault("heartbeat-rate-limit-seconds", offline.RateLimitDefaultSeconds)

params := cmdparams.LoadOfflineParams(context.Background(), v)

assert.Equal(t, time.Duration(20)*time.Second, params.RateLimit)
assert.Equal(t, time.Duration(offline.RateLimitDefaultSeconds)*time.Second, params.RateLimit)
}

func TestLoadOfflineParams_RateLimit_NegativeNumber(t *testing.T) {
Expand All @@ -1863,7 +1864,7 @@ func TestLoadOfflineParams_RateLimit_NonIntegerValue(t *testing.T) {

params := cmdparams.LoadOfflineParams(context.Background(), v)

assert.Zero(t, params.RateLimit)
assert.Equal(t, time.Duration(offline.RateLimitDefaultSeconds)*time.Second, params.RateLimit)
}

func TestLoadOfflineParams_LastSentAt(t *testing.T) {
Expand All @@ -1889,7 +1890,7 @@ func TestLoadOfflineParams_LastSentAt_Err(t *testing.T) {

func TestLoadOfflineParams_LastSentAtFuture(t *testing.T) {
v := viper.New()
lastSentAt := time.Now().Add(time.Duration(2) * time.Hour)
lastSentAt := time.Now().Add(2 * time.Hour)
v.Set("internal.heartbeats_last_sent_at", lastSentAt.Format(inipkg.DateFormat))

params := cmdparams.LoadOfflineParams(context.Background(), v)
Expand Down Expand Up @@ -2658,14 +2659,14 @@ func TestOffline_String(t *testing.T) {
Disabled: true,
LastSentAt: lastSentAt,
PrintMax: 6,
RateLimit: 15,
RateLimit: time.Duration(15) * time.Second,
SyncMax: 12,
}

assert.Equal(
t,
"disabled: true, last sent at: '2021-08-30T18:50:42-03:00', print max: 6,"+
" num rate limit: 15, num sync max: 12",
" rate limit: 15s, num sync max: 12",
offline.String(),
)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ func setFlags(cmd *cobra.Command, v *viper.Viper) {
"Enable detecting language from file contents.")
flags.Int(
"heartbeat-rate-limit-seconds",
offline.RateLimitDefaultSeconds,
0, // Set default to zero to correctly handle it in params.go

Check warning on line 129 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L129

Added line #L129 was not covered by tests
fmt.Sprintf("Only sync heartbeats to the API once per these seconds, instead"+
" saving to the offline db. Defaults to %d. Use zero to disable.",
" saving to the offline db. Defaults to %d. Use -1 to disable.",

Check warning on line 131 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L131

Added line #L131 was not covered by tests
offline.RateLimitDefaultSeconds),
)
flags.String("hide-branch-names", "", "Obfuscate branch names. Will not send revision control branch names to api.")
Expand Down
4 changes: 4 additions & 0 deletions cmd/run_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ func TestParseConfigFiles(t *testing.T) {
assert.Equal(t,
"2006-01-02T15:04:05Z07:00",
v.GetString("internal.backoff_at"))
assert.Equal(t,
"2025-01-05T22:21:51Z03:00",
v.GetString("internal.heartbeats_last_sent_at"),
)
}

func TestParseConfigFiles_MissingAPIKey(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions cmd/testdata/.wakatime-internal.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[internal]
backoff_retries = 1
backoff_at = 2006-01-02T15:04:05Z07:00
heartbeats_last_sent_at = 2025-01-05T22:21:51Z03:00
1 change: 0 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,6 @@ func TestSendHeartbeats_ExtraHeartbeats_SyncLegacyOfflineActivity(t *testing.T)
"--offline-queue-file-legacy", offlineQueueFileLegacy.Name(),
"--lineno", "42",
"--lines-in-file", "100",
"--heartbeat-rate-limit-seconds", "0",
"--time", "1585598059",
"--hide-branch-names", ".*",
"--write",
Expand Down

0 comments on commit 7ab03d6

Please sign in to comment.