summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2024-01-09 23:22:16 +0100
committerAki <please@ignore.pl>2024-01-09 23:22:57 +0100
commit4c7bd2da2a7c0e01f9c7a70f46b2d9b9f575b49b (patch)
treea5248197e5f7d09fff65a726e757c4ca192b4da5
parentafc212f5dc1a70d35b3240589a55db7912c45f02 (diff)
downloadrudone-4c7bd2da2a7c0e01f9c7a70f46b2d9b9f575b49b.zip
rudone-4c7bd2da2a7c0e01f9c7a70f46b2d9b9f575b49b.tar.gz
rudone-4c7bd2da2a7c0e01f9c7a70f46b2d9b9f575b49b.tar.bz2
Created simple dashboard in the landing page
-rw-r--r--entry.go5
-rw-r--r--index.html.in6
-rw-r--r--main.go29
3 files changed, 36 insertions, 4 deletions
diff --git a/entry.go b/entry.go
index 04099ed..0d434e2 100644
--- a/entry.go
+++ b/entry.go
@@ -75,3 +75,8 @@ func ListEntries(from, count uint) (entries []Entry, err error) {
err = db.Select(&entries, "SELECT * FROM entries LIMIT ? OFFSET ?;", count, from)
return
}
+
+func EntriesSince(since time.Time) (entries []Entry, err error) {
+ err = db.Select(&entries, "SELECT * FROM entries WHERE started_at > ?;", since)
+ return
+}
diff --git a/index.html.in b/index.html.in
index ee76fdb..3d813ad 100644
--- a/index.html.in
+++ b/index.html.in
@@ -3,4 +3,8 @@
<meta charset="utf-8">
<title>rudone</title>
<h1>rudone &mdash; <span style="color: green">OK</span></h1>
-<p>{{.}} entries
+<table>
+{{range .Stats}}
+<tr><th>{{.Title}}<td>{{.Amount}} visits
+{{end}}
+</table>
diff --git a/main.go b/main.go
index 73de3e1..f5129ce 100644
--- a/main.go
+++ b/main.go
@@ -8,6 +8,7 @@ import (
"net/http"
"os"
"strconv"
+ "time"
"github.com/gorilla/mux"
)
@@ -74,15 +75,37 @@ func handleEntryGet(w http.ResponseWriter, r *http.Request) {
}
}
+type Stat struct {
+ Title string
+ Amount uint
+}
+
+type Home struct {
+ Stats []Stat
+}
+
func handleHome(pathname string) func(http.ResponseWriter, *http.Request) {
t := template.Must(template.ParseFS(content, pathname))
return func(w http.ResponseWriter, r *http.Request) {
- count, err := CountEntries()
- if err != nil {
+ var home Home
+ 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))})
+ }
+ 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))})
+ }
+ total, err := CountEntries()
+ if err == nil {
+ home.Stats = append(home.Stats, Stat{"Total", total})
+ }
+ if len(home.Stats) < 1 {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
- t.Execute(w, count)
+ t.Execute(w, home)
}
}