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