From 6fe3e5394e2e6736222ce85c7b5afbadffda5b82 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 10 Jan 2024 00:05:26 +0100 Subject: Added first visit date to the dashboard --- main.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 6b86223..011e8c4 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "embed" "encoding/json" + "fmt" "html/template" "log" "net/http" @@ -76,14 +77,18 @@ func handleEntryGet(w http.ResponseWriter, r *http.Request) { } type Stat struct { - Title string - Amount uint + Title string + Description string } type Home struct { Stats []Stat } +func count(title string, amount int) Stat { + return Stat{title, fmt.Sprintf("%d visits", amount)} +} + func handleHome(pathname string) func(http.ResponseWriter, *http.Request) { t := template.Must(template.ParseFS(content, pathname)) return func(w http.ResponseWriter, r *http.Request) { @@ -91,15 +96,19 @@ func handleHome(pathname string) func(http.ResponseWriter, *http.Request) { now := time.Now().UTC() last_day, err := EntriesSince(now.AddDate(0, 0, -1)) if err == nil { - home.Stats = append(home.Stats, Stat{"Last 24 hours", uint(len(last_day))}) + home.Stats = append(home.Stats, count("Last 24 hours", len(last_day))) } last_month, err := EntriesSince(now.AddDate(0, 0, -30)) if err == nil { - home.Stats = append(home.Stats, Stat{"Last 30 days", uint(len(last_month))}) + home.Stats = append(home.Stats, count("Last 30 days", len(last_month))) + } + total, err := CountEntries() + if err == nil { + home.Stats = append(home.Stats, count("Total", int(total))) } - total, err := CountEntries() + first, err := FirstEntry() if err == nil { - home.Stats = append(home.Stats, Stat{"Total", total}) + home.Stats = append(home.Stats, Stat{"First visit", first.StartedAt.Format("_2 January 2006, 15:04")}) } if len(home.Stats) < 1 { http.Error(w, err.Error(), http.StatusInternalServerError) -- cgit v1.1