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() { +
+
+

Stats

+
+
+ + + + + + + + + + + + + + +
Messages Seen{ fmt.Sprintf("%d", w.stats.MessagesRcv) }
Messages Sent{ fmt.Sprintf("%d", w.stats.MessagesSent) }
Uptime{ fmt.Sprintf("%v", w.stats.Uptime()) }
+
+
+} \ No newline at end of file diff --git a/bot/web/index_templ.go b/bot/web/index_templ.go index 8869935..e7b3aee 100644 --- a/bot/web/index_templ.go +++ b/bot/web/index_templ.go @@ -10,6 +10,8 @@ import "context" import "io" import "bytes" +import "fmt" + func (w *Web) Header(title string) templ.Component { return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) { templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer) @@ -35,7 +37,7 @@ func (w *Web) Header(title string) templ.Component { var templ_7745c5c3_Var2 string templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(w.botName()) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `bot/web/index.templ`, Line: 8, Col: 32} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `bot/web/index.templ`, Line: 10, Col: 32} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) if templ_7745c5c3_Err != nil { @@ -48,7 +50,7 @@ func (w *Web) Header(title string) templ.Component { var templ_7745c5c3_Var3 string templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(title) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `bot/web/index.templ`, Line: 8, Col: 44} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `bot/web/index.templ`, Line: 10, Col: 44} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) if templ_7745c5c3_Err != nil { @@ -66,7 +68,7 @@ func (w *Web) Header(title string) templ.Component { var templ_7745c5c3_Var4 string templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.JoinStringErrs(w.botName()) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `bot/web/index.templ`, Line: 10, Col: 32} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `bot/web/index.templ`, Line: 12, Col: 32} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var4)) if templ_7745c5c3_Err != nil { @@ -175,20 +177,20 @@ func (w *Web) Nav(currentPage string) templ.Component { templ_7745c5c3_Var7 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("