This commit is contained in:
2026-03-22 22:04:38 +01:00
parent 75d7464655
commit 97d4a990a8
8 changed files with 149 additions and 92 deletions
+40 -7
View File
@@ -5,23 +5,42 @@ import (
"fmt"
"html/template"
"net/http"
"github.com/sirupsen/logrus"
)
func (s *State) profilesHandler(w http.ResponseWriter, r *http.Request) {
data, _ := s.getLatest()
data, err := s.getLatest()
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
if err := json.NewEncoder(w).Encode(data); err != nil {
logrus.Errorf("Encode profiles failed: %v", err)
}
}
func (s *State) historyHandler(w http.ResponseWriter, r *http.Request) {
owner := r.URL.Query().Get("owner")
rows, _ := s.db.Query(`SELECT u.display_name, ph.timestamp, ph.rank, ph.upload, ph.current_upload, ph.current_download, ph.points, ph.seeding_count FROM profile_history ph JOIN users u ON ph.user_id = u.id WHERE u.display_name = ? ORDER BY ph.timestamp ASC`, owner)
if owner == "" {
http.Error(w, "Owner required", http.StatusBadRequest)
return
}
rows, err := s.db.Query(`SELECT u.display_name, ph.timestamp, ph.rank, ph.upload, ph.current_upload, ph.current_download, ph.points, ph.seeding_count FROM profile_history ph JOIN users u ON ph.user_id = u.id WHERE u.display_name = ? ORDER BY ph.timestamp ASC`, owner)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
defer rows.Close()
var history []ProfileData
for rows.Next() {
var p ProfileData
rows.Scan(&p.Owner, &p.Timestamp, &p.Rank, &p.Upload, &p.CurrentUpload, &p.CurrentDownload, &p.Points, &p.SeedingCount)
if err := rows.Scan(&p.Owner, &p.Timestamp, &p.Rank, &p.Upload, &p.CurrentUpload, &p.CurrentDownload, &p.Points, &p.SeedingCount); err != nil {
logrus.Errorf("Scan history failed: %v", err)
continue
}
history = append(history, p)
}
w.Header().Set("Content-Type", "application/json")
@@ -30,6 +49,10 @@ func (s *State) historyHandler(w http.ResponseWriter, r *http.Request) {
func (s *State) historyModalHandler(w http.ResponseWriter, r *http.Request) {
owner := r.URL.Query().Get("owner")
if owner == "" {
http.Error(w, "Owner required", http.StatusBadRequest)
return
}
fmt.Fprintf(w, `<div id="chart-data-container" data-owner="%s" x-init="renderChart('%s')"></div>`, owner, owner)
}
@@ -38,7 +61,17 @@ func (s *State) rootHandler(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
latest, _ := s.getLatest()
tmpl, _ := template.ParseFiles("web/index.html")
tmpl.Execute(w, struct{ Profiles []ProfileData }{latest})
latest, err := s.getLatest()
if err != nil {
logrus.Errorf("Get latest failed: %v", err)
}
tmpl, err := template.ParseFiles("web/index.html")
if err != nil {
logrus.Errorf("Template parse failed: %v", err)
http.Error(w, "Template Error", http.StatusInternalServerError)
return
}
if err := tmpl.Execute(w, struct{ Profiles []ProfileData }{latest}); err != nil {
logrus.Errorf("Template execute failed: %v", err)
}
}