Skip to content

Commit

Permalink
Refactor: TotalTimest return seconds (#25370)
Browse files Browse the repository at this point in the history
so template/browser can deal with string format

---
*Sponsored by Kithara Software GmbH*
  • Loading branch information
6543 authored Jun 19, 2023
1 parent bd2e322 commit 749802c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
8 changes: 4 additions & 4 deletions models/issues/tracked_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ func addTime(ctx context.Context, user *user_model.User, issue *Issue, amount in
return tt, db.Insert(ctx, tt)
}

// TotalTimes returns the spent time for each user by an issue
func TotalTimes(options *FindTrackedTimesOptions) (map[*user_model.User]string, error) {
// TotalTimes returns the spent time in seconds for each user by an issue
func TotalTimes(options *FindTrackedTimesOptions) (map[*user_model.User]int64, error) {
trackedTimes, err := GetTrackedTimes(db.DefaultContext, options)
if err != nil {
return nil, err
Expand All @@ -211,7 +211,7 @@ func TotalTimes(options *FindTrackedTimesOptions) (map[*user_model.User]string,
totalTimesByUser[t.UserID] += t.Time
}

totalTimes := make(map[*user_model.User]string)
totalTimes := make(map[*user_model.User]int64)
// Fetching User and making time human readable
for userID, total := range totalTimesByUser {
user, err := user_model.GetUserByID(db.DefaultContext, userID)
Expand All @@ -221,7 +221,7 @@ func TotalTimes(options *FindTrackedTimesOptions) (map[*user_model.User]string,
}
return nil, err
}
totalTimes[user] = util.SecToTime(total)
totalTimes[user] = total
}
return totalTimes, nil
}
Expand Down
12 changes: 6 additions & 6 deletions models/issues/tracked_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ func TestTotalTimes(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, total, 1)
for user, time := range total {
assert.Equal(t, int64(1), user.ID)
assert.Equal(t, "6 minutes 40 seconds", time)
assert.EqualValues(t, 1, user.ID)
assert.EqualValues(t, 400, time)
}

total, err = issues_model.TotalTimes(&issues_model.FindTrackedTimesOptions{IssueID: 2})
assert.NoError(t, err)
assert.Len(t, total, 2)
for user, time := range total {
if user.ID == 2 {
assert.Equal(t, "1 hour 1 minute", time)
assert.EqualValues(t, 3662, time)
} else if user.ID == 1 {
assert.Equal(t, "20 seconds", time)
assert.EqualValues(t, 20, time)
} else {
assert.Error(t, assert.AnError)
}
Expand All @@ -107,8 +107,8 @@ func TestTotalTimes(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, total, 1)
for user, time := range total {
assert.Equal(t, int64(2), user.ID)
assert.Equal(t, "1 second", time)
assert.EqualValues(t, 2, user.ID)
assert.EqualValues(t, 1, time)
}

total, err = issues_model.TotalTimes(&issues_model.FindTrackedTimesOptions{IssueID: 4})
Expand Down
2 changes: 1 addition & 1 deletion templates/repo/issue/view_content/sidebar.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
<div class="content">
{{template "shared/user/authorlink" $user}}
<div class="text">
{{$trackedtime}}
{{$trackedtime|Sec2Time}}
</div>
</div>
</div>
Expand Down

0 comments on commit 749802c

Please sign in to comment.