Files
veola/internal/handlers/results.go
prosolis d87536c879 Fix bugs found in local testing
- Dashboard auto-refresh rendered the full layout into its own
  refresh container, producing a duplicate sidebar every 60s; it now
  renders only the body partial.
- 'Run Now' runs synchronously with a bounded timeout and returns
  refreshed results plus success/error feedback, instead of
  firing-and-forgetting with no signal.
- Price-history chart data moved from a <script> block to a data-
  attribute: templ does not interpolate expressions inside <script>
  element content, so the JSON was emitted literally.
- The htmx indicator spinner was permanently visible due to CSS
  source order; the indicator rules now follow .v-spinner.

Also refreshes README for this session's changes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 12:11:07 -07:00

150 lines
3.7 KiB
Go

package handlers
import (
"net/http"
"strconv"
"strings"
"time"
"veola/internal/db"
"veola/internal/models"
"veola/internal/scheduler"
"veola/templates"
)
const resultsPerPage = 20
func (a *App) GetItemResults(w http.ResponseWriter, r *http.Request) {
id := intParam(r, "id")
it, err := a.Store.GetItem(r.Context(), id)
if err != nil || it == nil {
http.NotFound(w, r)
return
}
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
d, err := a.buildItemResultsData(r, it, page, r.URL.Query().Get("order"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
render(w, r, templates.ItemResults(d))
}
// buildItemResultsData assembles the per-item results view: paginated results,
// price history, badge, and chart JSON. Shared by GetItemResults and the
// "Run Now" handler so both render identical data.
func (a *App) buildItemResultsData(r *http.Request, it *models.Item, page int, order string) (templates.ItemResultsData, error) {
if order == "" {
order = "found_desc"
}
if page < 1 {
page = 1
}
total, err := a.Store.CountResults(r.Context(), it.ID)
if err != nil {
return templates.ItemResultsData{}, err
}
totalPages := (total + resultsPerPage - 1) / resultsPerPage
if totalPages < 1 {
totalPages = 1
}
if page > totalPages {
page = totalPages
}
results, err := a.Store.ListResults(r.Context(), db.ResultsQuery{
ItemID: it.ID,
Limit: resultsPerPage,
Offset: (page - 1) * resultsPerPage,
Order: order,
})
if err != nil {
return templates.ItemResultsData{}, err
}
history, err := a.Store.ListPriceHistory(r.Context(), it.ID)
if err != nil {
return templates.ItemResultsData{}, err
}
return templates.ItemResultsData{
Page: a.page(r, it.Name, "items"),
Item: *it,
Badge: scheduler.PickBadge(*it, history, time.Now()),
History: history,
Results: results,
Page_: page,
TotalPages: totalPages,
Order: order,
HistoryChartJSON: buildChartJSON(history),
}, nil
}
func buildChartJSON(history []models.PricePoint) string {
c := templates.ChartJSON{
Labels: make([]string, 0, len(history)),
Points: make([]float64, 0, len(history)),
}
for _, p := range history {
c.Labels = append(c.Labels, p.PolledAt.Format("2006-01-02"))
c.Points = append(c.Points, p.Price)
}
return templates.MustChartJSON(c)
}
func (a *App) GetGlobalResults(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
itemID, _ := strconv.ParseInt(q.Get("item_id"), 10, 64)
from := strings.TrimSpace(q.Get("from"))
to := strings.TrimSpace(q.Get("to"))
items, err := a.Store.ListItems(r.Context())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
names := make(map[int64]string, len(items))
for _, it := range items {
names[it.ID] = it.Name
}
results, err := a.Store.ListResults(r.Context(), db.ResultsQuery{
ItemID: itemID,
Limit: 200,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fromT, _ := time.Parse("2006-01-02", from)
toT, _ := time.Parse("2006-01-02", to)
if !toT.IsZero() {
toT = toT.Add(24 * time.Hour)
}
rows := make([]templates.ItemResultRow, 0, len(results))
for _, res := range results {
if !fromT.IsZero() && res.FoundAt.Before(fromT) {
continue
}
if !toT.IsZero() && !res.FoundAt.Before(toT) {
continue
}
rows = append(rows, templates.ItemResultRow{
Result: res,
ItemName: names[res.ItemID],
})
}
render(w, r, templates.GlobalResults(templates.GlobalResultsData{
Page: a.page(r, "Results", "results"),
Items: items,
Results: rows,
ItemID: itemID,
From: from,
To: to,
}))
}