Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of dashboard list orgs #16099

Merged
merged 6 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,25 @@ func GetOrgsByUserID(userID int64, showAll bool) ([]*User, error) {
return getOrgsByUserID(sess, userID, showAll)
}

// queryUserOrgIDs returns a condition to return user's organization id
func queryUserOrgIDs(uid int64) *builder.Builder {
return builder.Select("team.org_id").
From("team_user").InnerJoin("team", "team.id = team_user.team_id").
Where(builder.Eq{"team_user.uid": uid})
}

// SimpleOrg represents a simple orgnization with only needed columns
type SimpleOrg = User
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just use User directly?
for later refactors?!?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To notice that the struct only have part fields filled. If we don't add this, we cannot differ the two Users.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesnt it make more sence then to create a new struct type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried that, but we have to copy some functions from User.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why most Java design moved to use POJOs for data with functionality moved to Services eg UserService to do actions on that data.

Our data structs are too powerful & ultimately too tightly bound to the db interface. modules/git is another example and is why creating a second backend was so so hard.

For the moment don't call this "Simple" call it MinimalUser or something like that.

6543 marked this conversation as resolved.
Show resolved Hide resolved

// GetUserOrgsList returns one user's all orgs list
func GetUserOrgsList(uid int64) ([]*SimpleOrg, error) {
6543 marked this conversation as resolved.
Show resolved Hide resolved
var orgs = make([]*SimpleOrg, 0, 20)
6543 marked this conversation as resolved.
Show resolved Hide resolved
return orgs, x.Select("id, name, full_name, visibility, avatar, avatar_email, use_custom_avatar").
Table("user").
In("id", queryUserOrgIDs(uid)).
Find(&orgs)
}

func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) {
orgs := make([]*User, 0, 10)
return orgs, sess.
Expand Down
7 changes: 4 additions & 3 deletions routers/web/user/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ func getDashboardContextUser(ctx *context.Context) *models.User {
}
ctx.Data["ContextUser"] = ctxUser

if err := ctx.User.GetOrganizations(&models.SearchOrganizationsOptions{All: true}); err != nil {
ctx.ServerError("GetOrganizations", err)
orgs, err := models.GetUserOrgsList(ctx.User.ID)
if err != nil {
ctx.ServerError("GetUserOrgsList", err)
return nil
}
ctx.Data["Orgs"] = ctx.User.Orgs
ctx.Data["Orgs"] = orgs

return ctxUser
}
Expand Down