loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: use better code to group UID and stopwatches

- Instead of having code that relied on the result being sorted (which
wasn't specified in the query and therefore not safe to assume so). Use
a map where it doesn't care if the result that we get from the database
is sorted or not.
- Added unit test.

Gusted e4eb82b7 1e1b162c

+58 -22
+11
models/issues/TestGetUIDsAndStopwatch/stopwatch.yml
··· 1 + - 2 + id: 3 3 + user_id: 1 4 + issue_id: 2 5 + created_unix: 1500988004 6 + 7 + - 8 + id: 4 9 + user_id: 3 10 + issue_id: 0 11 + created_unix: 1500988003
+4 -19
models/issues/stopwatch.go
··· 60 60 return sw, exists, err 61 61 } 62 62 63 - // UserIDCount is a simple coalition of UserID and Count 64 - type UserStopwatch struct { 65 - UserID int64 66 - StopWatches []*Stopwatch 67 - } 68 - 69 63 // GetUIDsAndNotificationCounts between the two provided times 70 - func GetUIDsAndStopwatch(ctx context.Context) ([]*UserStopwatch, error) { 64 + func GetUIDsAndStopwatch(ctx context.Context) (map[int64][]*Stopwatch, error) { 71 65 sws := []*Stopwatch{} 72 66 if err := db.GetEngine(ctx).Where("issue_id != 0").Find(&sws); err != nil { 73 67 return nil, err 74 68 } 69 + res := map[int64][]*Stopwatch{} 75 70 if len(sws) == 0 { 76 - return []*UserStopwatch{}, nil 71 + return res, nil 77 72 } 78 73 79 - lastUserID := int64(-1) 80 - res := []*UserStopwatch{} 81 74 for _, sw := range sws { 82 - if lastUserID == sw.UserID { 83 - lastUserStopwatch := res[len(res)-1] 84 - lastUserStopwatch.StopWatches = append(lastUserStopwatch.StopWatches, sw) 85 - } else { 86 - res = append(res, &UserStopwatch{ 87 - UserID: sw.UserID, 88 - StopWatches: []*Stopwatch{sw}, 89 - }) 90 - } 75 + res[sw.UserID] = append(res[sw.UserID], sw) 91 76 } 92 77 return res, nil 93 78 }
+40
models/issues/stopwatch_test.go
··· 4 4 package issues_test 5 5 6 6 import ( 7 + "path/filepath" 7 8 "testing" 8 9 9 10 "code.gitea.io/gitea/models/db" 10 11 issues_model "code.gitea.io/gitea/models/issues" 11 12 "code.gitea.io/gitea/models/unittest" 12 13 user_model "code.gitea.io/gitea/models/user" 14 + "code.gitea.io/gitea/modules/setting" 13 15 "code.gitea.io/gitea/modules/timeutil" 14 16 15 17 "github.com/stretchr/testify/assert" ··· 77 79 unittest.AssertNotExistsBean(t, &issues_model.Stopwatch{UserID: 2, IssueID: 2}) 78 80 unittest.AssertExistsAndLoadBean(t, &issues_model.TrackedTime{UserID: 2, IssueID: 2}) 79 81 } 82 + 83 + func TestGetUIDsAndStopwatch(t *testing.T) { 84 + defer unittest.OverrideFixtures( 85 + unittest.FixturesOptions{ 86 + Dir: filepath.Join(setting.AppWorkPath, "models/fixtures/"), 87 + Base: setting.AppWorkPath, 88 + Dirs: []string{"models/issues/TestGetUIDsAndStopwatch/"}, 89 + }, 90 + )() 91 + require.NoError(t, unittest.PrepareTestDatabase()) 92 + 93 + uidStopwatches, err := issues_model.GetUIDsAndStopwatch(db.DefaultContext) 94 + require.NoError(t, err) 95 + assert.EqualValues(t, map[int64][]*issues_model.Stopwatch{ 96 + 1: { 97 + { 98 + ID: 1, 99 + UserID: 1, 100 + IssueID: 1, 101 + CreatedUnix: timeutil.TimeStamp(1500988001), 102 + }, 103 + { 104 + ID: 3, 105 + UserID: 1, 106 + IssueID: 2, 107 + CreatedUnix: timeutil.TimeStamp(1500988004), 108 + }, 109 + }, 110 + 2: { 111 + { 112 + ID: 2, 113 + UserID: 2, 114 + IssueID: 2, 115 + CreatedUnix: timeutil.TimeStamp(1500988002), 116 + }, 117 + }, 118 + }, uidStopwatches) 119 + }
+3 -3
modules/eventsource/manager_run.go
··· 90 90 return 91 91 } 92 92 93 - for _, userStopwatches := range usersStopwatches { 94 - apiSWs, err := convert.ToStopWatches(ctx, userStopwatches.StopWatches) 93 + for uid, stopwatches := range usersStopwatches { 94 + apiSWs, err := convert.ToStopWatches(ctx, stopwatches) 95 95 if err != nil { 96 96 if !issues_model.IsErrIssueNotExist(err) { 97 97 log.Error("Unable to APIFormat stopwatches: %v", err) ··· 103 103 log.Error("Unable to marshal stopwatches: %v", err) 104 104 continue 105 105 } 106 - m.SendMessage(userStopwatches.UserID, &Event{ 106 + m.SendMessage(uid, &Event{ 107 107 Name: "stopwatches", 108 108 Data: string(dataBs), 109 109 })