Skip to content

Commit

Permalink
Check if legacy offline queue file exists
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed Jul 29, 2024
1 parent 65ec92a commit 65d83f0
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 175 deletions.
6 changes: 1 addition & 5 deletions cmd/heartbeat/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (

// Run executes the heartbeat command.
func Run(v *viper.Viper) (int, error) {
queueFilepath, err := offline.QueueFilepath()
queueFilepath, err := offline.QueueFilepath(v)

Check warning on line 34 in cmd/heartbeat/heartbeat.go

View check run for this annotation

Codecov / codecov/patch

cmd/heartbeat/heartbeat.go#L34

Added line #L34 was not covered by tests
if err != nil {
log.Warnf("failed to load offline queue filepath: %s", err)
}
Expand Down Expand Up @@ -115,10 +115,6 @@ func SendHeartbeats(v *viper.Viper, queueFilepath string) error {
handleOpts := initHandleOptions(params)

if !params.Offline.Disabled {
if params.Offline.QueueFile != "" {
queueFilepath = params.Offline.QueueFile
}

handleOpts = append(handleOpts, offline.WithQueue(queueFilepath))
}

Expand Down
4 changes: 0 additions & 4 deletions cmd/offline/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ func SaveHeartbeats(v *viper.Viper, heartbeats []heartbeat.Heartbeat, queueFilep

handleOpts := initHandleOptions(params)

if params.Offline.QueueFile != "" {
queueFilepath = params.Offline.QueueFile
}

handleOpts = append(handleOpts, offline.WithQueue(queueFilepath))

sender := offline.Noop{}
Expand Down
9 changes: 1 addition & 8 deletions cmd/offlinecount/offlinecount.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package offlinecount
import (
"fmt"

"github.com/wakatime/wakatime-cli/cmd/params"
"github.com/wakatime/wakatime-cli/pkg/exitcode"
"github.com/wakatime/wakatime-cli/pkg/offline"

Expand All @@ -12,20 +11,14 @@ import (

// Run executes the offline-count command.
func Run(v *viper.Viper) (int, error) {
queueFilepath, err := offline.QueueFilepath()
queueFilepath, err := offline.QueueFilepath(v)
if err != nil {
return exitcode.ErrGeneric, fmt.Errorf(
"failed to load offline queue filepath: %s",
err,
)
}

p := params.LoadOfflineParams(v)

if p.QueueFile != "" {
queueFilepath = p.QueueFile
}

count, err := offline.CountHeartbeats(queueFilepath)
if err != nil {
fmt.Println(err)
Expand Down
6 changes: 1 addition & 5 deletions cmd/offlineprint/offlineprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// Run executes the print-offline-heartbeats command.
func Run(v *viper.Viper) (int, error) {
queueFilepath, err := offline.QueueFilepath()
queueFilepath, err := offline.QueueFilepath(v)
if err != nil {
return exitcode.ErrGeneric, fmt.Errorf(
"failed to load offline queue filepath: %s",
Expand All @@ -25,10 +25,6 @@ func Run(v *viper.Viper) (int, error) {

p := params.LoadOfflineParams(v)

if p.QueueFile != "" {
queueFilepath = p.QueueFile
}

hh, err := offline.ReadHeartbeats(queueFilepath, p.PrintMax)
if err != nil {
fmt.Println(err)
Expand Down
22 changes: 12 additions & 10 deletions cmd/offlinesync/offlinesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ func run(v *viper.Viper) (int, error) {
return exitcode.Success, nil
}

queueFilepath, err := offline.QueueFilepath()
queueFilepath, err := offline.QueueFilepath(v)
if err != nil {
return exitcode.ErrGeneric, fmt.Errorf(
"offline sync failed: failed to load offline queue filepath: %s",
err,
)
}

queueFilepathLegacy, err := offline.QueueFilepathLegacy()
queueFilepathLegacy, err := offline.QueueFilepathLegacy(v)
if err != nil {
log.Warnf("legacy offline sync failed: failed to load offline queue filepath: %s", err)
}
Expand Down Expand Up @@ -84,6 +84,10 @@ func syncOfflineActivityLegacy(v *viper.Viper, queueFilepath string) error {
return nil
}

if !fileExists(queueFilepath) {
return nil
}

paramOffline := params.LoadOfflineParams(v)

paramAPI, err := params.LoadAPIParams(v)
Expand All @@ -96,10 +100,6 @@ func syncOfflineActivityLegacy(v *viper.Viper, queueFilepath string) error {
return fmt.Errorf("failed to initialize api client: %w", err)
}

if paramOffline.QueueFileLegacy != "" {
queueFilepath = paramOffline.QueueFileLegacy
}

handle := heartbeat.NewHandle(apiClient,
offline.WithSync(queueFilepath, paramOffline.SyncMax),
apikey.WithReplacing(apikey.Config{
Expand Down Expand Up @@ -135,10 +135,6 @@ func SyncOfflineActivity(v *viper.Viper, queueFilepath string) error {

paramOffline := params.LoadOfflineParams(v)

if paramOffline.QueueFile != "" {
queueFilepath = paramOffline.QueueFile
}

handle := heartbeat.NewHandle(apiClient,
offline.WithSync(queueFilepath, paramOffline.SyncMax),
apikey.WithReplacing(apikey.Config{
Expand All @@ -158,3 +154,9 @@ func SyncOfflineActivity(v *viper.Viper, queueFilepath string) error {

return nil
}

// fileExists checks if a file or directory exist.
func fileExists(fp string) bool {
_, err := os.Stat(fp)
return err == nil || os.IsExist(err)
}
91 changes: 0 additions & 91 deletions cmd/offlinesync/offlinesync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,97 +311,6 @@ func TestSyncOfflineActivity(t *testing.T) {
assert.Eventually(t, func() bool { return numCalls == 1 }, time.Second, 50*time.Millisecond)
}

func TestSyncOfflineActivity_QueueFileFromConfig(t *testing.T) {
resetSingleton(t)

testServerURL, router, tearDown := setupTestServer()
defer tearDown()

var (
plugin = "plugin/0.0.1"
numCalls int
)

router.HandleFunc("/users/current/heartbeats.bulk", func(w http.ResponseWriter, req *http.Request) {
numCalls++

// check request
assert.Equal(t, http.MethodPost, req.Method)
assert.Equal(t, []string{"application/json"}, req.Header["Accept"])
assert.Equal(t, []string{"application/json"}, req.Header["Content-Type"])
assert.Equal(t, []string{"Basic MDAwMDAwMDAtMDAwMC00MDAwLTgwMDAtMDAwMDAwMDAwMDAw"}, req.Header["Authorization"])
assert.True(t, strings.HasSuffix(req.Header["User-Agent"][0], plugin), fmt.Sprintf(
"%q should have suffix %q",
req.Header["User-Agent"][0],
plugin,
))

expectedBody, err := os.ReadFile("testdata/api_heartbeats_request_template.json")
require.NoError(t, err)

body, err := io.ReadAll(req.Body)
require.NoError(t, err)

assert.JSONEq(t, string(expectedBody), string(body))

// send response
w.WriteHeader(http.StatusCreated)

f, err := os.Open("testdata/api_heartbeats_response.json")
require.NoError(t, err)
defer f.Close()

_, err = io.Copy(w, f)
require.NoError(t, err)
})

// setup offline queue
f, err := os.CreateTemp(t.TempDir(), "")
require.NoError(t, err)

db, err := bolt.Open(f.Name(), 0600, nil)
require.NoError(t, err)

dataGo, err := os.ReadFile("testdata/heartbeat_go.json")
require.NoError(t, err)

dataPy, err := os.ReadFile("testdata/heartbeat_py.json")
require.NoError(t, err)

dataJs, err := os.ReadFile("testdata/heartbeat_js.json")
require.NoError(t, err)

insertHeartbeatRecords(t, db, "heartbeats", []heartbeatRecord{
{
ID: "1592868367.219124-file-coding-wakatime-cli-heartbeat-/tmp/main.go-true",
Heartbeat: string(dataGo),
},
{
ID: "1592868386.079084-file-debugging-wakatime-summary-/tmp/main.py-false",
Heartbeat: string(dataPy),
},
{
ID: "1592868394.084354-file-building-wakatime-todaygoal-/tmp/main.js-false",
Heartbeat: string(dataJs),
},
})

err = db.Close()
require.NoError(t, err)

v := viper.New()
v.Set("api-url", testServerURL)
v.Set("key", "00000000-0000-4000-8000-000000000000")
v.Set("offline-queue-file", f.Name())
v.Set("sync-offline-activity", 100)
v.Set("plugin", plugin)

err = offlinesync.SyncOfflineActivity(v, "/another/file")
require.NoError(t, err)

assert.Eventually(t, func() bool { return numCalls == 1 }, time.Second, 50*time.Millisecond)
}

func TestSyncOfflineActivity_MultipleApiKey(t *testing.T) {
resetSingleton(t)

Expand Down
29 changes: 11 additions & 18 deletions cmd/params/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,11 @@ type (

// Offline contains offline related parameters.
Offline struct {
Disabled bool
LastSentAt time.Time
PrintMax int
QueueFile string
QueueFileLegacy string
RateLimit time.Duration
SyncMax int
Disabled bool
LastSentAt time.Time
PrintMax int
RateLimit time.Duration
SyncMax int
}

// ProjectParams params for project name sanitization.
Expand Down Expand Up @@ -674,13 +672,11 @@ func LoadOfflineParams(v *viper.Viper) Offline {
}

return Offline{
Disabled: disabled,
LastSentAt: lastSentAt,
PrintMax: v.GetInt("print-offline-heartbeats"),
QueueFile: vipertools.GetString(v, "offline-queue-file"),
QueueFileLegacy: vipertools.GetString(v, "offline-queue-file-legacy"),
RateLimit: time.Duration(rateLimit) * time.Second,
SyncMax: syncMax,
Disabled: disabled,
LastSentAt: lastSentAt,
PrintMax: v.GetInt("print-offline-heartbeats"),
RateLimit: time.Duration(rateLimit) * time.Second,
SyncMax: syncMax,
}
}

Expand Down Expand Up @@ -1074,13 +1070,10 @@ func (p Offline) String() string {
}

return fmt.Sprintf(
"disabled: %t, last sent at: '%s', print max: %d, queue file: '%s', queue file legacy: '%s',"+
" num rate limit: %d, num sync max: %d",
"disabled: %t, last sent at: '%s', print max: %d, num rate limit: %d, num sync max: %d",
p.Disabled,
lastSentAt,
p.PrintMax,
p.QueueFile,
p.QueueFileLegacy,
p.RateLimit,
p.SyncMax,
)
Expand Down
31 changes: 5 additions & 26 deletions cmd/params/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1771,24 +1771,6 @@ func TestLoadOfflineParams_LastSentAt_Err(t *testing.T) {
assert.Zero(t, params.LastSentAt)
}

func TestLoadOfflineParams_QueueFile(t *testing.T) {
v := viper.New()
v.Set("offline-queue-file", "/path/to/file")

params := cmdparams.LoadOfflineParams(v)

assert.Equal(t, "/path/to/file", params.QueueFile)
}

func TestLoadOfflineParams_QueueFileLegacy(t *testing.T) {
v := viper.New()
v.Set("offline-queue-file-legacy", "/path/to/file")

params := cmdparams.LoadOfflineParams(v)

assert.Equal(t, "/path/to/file", params.QueueFileLegacy)
}

func TestLoadOfflineParams_SyncMax(t *testing.T) {
v := viper.New()
v.Set("sync-offline-activity", 42)
Expand Down Expand Up @@ -2552,19 +2534,16 @@ func TestOffline_String(t *testing.T) {
require.NoError(t, err)

offline := cmdparams.Offline{
Disabled: true,
LastSentAt: lastSentAt,
PrintMax: 6,
QueueFile: "/path/to/queue.file",
QueueFileLegacy: "/path/to/legacy.file",
RateLimit: 15,
SyncMax: 12,
Disabled: true,
LastSentAt: lastSentAt,
PrintMax: 6,
RateLimit: 15,
SyncMax: 12,
}

assert.Equal(
t,
"disabled: true, last sent at: '2021-08-30T18:50:42-03:00', print max: 6,"+
" queue file: '/path/to/queue.file', queue file legacy: '/path/to/legacy.file',"+
" num rate limit: 15, num sync max: 12",
offline.String(),
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ func runCmd(v *viper.Viper, verbose bool, sendDiagsOnErrors bool, cmd cmdFn) (er
}

func saveHeartbeats(v *viper.Viper) int {
queueFilepath, err := offline.QueueFilepath()
queueFilepath, err := offline.QueueFilepath(v)

Check warning on line 352 in cmd/run.go

View check run for this annotation

Codecov / codecov/patch

cmd/run.go#L352

Added line #L352 was not covered by tests
if err != nil {
log.Warnf("failed to load offline queue filepath: %s", err)
}
Expand Down
8 changes: 5 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func TestSendHeartbeats_ExtraHeartbeats_SyncLegacyOfflineActivity(t *testing.T)
tmpDir := t.TempDir()

// create legacy offline queue file and add some heartbeats
offlineQueueFileLegacy, err := os.CreateTemp(tmpDir, "")
offlineQueueFileLegacy, err := os.CreateTemp(tmpDir, "legacy-offline-file")
require.NoError(t, err)

// early close to avoid file locking in Windows
Expand Down Expand Up @@ -409,7 +409,7 @@ func TestSendHeartbeats_ExtraHeartbeats_SyncLegacyOfflineActivity(t *testing.T)
err = db.Close()
require.NoError(t, err)

offlineQueueFile, err := os.CreateTemp(tmpDir, "")
offlineQueueFile, err := os.CreateTemp(tmpDir, "new-offline-file")
require.NoError(t, err)

defer offlineQueueFile.Close()
Expand All @@ -429,6 +429,8 @@ func TestSendHeartbeats_ExtraHeartbeats_SyncLegacyOfflineActivity(t *testing.T)

buffer := bytes.NewBuffer(data)

assert.FileExists(t, offlineQueueFileLegacy.Name())

runWakatimeCli(
t,
buffer,
Expand All @@ -439,11 +441,11 @@ func TestSendHeartbeats_ExtraHeartbeats_SyncLegacyOfflineActivity(t *testing.T)
"--entity", "testdata/main.go",
"--extra-heartbeats", "true",
"--cursorpos", "12",
"--sync-offline-activity", "0",
"--offline-queue-file", offlineQueueFile.Name(),
"--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
Loading

0 comments on commit 65d83f0

Please sign in to comment.