From 5f7b6b55a5204ad074ec29a54945d7cdabccbce0 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 20 Mar 2024 11:02:35 +0800 Subject: [PATCH] Only do counting when count_only=true for repo dashboard (#29884) (#29905) Ref: #29878 Backport #29884 Co-authored-by: wxiaoguang --- routers/web/repo/repo.go | 24 ++++++++++++--------- web_src/js/components/DashboardRepoList.vue | 2 +- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 88cbd701d21..715ce34e64d 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -575,23 +575,27 @@ func SearchRepo(ctx *context.Context) { } } - var err error + // To improve performance when only the count is requested + if ctx.FormBool("count_only") { + if count, err := repo_model.CountRepository(ctx, opts); err != nil { + log.Error("CountRepository: %v", err) + ctx.JSON(http.StatusInternalServerError, nil) // frontend JS doesn't handle error response (same as below) + } else { + ctx.SetTotalCountHeader(count) + ctx.JSONOK() + } + return + } + repos, count, err := repo_model.SearchRepository(ctx, opts) if err != nil { - ctx.JSON(http.StatusInternalServerError, api.SearchError{ - OK: false, - Error: err.Error(), - }) + log.Error("SearchRepository: %v", err) + ctx.JSON(http.StatusInternalServerError, nil) return } ctx.SetTotalCountHeader(count) - // To improve performance when only the count is requested - if ctx.FormBool("count_only") { - return - } - // collect the latest commit of each repo // at most there are dozens of repos (limited by MaxResponseItems), so it's not a big problem at the moment repoBranchNames := make(map[int64]string, len(repos)) diff --git a/web_src/js/components/DashboardRepoList.vue b/web_src/js/components/DashboardRepoList.vue index f7cea32fb63..11ca6a6ac7c 100644 --- a/web_src/js/components/DashboardRepoList.vue +++ b/web_src/js/components/DashboardRepoList.vue @@ -236,7 +236,7 @@ const sfc = { if (!this.reposTotalCount) { const totalCountSearchURL = `${this.subUrl}/repo/search?count_only=1&uid=${this.uid}&team_id=${this.teamId}&q=&page=1&mode=`; response = await GET(totalCountSearchURL); - this.reposTotalCount = response.headers.get('X-Total-Count'); + this.reposTotalCount = response.headers.get('X-Total-Count') ?? '?'; } response = await GET(searchedURL);