summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2024-01-10 00:05:26 +0100
committerAki <please@ignore.pl>2024-01-10 00:05:26 +0100
commit6fe3e5394e2e6736222ce85c7b5afbadffda5b82 (patch)
tree847b919c489ca277e576b2ba9b3b6d5eba6d94c1
parent0f857271eac9995b90e16f58181d29a0bd6a47b6 (diff)
downloadrudone-6fe3e5394e2e6736222ce85c7b5afbadffda5b82.zip
rudone-6fe3e5394e2e6736222ce85c7b5afbadffda5b82.tar.gz
rudone-6fe3e5394e2e6736222ce85c7b5afbadffda5b82.tar.bz2
Added first visit date to the dashboard
-rw-r--r--entry.go5
-rw-r--r--index.html.in2
-rw-r--r--main.go21
3 files changed, 21 insertions, 7 deletions
diff --git a/entry.go b/entry.go
index 0d434e2..44ca42d 100644
--- a/entry.go
+++ b/entry.go
@@ -80,3 +80,8 @@ func EntriesSince(since time.Time) (entries []Entry, err error) {
err = db.Select(&entries, "SELECT * FROM entries WHERE started_at > ?;", since)
return
}
+
+func FirstEntry() (entry Entry, err error) {
+ err = db.Get(&entry, "SELECT * FROM entries ORDER BY started_at ASC LIMIT 1;")
+ return
+}
diff --git a/index.html.in b/index.html.in
index 9c1fbc3..53d0402 100644
--- a/index.html.in
+++ b/index.html.in
@@ -7,6 +7,6 @@
<h1>rudone &mdash; <span class="ok">OK</span></h1>
<table>
{{range .Stats}}
-<tr><th>{{.Title}}<td>{{.Amount}} visits
+<tr><th>{{.Title}}<td>{{.Description}}
{{end}}
</table>
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)