From 15643bd7a4e9ec011a535bfe9c620b258ad57d60 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 6 May 2022 17:21:08 +0800 Subject: [PATCH 1/5] Don't reload action.Repo when it has been loaded --- models/action.go | 16 +++++++++++++--- models/action_list.go | 3 +++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/models/action.go b/models/action.go index efbe243bed3b9..aa52c6e212303 100644 --- a/models/action.go +++ b/models/action.go @@ -328,6 +328,11 @@ type GetFeedsOptions struct { Date string // the day we want activity for: YYYY-MM-DD } +type ExtAction struct { + Action + Repo *repo_model.Repository `xorm:"extends"` +} + // GetFeeds returns actions according to the provided options func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, error) { if opts.RequestedUser == nil && opts.RequestedTeam == nil && opts.RequestedRepo == nil { @@ -345,12 +350,17 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, error) { opts.SetDefaultValues() sess = db.SetSessionPagination(sess, &opts) - actions := make([]*Action, 0, opts.PageSize) - - if err := sess.Desc("`action`.created_unix").Find(&actions); err != nil { + extActions := make([]*ExtAction, 0, opts.PageSize) + if err := sess.Desc("`action`.created_unix").Find(&extActions); err != nil { return nil, fmt.Errorf("Find: %v", err) } + actions := make(ActionList, 0, len(extActions)) + for i := 0; i < len(extActions); i++ { + extActions[i].Action.Repo = extActions[i].Repo + actions = append(actions, &extActions[i].Action) + } + if err := ActionList(actions).loadAttributes(e); err != nil { return nil, fmt.Errorf("LoadAttributes: %v", err) } diff --git a/models/action_list.go b/models/action_list.go index 5f7b17b9de1ef..495e2438645ee 100644 --- a/models/action_list.go +++ b/models/action_list.go @@ -49,6 +49,9 @@ func (actions ActionList) loadUsers(e db.Engine) (map[int64]*user_model.User, er func (actions ActionList) getRepoIDs() []int64 { repoIDs := make(map[int64]struct{}, len(actions)) for _, action := range actions { + if action.Repo != nil { + continue + } if _, ok := repoIDs[action.RepoID]; !ok { repoIDs[action.RepoID] = struct{}{} } From de216047627288a01bbe32161045c747b4b217e8 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 6 May 2022 17:25:10 +0800 Subject: [PATCH 2/5] improve --- models/action_list.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/models/action_list.go b/models/action_list.go index 495e2438645ee..b8d30227e8d38 100644 --- a/models/action_list.go +++ b/models/action_list.go @@ -65,6 +65,10 @@ func (actions ActionList) loadRepositories(e db.Engine) error { } repoIDs := actions.getRepoIDs() + if len(repoIDs) <= 0 { + return nil + } + repoMaps := make(map[int64]*repo_model.Repository, len(repoIDs)) err := e.In("id", repoIDs).Find(&repoMaps) if err != nil { From f54b987fddc9f51c61805aa96c879bac3ec43c5a Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 8 May 2022 16:39:03 +0800 Subject: [PATCH 3/5] Fix lint --- models/action_list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/action_list.go b/models/action_list.go index b8d30227e8d38..2e609a9e2e7cd 100644 --- a/models/action_list.go +++ b/models/action_list.go @@ -65,7 +65,7 @@ func (actions ActionList) loadRepositories(e db.Engine) error { } repoIDs := actions.getRepoIDs() - if len(repoIDs) <= 0 { + if len(repoIDs) == 0 { return nil } From 952dd399c315ecd1d076cf378feb268b05950bbb Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 8 May 2022 17:34:35 +0800 Subject: [PATCH 4/5] Fix bug --- models/action.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/action.go b/models/action.go index aa52c6e212303..7a7934c6e7c41 100644 --- a/models/action.go +++ b/models/action.go @@ -351,7 +351,7 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, error) { sess = db.SetSessionPagination(sess, &opts) extActions := make([]*ExtAction, 0, opts.PageSize) - if err := sess.Desc("`action`.created_unix").Find(&extActions); err != nil { + if err := sess.Table("action").Desc("`action`.created_unix").Find(&extActions); err != nil { return nil, fmt.Errorf("Find: %v", err) } From 4a45c719c36509c9fb30e34c3c4eaee14b0898a3 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 9 May 2022 00:19:52 +0800 Subject: [PATCH 5/5] Fix bug --- models/action.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/action.go b/models/action.go index 7a7934c6e7c41..521de3ff5c3b4 100644 --- a/models/action.go +++ b/models/action.go @@ -329,8 +329,8 @@ type GetFeedsOptions struct { } type ExtAction struct { - Action - Repo *repo_model.Repository `xorm:"extends"` + Action `xorm:"extends"` + Repo *repo_model.Repository `xorm:"extends"` } // GetFeeds returns actions according to the provided options