Skip to content

Commit 4e912a6

Browse files
author
Gusted
authored
Improve Stopwatch behavior (#18930)
- Don't send empty stopwatch over and over again, only send once. - Stop interval to update stopwatch's timer when there is no more stopwatch.
1 parent 1ebb30e commit 4e912a6

File tree

5 files changed

+77
-33
lines changed

5 files changed

+77
-33
lines changed

models/issue_stopwatch.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,38 @@ func getStopwatch(ctx context.Context, userID, issueID int64) (sw *Stopwatch, ex
6666
return
6767
}
6868

69+
// UserIDCount is a simple coalition of UserID and Count
70+
type UserStopwatch struct {
71+
UserID int64
72+
StopWatches []*Stopwatch
73+
}
74+
75+
// GetUIDsAndNotificationCounts between the two provided times
76+
func GetUIDsAndStopwatch() ([]*UserStopwatch, error) {
77+
sws := []*Stopwatch{}
78+
if err := db.GetEngine(db.DefaultContext).Find(&sws); err != nil {
79+
return nil, err
80+
}
81+
if len(sws) == 0 {
82+
return []*UserStopwatch{}, nil
83+
}
84+
85+
lastUserID := int64(-1)
86+
res := []*UserStopwatch{}
87+
for _, sw := range sws {
88+
if lastUserID == sw.UserID {
89+
lastUserStopwatch := res[len(res)-1]
90+
lastUserStopwatch.StopWatches = append(lastUserStopwatch.StopWatches, sw)
91+
} else {
92+
res = append(res, &UserStopwatch{
93+
UserID: sw.UserID,
94+
StopWatches: []*Stopwatch{sw},
95+
})
96+
}
97+
}
98+
return res, nil
99+
}
100+
69101
// GetUserStopwatches return list of all stopwatches of a user
70102
func GetUserStopwatches(userID int64, listOptions db.ListOptions) ([]*Stopwatch, error) {
71103
sws := make([]*Stopwatch, 0, 8)

modules/eventsource/manager_run.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"time"
1010

1111
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/modules/convert"
1213
"code.gitea.io/gitea/modules/graceful"
14+
"code.gitea.io/gitea/modules/json"
1315
"code.gitea.io/gitea/modules/log"
1416
"code.gitea.io/gitea/modules/process"
1517
"code.gitea.io/gitea/modules/setting"
@@ -80,6 +82,31 @@ loop:
8082
})
8183
}
8284
then = now
85+
86+
if setting.Service.EnableTimetracking {
87+
usersStopwatches, err := models.GetUIDsAndStopwatch()
88+
if err != nil {
89+
log.Error("Unable to get GetUIDsAndStopwatch: %v", err)
90+
return
91+
}
92+
93+
for _, userStopwatches := range usersStopwatches {
94+
apiSWs, err := convert.ToStopWatches(userStopwatches.StopWatches)
95+
if err != nil {
96+
log.Error("Unable to APIFormat stopwatches: %v", err)
97+
continue
98+
}
99+
dataBs, err := json.Marshal(apiSWs)
100+
if err != nil {
101+
log.Error("Unable to marshal stopwatches: %v", err)
102+
continue
103+
}
104+
m.SendMessage(userStopwatches.UserID, &Event{
105+
Name: "stopwatches",
106+
Data: string(dataBs),
107+
})
108+
}
109+
}
83110
}
84111
}
85112
m.UnregisterAll()

routers/web/events/events.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@ import (
88
"net/http"
99
"time"
1010

11-
"code.gitea.io/gitea/models"
12-
"code.gitea.io/gitea/models/db"
1311
"code.gitea.io/gitea/modules/context"
14-
"code.gitea.io/gitea/modules/convert"
1512
"code.gitea.io/gitea/modules/eventsource"
1613
"code.gitea.io/gitea/modules/graceful"
17-
"code.gitea.io/gitea/modules/json"
1814
"code.gitea.io/gitea/modules/log"
19-
"code.gitea.io/gitea/modules/setting"
2015
"code.gitea.io/gitea/routers/web/auth"
2116
)
2217

@@ -71,8 +66,6 @@ func Events(ctx *context.Context) {
7166

7267
timer := time.NewTicker(30 * time.Second)
7368

74-
stopwatchTimer := time.NewTicker(setting.UI.Notification.MinTimeout)
75-
7669
loop:
7770
for {
7871
select {
@@ -93,32 +86,6 @@ loop:
9386
case <-shutdownCtx.Done():
9487
go unregister()
9588
break loop
96-
case <-stopwatchTimer.C:
97-
sws, err := models.GetUserStopwatches(ctx.Doer.ID, db.ListOptions{})
98-
if err != nil {
99-
log.Error("Unable to GetUserStopwatches: %v", err)
100-
continue
101-
}
102-
apiSWs, err := convert.ToStopWatches(sws)
103-
if err != nil {
104-
log.Error("Unable to APIFormat stopwatches: %v", err)
105-
continue
106-
}
107-
dataBs, err := json.Marshal(apiSWs)
108-
if err != nil {
109-
log.Error("Unable to marshal stopwatches: %v", err)
110-
continue
111-
}
112-
_, err = (&eventsource.Event{
113-
Name: "stopwatches",
114-
Data: string(dataBs),
115-
}).WriteTo(ctx.Resp)
116-
if err != nil {
117-
log.Error("Unable to write to EventStream for user %s: %v", ctx.Doer.Name, err)
118-
go unregister()
119-
break loop
120-
}
121-
ctx.Resp.Flush()
12289
case event, ok := <-messageChan:
12390
if !ok {
12491
break loop

routers/web/repo/issue_stopwatch.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"strings"
1010

1111
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/models/db"
1213
"code.gitea.io/gitea/modules/context"
14+
"code.gitea.io/gitea/modules/eventsource"
1315
)
1416

1517
// IssueStopwatch creates or stops a stopwatch for the given issue.
@@ -59,6 +61,18 @@ func CancelStopwatch(c *context.Context) {
5961
return
6062
}
6163

64+
stopwatches, err := models.GetUserStopwatches(c.Doer.ID, db.ListOptions{})
65+
if err != nil {
66+
c.ServerError("GetUserStopwatches", err)
67+
return
68+
}
69+
if len(stopwatches) == 0 {
70+
eventsource.GetManager().SendMessage(c.Doer.ID, &eventsource.Event{
71+
Name: "stopwatches",
72+
Data: "{}",
73+
})
74+
}
75+
6276
url := issue.HTMLURL()
6377
c.Redirect(url, http.StatusSeeOther)
6478
}

web_src/js/features/stopwatch.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ function updateStopwatchData(data) {
127127
const watch = data[0];
128128
const btnEl = $('.active-stopwatch-trigger');
129129
if (!watch) {
130+
if (updateTimeInterval) {
131+
clearInterval(updateTimeInterval);
132+
updateTimeInterval = null;
133+
}
130134
btnEl.addClass('hidden');
131135
} else {
132136
const {repo_owner_name, repo_name, issue_index, seconds} = watch;

0 commit comments

Comments
 (0)