Monorepo for Tangled tangled.org
772
fork

Configure Feed

Select the types of activity you want to include in your feed.

appview: add pagination to stars page #1

open opened by pdewey.com targeting master

Performance of the stars page for repos with many stars is pretty bad, with the page fetching avatars and triggering DID resolution for all users shown on the page (~820 for tangled.org/core for example). This PR adds pagination to only show 30 users per page, and also only resolve those DIDs and avatars.

Sorry I didn't catch this when I implemented the stars page, I didn't have a test repo with enough stars to run into this (I also don't have enough test accounts)

Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:hm5f3dnm6jdhrc55qp2npdja/sh.tangled.repo.pull/3mk26vmz4ee22
+31 -5
Diff #0
+3 -2
appview/db/star.go
··· 52 52 return &star, nil 53 53 } 54 54 55 - func GetStars(e Execer, subjectAt syntax.ATURI) ([]models.Star, error) { 55 + func GetStars(e Execer, subjectAt syntax.ATURI, page pagination.Page) ([]models.Star, error) { 56 56 query := ` 57 57 select did, subject_at, created, rkey 58 58 from stars 59 59 where subject_at = ? 60 60 order by created desc 61 + limit ? offset ? 61 62 ` 62 - rows, err := e.Query(query, subjectAt) 63 + rows, err := e.Query(query, subjectAt, page.Limit, page.Offset) 63 64 if err != nil { 64 65 return nil, err 65 66 }
+2
appview/pages/pages.go
··· 1495 1495 RepoInfo repoinfo.RepoInfo 1496 1496 Active string 1497 1497 Starrers []models.Star 1498 + Page pagination.Page 1499 + TotalCount int 1498 1500 } 1499 1501 1500 1502 func (p *Pages) RepoStars(w io.Writer, params RepoStarsParams) error {
+10 -1
appview/pages/templates/repo/stars.html
··· 16 16 </div> 17 17 </div> 18 18 </div> 19 - {{ else }} 19 + {{ end }} 20 + {{ if eq .TotalCount 0 }} 20 21 <p class="text-gray-500 dark:text-gray-400 col-span-3">No stars yet.</p> 21 22 {{ end }} 22 23 </div> 24 + {{ if gt .TotalCount .Page.Limit }} 25 + {{ template "fragments/pagination" (dict 26 + "Page" .Page 27 + "TotalCount" .TotalCount 28 + "BasePath" (printf "/%s/stars" .RepoInfo.FullName) 29 + "QueryParams" (queryParams) 30 + ) }} 31 + {{ end }} 23 32 </div> 24 33 {{ end }}
+15 -1
appview/repo/repo.go
··· 21 21 "tangled.org/core/appview/notify" 22 22 "tangled.org/core/appview/oauth" 23 23 "tangled.org/core/appview/pages" 24 + "tangled.org/core/appview/pagination" 24 25 "tangled.org/core/appview/reporesolver" 25 26 "tangled.org/core/appview/validator" 26 27 xrpcclient "tangled.org/core/appview/xrpcclient" ··· 1270 1271 return 1271 1272 } 1272 1273 1273 - starrers, err := db.GetStars(rp.db, f.RepoAt()) 1274 + page := pagination.FromContext(r.Context()) 1275 + if page.Limit > 30 || page.Limit <= 0 { 1276 + page.Limit = 30 1277 + } 1278 + 1279 + starrers, err := db.GetStars(rp.db, f.RepoAt(), page) 1274 1280 if err != nil { 1275 1281 l.Error("failed to fetch starrers", "err", err, "repoAt", f.RepoAt()) 1276 1282 return 1277 1283 } 1278 1284 1285 + totalCount, err := db.GetStarCount(rp.db, f.RepoAt()) 1286 + if err != nil { 1287 + l.Error("failed to fetch star count", "err", err, "repoAt", f.RepoAt()) 1288 + return 1289 + } 1290 + 1279 1291 rp.pages.RepoStars(w, pages.RepoStarsParams{ 1280 1292 LoggedInUser: user, 1281 1293 RepoInfo: rp.repoResolver.GetRepoInfo(r, user), 1282 1294 Starrers: starrers, 1295 + Page: page, 1296 + TotalCount: totalCount, 1283 1297 }) 1284 1298 } 1285 1299
+1 -1
appview/repo/router.go
··· 47 47 // a file path 48 48 r.Get("/archive/{ref}", rp.DownloadArchive) 49 49 50 - r.Get("/stars", rp.Stars) 50 + r.With(middleware.Paginate).Get("/stars", rp.Stars) 51 51 52 52 r.Route("/fork", func(r chi.Router) { 53 53 r.Use(middleware.AuthMiddleware(rp.oauth))

History

1 round 0 comments
sign up or login to add to the discussion
pdewey.com submitted #0
1 commit
expand
d5e678a5
appview: add pagination to stars page
merge conflicts detected
expand
  • appview/db/star.go:52
  • appview/pages/pages.go:1495
  • appview/pages/templates/repo/stars.html:16
  • appview/repo/repo.go:21
  • appview/repo/router.go:47
expand 0 comments