From 25f91712236ccfaaccd83f9c3dcc088870d5ea09 Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Wed, 28 Feb 2024 14:32:28 -0500 Subject: [PATCH] web: add stats page --- bot/bot.go | 5 ++- bot/handlers.go | 3 ++ bot/mock.go | 3 +- bot/stats/stats.go | 19 ++++++++++ bot/web/index.templ | 29 ++++++++++++++- bot/web/index_templ.go | 81 +++++++++++++++++++++++++++++++++++++----- bot/web/web.go | 7 ++-- 7 files changed, 134 insertions(+), 13 deletions(-) create mode 100644 bot/stats/stats.go diff --git a/bot/bot.go b/bot/bot.go index 7441945..71502a4 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -4,6 +4,7 @@ package bot import ( "fmt" + "github.com/velour/catbase/bot/stats" "github.com/velour/catbase/bot/web" "math/rand" "os" @@ -63,6 +64,7 @@ type bot struct { quiet bool history *history.History + stats *stats.Stats } // Variable represents a $var replacement @@ -99,6 +101,7 @@ func New(config *config.Config, connector Connector) Bot { filters: make(map[string]func(string) string), callbacks: make(CallbackMap), history: history.New(historySz), + stats: stats.New(), } bot.migrateDB() @@ -106,7 +109,7 @@ func New(config *config.Config, connector Connector) Bot { bot.RefreshPluginBlacklist() bot.RefreshPluginWhitelist() - bot.web = web.New(bot.config) + bot.web = web.New(bot.config, bot.stats) connector.RegisterEvent(bot.Receive) diff --git a/bot/handlers.go b/bot/handlers.go index 2e202de..69e39dd 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -25,6 +25,8 @@ func (b *bot) Receive(conn Connector, kind Kind, msg msg.Message, args ...any) b // msg := b.buildMessage(client, inMsg) // do need to look up user and fix it + b.stats.MessagesRcv++ + if kind == Edit { b.history.Edit(msg.ID, &msg) } else { @@ -88,6 +90,7 @@ func (b *bot) Send(conn Connector, kind Kind, args ...any) (string, error) { if b.quiet { return "", nil } + b.stats.MessagesSent++ return conn.Send(kind, args...) } diff --git a/bot/mock.go b/bot/mock.go index 9cdb757..e34e8a1 100644 --- a/bot/mock.go +++ b/bot/mock.go @@ -4,6 +4,7 @@ package bot import ( "fmt" + "github.com/velour/catbase/bot/stats" "github.com/velour/catbase/bot/web" "net/http" "regexp" @@ -119,7 +120,7 @@ func NewMockBot() *MockBot { Messages: make([]string, 0), Actions: make([]string, 0), } - b.web = web.New(cfg) + b.web = web.New(cfg, stats.New()) // If any plugin registered a route, we need to reset those before any new test http.DefaultServeMux = new(http.ServeMux) return &b diff --git a/bot/stats/stats.go b/bot/stats/stats.go new file mode 100644 index 0000000..3ca919a --- /dev/null +++ b/bot/stats/stats.go @@ -0,0 +1,19 @@ +package stats + +import ( + "time" +) + +type Stats struct { + MessagesSent int + MessagesRcv int + startTime time.Time +} + +func New() *Stats { + return &Stats{startTime: time.Now()} +} + +func (s Stats) Uptime() time.Duration { + return time.Now().Sub(s.startTime) +} diff --git a/bot/web/index.templ b/bot/web/index.templ index b6c9f8a..f6d6a29 100644 --- a/bot/web/index.templ +++ b/bot/web/index.templ @@ -1,5 +1,7 @@ package web +import "fmt" + templ (w *Web) Header(title string) {
@@ -41,7 +43,7 @@ templ (w *Web) Nav(currentPage string) { } + +templ (w *Web) showStats() { +Messages Seen | +{ fmt.Sprintf("%d", w.stats.MessagesRcv) } | +
Messages Sent | +{ fmt.Sprintf("%d", w.stats.MessagesSent) } | +
Uptime | +{ fmt.Sprintf("%v", w.stats.Uptime()) } | +