Precalculate index stats

This commit is contained in:
Pijus Kamandulis
2025-06-23 17:52:20 +03:00
parent d801debaf6
commit be637f4540
13 changed files with 243 additions and 81 deletions

View File

@@ -1,19 +0,0 @@
package web
import (
"html/template"
"net/http"
"pool-stats/stats"
)
func (ws *WebServer) IndexHandler(w http.ResponseWriter, r *http.Request) {
shareStats, err := stats.GetStats(ws.db)
if err != nil {
http.Error(w, "Failed to load stats", 500)
return
}
tmpl := template.Must(template.ParseFiles("templates/index.html"))
tmpl.Execute(w, shareStats)
}

41
web/indexHandler.go Normal file
View File

@@ -0,0 +1,41 @@
package web
import (
"html/template"
"net/http"
"pool-stats/database"
"pool-stats/helpers"
"pool-stats/models"
)
type IndexPageData struct {
Stats []models.TimeWindowHighShare
}
func (ws *WebServer) IndexHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.New("index").Funcs(template.FuncMap{
"humanDiff": helpers.HumanDiff,
"formatCreateDate": helpers.FormatCreateDate,
})
tmpl, err := tmpl.ParseFiles("templates/index.html")
if err != nil {
http.Error(w, "Failed to load template", 500)
return
}
tws := database.GetTimeWindowHighShares(ws.db)
if tws == nil {
http.Error(w, "Failed to load time window high shares", 500)
return
}
indexData := IndexPageData{
Stats: tws,
}
if err := tmpl.Execute(w, indexData); err != nil {
http.Error(w, "Failed to render template", 500)
println("Error rendering template:", err.Error())
return
}
}