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 dashboard's repo list performance #18963

Merged
merged 18 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
40 changes: 32 additions & 8 deletions routers/api/v1/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@ func Search(ctx *context.APIContext) {
return
}

// Undocumented mode for internal usages, don't send
Gusted marked this conversation as resolved.
Show resolved Hide resolved
// any data only the count.
if ctx.FormBool("count_only") {
ctx.SetTotalCountHeader(count)
return
}

// Undocumented mode for internal usages, only return
// information that's useful for the dashboard's repo list.
minimalMode := ctx.FormBool("minimal")

results := make([]*api.Repository, len(repos))
for i, repo := range repos {
if err = repo.GetOwner(db.DefaultContext); err != nil {
Expand All @@ -224,16 +235,29 @@ func Search(ctx *context.APIContext) {
})
return
}
accessMode, err := models.AccessLevel(ctx.User, repo)
if err != nil {
ctx.JSON(http.StatusInternalServerError, api.SearchError{
OK: false,
Error: err.Error(),
})
if minimalMode {
results[i] = &api.Repository{
ID: repo.ID,
FullName: repo.FullName(),
Fork: repo.IsFork,
Private: repo.IsPrivate,
Template: repo.IsTemplate,
Mirror: repo.IsMirror,
Stars: repo.NumStars,
HTMLURL: repo.HTMLURL(),
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
}
} else {
accessMode, err := models.AccessLevel(ctx.User, repo)
if err != nil {
ctx.JSON(http.StatusInternalServerError, api.SearchError{
OK: false,
Error: err.Error(),
})
}
results[i] = convert.ToRepo(repo, accessMode)
}
results[i] = convert.ToRepo(repo, accessMode)
}

ctx.SetLinkHeader(int(count), opts.PageSize)
ctx.SetTotalCountHeader(count)
ctx.JSON(http.StatusOK, api.SearchResults{
Expand Down
48 changes: 27 additions & 21 deletions web_src/js/components/DashboardRepoList.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function initVueComponents() {
return this.repos.length > 0 && this.repos.length < this.counts[`${this.reposFilter}:${this.archivedFilter}:${this.privateFilter}`];
},
searchURL() {
return `${this.subUrl}/api/v1/repos/search?sort=updated&order=desc&uid=${this.uid}&team_id=${this.teamId}&q=${this.searchQuery
return `${this.subUrl}/api/v1/repos/search?sort=updated&order=desc&minimal=1&uid=${this.uid}&team_id=${this.teamId}&q=${this.searchQuery
Gusted marked this conversation as resolved.
Show resolved Hide resolved
}&page=${this.page}&limit=${this.searchLimit}&mode=${this.repoTypes[this.reposFilter].searchMode
}${this.reposFilter !== 'all' ? '&exclusive=1' : ''
}${this.archivedFilter === 'archived' ? '&archived=true' : ''}${this.archivedFilter === 'unarchived' ? '&archived=false' : ''
Expand Down Expand Up @@ -298,36 +298,42 @@ function initVueComponents() {
this.searchRepos();
},

searchRepos() {
async searchRepos() {
this.isLoading = true;

if (!this.reposTotalCount) {
const totalCountSearchURL = `${this.subUrl}/api/v1/repos/search?sort=updated&order=desc&uid=${this.uid}&team_id=${this.teamId}&q=&page=1&mode=`;
$.getJSON(totalCountSearchURL, (_result, _textStatus, request) => {
this.reposTotalCount = request.getResponseHeader('X-Total-Count');
});
}

const searchedMode = this.repoTypes[this.reposFilter].searchMode;
const searchedURL = this.searchURL;
const searchedQuery = this.searchQuery;

$.getJSON(searchedURL, (result, _textStatus, request) => {
if (searchedURL === this.searchURL) {
this.repos = result.data;
const count = request.getResponseHeader('X-Total-Count');
if (searchedQuery === '' && searchedMode === '' && this.archivedFilter === 'both') {
this.reposTotalCount = count;
}
Vue.set(this.counts, `${this.reposFilter}:${this.archivedFilter}:${this.privateFilter}`, count);
this.finalPage = Math.ceil(count / this.searchLimit);
this.updateHistory();
let json = {};
let response;
Gusted marked this conversation as resolved.
Show resolved Hide resolved
try {
if (!this.reposTotalCount) {
const totalCountSearchURL = `${this.subUrl}/api/v1/repos/search?sort=updated&count_only=1&order=desc&uid=${this.uid}&team_id=${this.teamId}&q=&page=1&mode=`;
const response = await fetch(totalCountSearchURL);
Gusted marked this conversation as resolved.
Show resolved Hide resolved
this.reposTotalCount = response.headers.get('X-Total-Count');
}
}).always(() => {

response = await fetch(searchedURL);
json = await response.json();
} catch {
if (searchedURL === this.searchURL) {
this.isLoading = false;
}
});
return;
Gusted marked this conversation as resolved.
Show resolved Hide resolved
}

if (searchedURL === this.searchURL) {
this.repos = json.data;
const count = response.headers.get('X-Total-Count');
if (searchedQuery === '' && searchedMode === '' && this.archivedFilter === 'both') {
this.reposTotalCount = count;
}
Vue.set(this.counts, `${this.reposFilter}:${this.archivedFilter}:${this.privateFilter}`, count);
this.finalPage = Math.ceil(count / this.searchLimit);
this.updateHistory();
this.isLoading = false;
}
},

repoIcon(repo) {
Expand Down