From 7db030d6ebbc6d0200701b6c3dd2de30ca4f6cce Mon Sep 17 00:00:00 2001 From: John D Date: Sun, 28 Sep 2025 10:54:33 -0500 Subject: [PATCH 1/4] Implement pagination for network results in viewNetworks and update template --- main.go | 40 ++++++++++++++++++++++++++++++++++------ templates/networks.tmpl | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 72 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 37dea84..eeb94e3 100644 --- a/main.go +++ b/main.go @@ -1427,13 +1427,31 @@ func getNetworkCounts(networks []db.Network) map[uint]uint64 { func viewNetworks(c *gin.Context) { var networks []db.Network + var total int64 var err error + + // Pagination parameters + page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) + if page < 1 { + page = 1 + } + pageSize := 100 + offset := (page - 1) * pageSize + run := c.Param("run") run = strings.TrimPrefix(run, "/") + + // Get total count and paginated results if run == "" { - err = db.GetDB().Order("id desc").Find(&networks).Error + err = db.GetDB().Model(&db.Network{}).Count(&total).Error + if err == nil { + err = db.GetDB().Order("id desc").Offset(offset).Limit(pageSize).Find(&networks).Error + } } else { - err = db.GetDB().Order("id desc").Where("training_run_id = ?", run).Find(&networks).Error + err = db.GetDB().Model(&db.Network{}).Where("training_run_id = ?", run).Count(&total).Error + if err == nil { + err = db.GetDB().Order("id desc").Where("training_run_id = ?", run).Offset(offset).Limit(pageSize).Find(&networks).Error + } } if err != nil { log.Println(err) @@ -1459,9 +1477,6 @@ func viewNetworks(c *gin.Context) { counts := getNetworkCounts(networks) json := []gin.H{} - if c.DefaultQuery("show_all", "1") == "0" { - networks = networks[0:99] - } for _, network := range networks { json = append(json, gin.H{ "id": network.ID, @@ -1478,8 +1493,21 @@ func viewNetworks(c *gin.Context) { }) } + // Calculate pagination info + totalPages := int((total + int64(pageSize) - 1) / int64(pageSize)) + hasPrev := page > 1 + hasNext := page < totalPages + c.HTML(http.StatusOK, "networks", gin.H{ - "networks": json, + "networks": json, + "page": page, + "totalPages": totalPages, + "total": total, + "hasPrev": hasPrev, + "hasNext": hasNext, + "prevPage": page - 1, + "nextPage": page + 1, + "run": run, }) } diff --git a/templates/networks.tmpl b/templates/networks.tmpl index 4924aa3..a2d29ad 100755 --- a/templates/networks.tmpl +++ b/templates/networks.tmpl @@ -31,9 +31,45 @@ {{end}} - show all networks (warning: large page) + + + + +
+ Showing {{len .networks}} networks per page out of {{.total}} networks. +
{{end}} {{define "scripts"}} -{{end}} +{{end}} \ No newline at end of file From 500538db1048115e39d369174aa470090579527b Mon Sep 17 00:00:00 2001 From: John D Date: Sun, 28 Sep 2025 11:02:32 -0500 Subject: [PATCH 2/4] Give matches page pagination too --- main.go | 40 ++++++++++++++++++++++++++++++++++------ templates/matches.tmpl | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index eeb94e3..46d4e87 100644 --- a/main.go +++ b/main.go @@ -1606,16 +1606,31 @@ func viewStats(c *gin.Context) { func viewMatches(c *gin.Context) { var matches []db.Match + var total int64 var err error + + // Pagination parameters + page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) + if page < 1 { + page = 1 + } + pageSize := 100 + offset := (page - 1) * pageSize + run := c.Param("run") run = strings.TrimPrefix(run, "/") + + // Get total count and paginated results for matches if run == "" { - err = db.GetDB().Order("id desc").Find(&matches).Error + err = db.GetDB().Model(&db.Match{}).Count(&total).Error + if err == nil { + err = db.GetDB().Order("id desc").Offset(offset).Limit(pageSize).Find(&matches).Error + } } else { - err = db.GetDB().Order("id desc").Where("training_run_id = ?", run).Find(&matches).Error - } - if c.DefaultQuery("show_all", "1") == "0" { - matches = matches[0:99] + err = db.GetDB().Model(&db.Match{}).Where("training_run_id = ?", run).Count(&total).Error + if err == nil { + err = db.GetDB().Order("id desc").Where("training_run_id = ?", run).Offset(offset).Limit(pageSize).Find(&matches).Error + } } if err != nil { log.Println(err) @@ -1684,8 +1699,21 @@ func viewMatches(c *gin.Context) { }) } + // Calculate pagination info + totalPages := int((total + int64(pageSize) - 1) / int64(pageSize)) + hasPrev := page > 1 + hasNext := page < totalPages + c.HTML(http.StatusOK, "matches", gin.H{ - "matches": json, + "matches": json, + "page": page, + "totalPages": totalPages, + "total": total, + "hasPrev": hasPrev, + "hasNext": hasNext, + "prevPage": page - 1, + "nextPage": page + 1, + "run": run, }) } diff --git a/templates/matches.tmpl b/templates/matches.tmpl index 15d5cc5..6bf2d62 100755 --- a/templates/matches.tmpl +++ b/templates/matches.tmpl @@ -33,7 +33,43 @@ {{end}} - show all matches (warning: large page) + + + + +
+ Showing {{len .matches}} matches per page out of {{.total}} matches. +
{{end}} From c23c7e5ab2e6effe18129e6d2166812a2d30de6a Mon Sep 17 00:00:00 2001 From: John D Date: Sun, 28 Sep 2025 11:16:36 -0500 Subject: [PATCH 3/4] Fix pagination when run is specified --- templates/matches.tmpl | 12 ++++++++++-- templates/networks.tmpl | 12 ++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/templates/matches.tmpl b/templates/matches.tmpl index 6bf2d62..bb8e022 100755 --- a/templates/matches.tmpl +++ b/templates/matches.tmpl @@ -39,7 +39,11 @@
    {{if .hasPrev}}
  • - + {{if .run}} + + {{else}} + + {{end}}
  • @@ -55,7 +59,11 @@ {{if .hasNext}}
  • - + {{if .run}} + + {{else}} + + {{end}}
  • diff --git a/templates/networks.tmpl b/templates/networks.tmpl index a2d29ad..9e77db7 100755 --- a/templates/networks.tmpl +++ b/templates/networks.tmpl @@ -37,7 +37,11 @@
      {{if .hasPrev}}
    • - + {{if .run}} + + {{else}} + + {{end}}
    • @@ -53,7 +57,11 @@ {{if .hasNext}}
    • - + {{if .run}} + + {{else}} + + {{end}}
    • From 3458fe8a5f3bf41e1fc8b016562ff72ba75eb011 Mon Sep 17 00:00:00 2001 From: John D Date: Sun, 28 Sep 2025 11:59:00 -0500 Subject: [PATCH 4/4] Center pagination on page --- templates/matches.tmpl | 4 ++-- templates/networks.tmpl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/templates/matches.tmpl b/templates/matches.tmpl index bb8e022..9d0f35a 100755 --- a/templates/matches.tmpl +++ b/templates/matches.tmpl @@ -36,7 +36,7 @@ -
      +
      Showing {{len .matches}} matches per page out of {{.total}} matches.
      diff --git a/templates/networks.tmpl b/templates/networks.tmpl index 9e77db7..855ff7d 100755 --- a/templates/networks.tmpl +++ b/templates/networks.tmpl @@ -34,7 +34,7 @@ -
      +
      Showing {{len .networks}} networks per page out of {{.total}} networks.