Skip to content

Commit

Permalink
- fixed issue with duration when computer sleeps
Browse files Browse the repository at this point in the history
  • Loading branch information
maZahaca committed Nov 5, 2021
1 parent 28d3110 commit 1d2de84
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
15 changes: 12 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func init() {
}

func updateEntries(trackerSvc trackers.TrackerApi, syncSvc sync.SyncApi, projects []interface{}, start time.Time, end time.Time) error {
fmt.Printf("Update entries for duration %s\n", end.Sub(start))
log.Printf("Update entries for duration %s\n", end.Sub(start))
entries, err := trackerSvc.GetTimeEntries(start, end)
if err != nil {
return err
Expand Down Expand Up @@ -75,6 +75,8 @@ func main() {
log.Fatal(err)
}

log.Printf("Sync started with config period: %s, every: %s", lookupTimeframe, every)

end := time.Now()
start := end.Add(-lookupTimeframe)

Expand All @@ -95,14 +97,21 @@ func main() {
if err != nil {
log.Fatal(err)
}
lastRun := time.Now().Format("2006-01-02T15:04:05-0700")
if service {
ticker := time.NewTicker(every)
ticker := time.NewTicker(time.Second)
quit := make(chan error, 1)
go func() {
for {
select {
case <-ticker.C:
err = updateEntries(trackerSvc, syncSvc, (jiraConf["projects"]).([]interface{}), start, end)
nowFormatted := time.Now().Format("2006-01-02T15:04:05-0700")
now, err := time.Parse("2006-01-02T15:04:05-0700", nowFormatted)
last, err := time.Parse("2006-01-02T15:04:05-0700", lastRun)
if now.Sub(last).Seconds() > every.Seconds() {
err = updateEntries(trackerSvc, syncSvc, (jiraConf["projects"]).([]interface{}), start, end)
lastRun = time.Now().Format("2006-01-02T15:04:05-0700")
}
if err != nil {
quit <- err
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/sync/jira.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package sync

import (
"fmt"
"log"

"github.com/andygrunwald/go-jira"
"godep.io/timemate/pkg/trackers"
)
Expand Down Expand Up @@ -48,9 +49,6 @@ func findWorklog(worklogs []jira.WorklogRecord, entry trackers.TimeEntry) *jira.

func (ji *JiraSync) Sync(tasks []Task) (err error) {
for _, t := range tasks {
//is, _, err := ji.api.Issue.Get(t.Id, &jira.GetQueryOptions{
// Fields: "worklog, worklogs",
//})
worklog, _, err := ji.api.Issue.GetWorklogs(t.Id, jira.WithQueryOptions(&jira.AddWorklogQueryOptions{
Expand: "properties",
}))
Expand All @@ -61,6 +59,10 @@ func (ji *JiraSync) Sync(tasks []Task) (err error) {
w := findWorklog(worklog.Worklogs, e)
st := jira.Time(e.Start())
diff := e.Stop().Sub(e.Start())
// do not perform update if we have the same values for time/description
if w != nil && w.Comment == e.Description() && int(diff.Seconds()) == w.TimeSpentSeconds {
continue
}
record := &jira.WorklogRecord{
Comment: e.Description(),
Started: &st,
Expand All @@ -79,7 +81,7 @@ func (ji *JiraSync) Sync(tasks []Task) (err error) {
} else {
_, _, err = ji.api.Issue.AddWorklogRecord(t.Id, record)
}
fmt.Printf("Synchronized time entry of %s for task %s\n", diff, t.Id)
log.Printf("Synchronized time entry of %s for task %s\n", diff, t.Id)
}
}
return err
Expand Down

0 comments on commit 1d2de84

Please sign in to comment.