From aa88ef44f5d043f5f9bcd282fe3f7676a51b2f3a Mon Sep 17 00:00:00 2001 From: Chris Sexton <3216719+chrissexton@users.noreply.github.com> Date: Thu, 2 Jun 2022 11:00:48 -0400 Subject: [PATCH] emojy: redo database schema - record emojy as timestamped log instead of a count --- .gitignore | 1 + go.sum | 3 --- plugins/emojy/emojy.go | 28 +++++++++++++++++----------- plugins/emojy/index.html | 2 +- plugins/emojy/web.go | 2 +- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 63bd8fc..5953a1c 100644 --- a/.gitignore +++ b/.gitignore @@ -75,3 +75,4 @@ rathaus.sh run.sh impact.ttf .env +rathaus_discord.sh diff --git a/go.sum b/go.sum index 58b11d9..7a9060d 100644 --- a/go.sum +++ b/go.sum @@ -78,7 +78,6 @@ github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8 github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= @@ -171,7 +170,6 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= -golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e h1:1SzTfNOXwIS2oWiMF+6qu0OUDKb0dauo6MoDUQyu+yU= golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM= golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= @@ -211,7 +209,6 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs= golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/plugins/emojy/emojy.go b/plugins/emojy/emojy.go index 5a06b16..02bfd3e 100644 --- a/plugins/emojy/emojy.go +++ b/plugins/emojy/emojy.go @@ -6,6 +6,7 @@ import ( "github.com/velour/catbase/bot" "github.com/velour/catbase/config" "regexp" + "time" ) type EmojyPlugin struct { @@ -28,9 +29,10 @@ func New(b bot.Bot) *EmojyPlugin { } func (p *EmojyPlugin) setupDB() { - p.db.MustExec(`create table if not exists emojyCounter ( - emojy text primary key, - count integer + p.db.MustExec(`create table if not exists emojyLog ( + id integer primary key autoincrement, + emojy text, + observed datetime )`) } @@ -50,9 +52,8 @@ func (p *EmojyPlugin) register() { } func (p *EmojyPlugin) recordReaction(emojy string) error { - q := `insert into emojyCounter (emojy, count) values (?, 1) - on conflict(emojy) do update set count=count+1 where emojy=?;` - _, err := p.db.Exec(q, emojy, emojy) + q := `insert into emojyLog (emojy, observed) values (?, ?)` + _, err := p.db.Exec(q, emojy, time.Now()) if err != nil { log.Error().Err(err).Msgf("recordReaction") return err @@ -61,13 +62,18 @@ func (p *EmojyPlugin) recordReaction(emojy string) error { } type EmojyEntry struct { - Emojy string `db:"emojy"` - Count int `db:"count"` + Emojy string `db:"emojy"` + Observed time.Time `db:"observed"` } -func (p *EmojyPlugin) all() ([]EmojyEntry, error) { - q := `select emojy, count from emojyCounter order by count desc` - result := []EmojyEntry{} +type EmojyCount struct { + Emojy string `json:"emojy"` + Count int `json:"count"` +} + +func (p *EmojyPlugin) allCounts() ([]EmojyCount, error) { + q := `select emojy, count(observed) as count from emojyLog group by emojy order by count desc` + result := []EmojyCount{} err := p.db.Select(&result, q) if err != nil { return nil, err diff --git a/plugins/emojy/index.html b/plugins/emojy/index.html index 0bbb17f..3bb1afd 100644 --- a/plugins/emojy/index.html +++ b/plugins/emojy/index.html @@ -34,7 +34,7 @@ diff --git a/plugins/emojy/web.go b/plugins/emojy/web.go index af42874..4cbf729 100644 --- a/plugins/emojy/web.go +++ b/plugins/emojy/web.go @@ -24,7 +24,7 @@ func (p *EmojyPlugin) handleIndex(w http.ResponseWriter, r *http.Request) { } func (p *EmojyPlugin) handleAll(w http.ResponseWriter, r *http.Request) { - emojy, err := p.all() + emojy, err := p.allCounts() if err != nil { w.WriteHeader(500) log.Error().Err(err).Msgf("handleAll")