Precalculate index stats
This commit is contained in:
@@ -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
41
web/indexHandler.go
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user