|
|
|
|
@@ -6,7 +6,6 @@ import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strings"
|
|
|
|
|
"vctp/components/views"
|
|
|
|
|
"vctp/internal/report"
|
|
|
|
|
|
|
|
|
|
@@ -22,7 +21,7 @@ import (
|
|
|
|
|
// @Failure 500 {string} string "Server error"
|
|
|
|
|
// @Router /snapshots/hourly [get]
|
|
|
|
|
func (h *Handler) SnapshotHourlyList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
h.renderSnapshotList(w, r, "inventory_daily_", "Hourly Inventory Snapshots", views.SnapshotHourlyList)
|
|
|
|
|
h.renderSnapshotList(w, r, "hourly", "Hourly Inventory Snapshots", views.SnapshotHourlyList)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SnapshotDailyList renders the daily snapshot list page.
|
|
|
|
|
@@ -34,7 +33,7 @@ func (h *Handler) SnapshotHourlyList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// @Failure 500 {string} string "Server error"
|
|
|
|
|
// @Router /snapshots/daily [get]
|
|
|
|
|
func (h *Handler) SnapshotDailyList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
h.renderSnapshotList(w, r, "inventory_daily_summary_", "Daily Inventory Snapshots", views.SnapshotDailyList)
|
|
|
|
|
h.renderSnapshotList(w, r, "daily", "Daily Inventory Snapshots", views.SnapshotDailyList)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SnapshotMonthlyList renders the monthly snapshot list page.
|
|
|
|
|
@@ -46,7 +45,7 @@ func (h *Handler) SnapshotDailyList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// @Failure 500 {string} string "Server error"
|
|
|
|
|
// @Router /snapshots/monthly [get]
|
|
|
|
|
func (h *Handler) SnapshotMonthlyList(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
h.renderSnapshotList(w, r, "inventory_monthly_summary_", "Monthly Inventory Snapshots", views.SnapshotMonthlyList)
|
|
|
|
|
h.renderSnapshotList(w, r, "monthly", "Monthly Inventory Snapshots", views.SnapshotMonthlyList)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SnapshotReportDownload streams a snapshot table as XLSX.
|
|
|
|
|
@@ -91,28 +90,28 @@ func (h *Handler) SnapshotReportDownload(w http.ResponseWriter, r *http.Request)
|
|
|
|
|
w.Write(reportData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handler) renderSnapshotList(w http.ResponseWriter, r *http.Request, prefix string, title string, renderer func([]views.SnapshotEntry) templ.Component) {
|
|
|
|
|
func (h *Handler) renderSnapshotList(w http.ResponseWriter, r *http.Request, snapshotType string, title string, renderer func([]views.SnapshotEntry) templ.Component) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
tables, err := report.ListTablesByPrefix(ctx, h.Database, prefix)
|
|
|
|
|
if err := report.EnsureSnapshotRegistry(ctx, h.Database); err != nil {
|
|
|
|
|
h.Logger.Error("Failed to ensure snapshot registry", "error", err)
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
fmt.Fprintf(w, "Unable to list snapshot tables: %s\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
records, err := report.ListSnapshots(ctx, h.Database, snapshotType)
|
|
|
|
|
if err != nil {
|
|
|
|
|
h.Logger.Error("Failed to list snapshot tables", "error", err, "prefix", prefix)
|
|
|
|
|
h.Logger.Error("Failed to list snapshots", "error", err, "snapshot_type", snapshotType)
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
fmt.Fprintf(w, "Unable to list snapshot tables: %s\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entries := make([]views.SnapshotEntry, 0, len(tables))
|
|
|
|
|
for _, table := range tables {
|
|
|
|
|
if prefix == "inventory_daily_" && strings.HasPrefix(table, "inventory_daily_summary_") {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
label := table
|
|
|
|
|
if parsed, ok := report.FormatSnapshotLabel(prefix, table); ok {
|
|
|
|
|
label = parsed
|
|
|
|
|
}
|
|
|
|
|
entries := make([]views.SnapshotEntry, 0, len(records))
|
|
|
|
|
for _, record := range records {
|
|
|
|
|
label := report.FormatSnapshotLabel(snapshotType, record.SnapshotTime, record.TableName)
|
|
|
|
|
entries = append(entries, views.SnapshotEntry{
|
|
|
|
|
Label: label,
|
|
|
|
|
Link: "/api/report/snapshot?table=" + url.QueryEscape(table),
|
|
|
|
|
Link: "/api/report/snapshot?table=" + url.QueryEscape(record.TableName),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|