Skip to content

Commit 076eaad

Browse files
Gusteddelvhwxiaoguangzeripath
authored
Improve dashboard's repo list performance (#18963)
* Improve dashboard's repo list performance - Avoid a lot of database lookups for all the repo's, by adding a undocumented "minimal" mode for this specific task, which returns the data that's only needed by this list which doesn't require any database lookups. - Makes fetching these list faster. - Less CPU overhead when a user visits home page. * Refactor javascript code + fix Fork icon - Use async in the function so we can use `await`. - Remove `archivedFilter` check for count, as it doesn't make sense to show the count of repos when you can't even see them(as they are filited away). * Add `count_only` * Remove uncessary code * Improve comment Co-authored-by: delvh <[email protected]> * Update web_src/js/components/DashboardRepoList.js Co-authored-by: delvh <[email protected]> * Update web_src/js/components/DashboardRepoList.js Co-authored-by: delvh <[email protected]> * By default apply minimal mode * Remove `minimal` paramater * Refactor count header * Simplify init Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: delvh <[email protected]> Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent 89eec15 commit 076eaad

File tree

3 files changed

+42
-36
lines changed

3 files changed

+42
-36
lines changed

routers/api/v1/repo/repo.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ func Search(ctx *context.APIContext) {
218218
}
219219
results[i] = convert.ToRepo(repo, accessMode)
220220
}
221-
222221
ctx.SetLinkHeader(int(count), opts.PageSize)
223222
ctx.SetTotalCountHeader(count)
224223
ctx.JSON(http.StatusOK, api.SearchResults{

routers/web/repo/repo.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -590,26 +590,28 @@ func SearchRepo(ctx *context.Context) {
590590
return
591591
}
592592

593+
ctx.SetTotalCountHeader(count)
594+
595+
// To improve performance when only the count is requested
596+
if ctx.FormBool("count_only") {
597+
return
598+
}
599+
593600
results := make([]*api.Repository, len(repos))
594601
for i, repo := range repos {
595-
if err = repo.GetOwner(ctx); err != nil {
596-
ctx.JSON(http.StatusInternalServerError, api.SearchError{
597-
OK: false,
598-
Error: err.Error(),
599-
})
600-
return
601-
}
602-
accessMode, err := models.AccessLevel(ctx.Doer, repo)
603-
if err != nil {
604-
ctx.JSON(http.StatusInternalServerError, api.SearchError{
605-
OK: false,
606-
Error: err.Error(),
607-
})
602+
results[i] = &api.Repository{
603+
ID: repo.ID,
604+
FullName: repo.FullName(),
605+
Fork: repo.IsFork,
606+
Private: repo.IsPrivate,
607+
Template: repo.IsTemplate,
608+
Mirror: repo.IsMirror,
609+
Stars: repo.NumStars,
610+
HTMLURL: repo.HTMLURL(),
611+
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
608612
}
609-
results[i] = convert.ToRepo(repo, accessMode)
610613
}
611614

612-
ctx.SetTotalCountHeader(count)
613615
ctx.JSON(http.StatusOK, api.SearchResults{
614616
OK: true,
615617
Data: results,

web_src/js/components/DashboardRepoList.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -298,36 +298,41 @@ function initVueComponents() {
298298
this.searchRepos();
299299
},
300300

301-
searchRepos() {
301+
async searchRepos() {
302302
this.isLoading = true;
303303

304-
if (!this.reposTotalCount) {
305-
const totalCountSearchURL = `${this.subUrl}/repo/search?sort=updated&order=desc&uid=${this.uid}&team_id=${this.teamId}&q=&page=1&mode=`;
306-
$.getJSON(totalCountSearchURL, (_result, _textStatus, request) => {
307-
this.reposTotalCount = request.getResponseHeader('X-Total-Count');
308-
});
309-
}
310-
311304
const searchedMode = this.repoTypes[this.reposFilter].searchMode;
312305
const searchedURL = this.searchURL;
313306
const searchedQuery = this.searchQuery;
314307

315-
$.getJSON(searchedURL, (result, _textStatus, request) => {
316-
if (searchedURL === this.searchURL) {
317-
this.repos = result.data;
318-
const count = request.getResponseHeader('X-Total-Count');
319-
if (searchedQuery === '' && searchedMode === '' && this.archivedFilter === 'both') {
320-
this.reposTotalCount = count;
321-
}
322-
Vue.set(this.counts, `${this.reposFilter}:${this.archivedFilter}:${this.privateFilter}`, count);
323-
this.finalPage = Math.ceil(count / this.searchLimit);
324-
this.updateHistory();
308+
let response, json;
309+
try {
310+
if (!this.reposTotalCount) {
311+
const totalCountSearchURL = `${this.subUrl}/repo/search?count_only=1&uid=${this.uid}&team_id=${this.teamId}&q=&page=1&mode=`;
312+
response = await fetch(totalCountSearchURL);
313+
this.reposTotalCount = response.headers.get('X-Total-Count');
325314
}
326-
}).always(() => {
315+
316+
response = await fetch(searchedURL);
317+
json = await response.json();
318+
} catch {
327319
if (searchedURL === this.searchURL) {
328320
this.isLoading = false;
329321
}
330-
});
322+
return;
323+
}
324+
325+
if (searchedURL === this.searchURL) {
326+
this.repos = json.data;
327+
const count = response.headers.get('X-Total-Count');
328+
if (searchedQuery === '' && searchedMode === '' && this.archivedFilter === 'both') {
329+
this.reposTotalCount = count;
330+
}
331+
Vue.set(this.counts, `${this.reposFilter}:${this.archivedFilter}:${this.privateFilter}`, count);
332+
this.finalPage = Math.ceil(count / this.searchLimit);
333+
this.updateHistory();
334+
this.isLoading = false;
335+
}
331336
},
332337

333338
repoIcon(repo) {

0 commit comments

Comments
 (0)