From 7f3a654b268ac1a25b9bd3290a36f70eefef4110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Sun, 4 Apr 2021 09:43:49 +0200 Subject: [PATCH] fix: import bug with small number of heartbeats (fix #160) --- routes/settings.go | 15 +++++++++++---- services/imports/wakatime.go | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/routes/settings.go b/routes/settings.go index 9dbff17..0bb3c3c 100644 --- a/routes/settings.go +++ b/routes/settings.go @@ -419,19 +419,26 @@ func (h *SettingsHandler) actionImportWaktime(w http.ResponseWriter, r *http.Req count := 0 batch := make([]*models.Heartbeat, 0) + insert := func(batch []*models.Heartbeat) { + if err := h.heartbeatSrvc.InsertBatch(batch); err != nil { + logbuch.Warn("failed to insert imported heartbeat, already existing? – %v", err) + } + } + for hb := range stream { count++ batch = append(batch, hb) if len(batch) == h.config.App.ImportBatchSize { - if err := h.heartbeatSrvc.InsertBatch(batch); err != nil { - logbuch.Warn("failed to insert imported heartbeat, already existing? – %v", err) - } - + insert(batch) batch = make([]*models.Heartbeat, 0) } } + if len(batch) > 0 { + insert(batch) + } + countAfter, _ := h.heartbeatSrvc.CountByUser(user) logbuch.Info("downloaded %d heartbeats for user '%s' (%d actually imported)", count, user.ID, countAfter-countBefore) diff --git a/services/imports/wakatime.go b/services/imports/wakatime.go index 52e75dc..429c4af 100644 --- a/services/imports/wakatime.go +++ b/services/imports/wakatime.go @@ -76,7 +76,7 @@ func (w *WakatimeHeartbeatImporter) Import(user *models.User, minFrom time.Time, d := day.Format(config.SimpleDateFormat) heartbeats, err := w.fetchHeartbeats(d) if err != nil { - logbuch.Error("failed to fetch heartbeats for day '%s' and user '%s' – &v", day, user.ID, err) + logbuch.Error("failed to fetch heartbeats for day '%s' and user '%s' – &v", d, user.ID, err) } for _, h := range heartbeats {