mirror of https://github.com/velour/catbase.git
Compare commits
No commits in common. "3cbbc4a08a871d0b877840e49c73a4e294153e78" and "8b38429a31a940011810b5df97334ca195daec87" have entirely different histories.
3cbbc4a08a
...
8b38429a31
|
@ -4,8 +4,6 @@ package countdown
|
|||
import (
|
||||
"fmt"
|
||||
bh "github.com/timshannon/bolthold"
|
||||
"github.com/velour/catbase/plugins/babbler"
|
||||
"github.com/velour/catbase/plugins/counter"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
@ -50,13 +48,35 @@ func (p *CountdownPlugin) scheduleNYE(f func()) {
|
|||
func (p *CountdownPlugin) newYearRollover() {
|
||||
log.Debug().Msgf("Happy new year!")
|
||||
|
||||
if err := p.clearBabblers(); err != nil {
|
||||
log.Error().Err(err).Msgf("error clearing babblers")
|
||||
// todo: something about rolling over
|
||||
//tx, err := p.db.Beginx()
|
||||
//if err != nil {
|
||||
// logError(err, "error getting transaction")
|
||||
// return
|
||||
//}
|
||||
//q := fmt.Sprintf(`create table counter_%d as select * from counter`, thisYear)
|
||||
//_, err = tx.Exec(q)
|
||||
//if err != nil {
|
||||
// logError(err, "error running insert into")
|
||||
// logError(tx.Rollback(), "error with insert rollback")
|
||||
// return
|
||||
//}
|
||||
//q = `delete from counter`
|
||||
//_, err = tx.Exec(q)
|
||||
//if err != nil {
|
||||
// logError(err, "error running delete")
|
||||
// logError(tx.Rollback(), "error with delete rollback")
|
||||
// return
|
||||
//}
|
||||
//err = tx.Commit()
|
||||
//logError(err, "error committing transaction")
|
||||
}
|
||||
|
||||
if err := p.archiveCounters(); err != nil {
|
||||
log.Error().Err(err).Msgf("error clearing babblers")
|
||||
func logError(err error, msg string) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
log.Error().Err(err).Msgf(msg)
|
||||
}
|
||||
|
||||
func (p *CountdownPlugin) nyeCountdown(i int) {
|
||||
|
@ -66,47 +86,3 @@ func (p *CountdownPlugin) nyeCountdown(i int) {
|
|||
p.b.Send(p.b.DefaultConnector(), bot.Message, ch, msg, msg)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *CountdownPlugin) clearBabblers() error {
|
||||
if err := p.store.DeleteMatching(babbler.Babbler{}, &bh.Query{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.store.DeleteMatching(babbler.BabblerArc{}, &bh.Query{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.store.DeleteMatching(babbler.BabblerNode{}, &bh.Query{}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.store.DeleteMatching(babbler.BabblerWord{}, &bh.Query{}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type counterArchive struct {
|
||||
counter.Item
|
||||
ID uint64 `boltholdKey:"ID"`
|
||||
Year int
|
||||
}
|
||||
|
||||
func (p *CountdownPlugin) archiveCounters() error {
|
||||
year := time.Now().Year()
|
||||
allCounters := []counter.Item{}
|
||||
|
||||
if err := p.store.Find(&allCounters, &bh.Query{}); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, c := range allCounters {
|
||||
ca := counterArchive{Item: c, Year: year}
|
||||
err := p.store.Insert(bh.NextSequence(), &ca)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if err := p.store.DeleteMatching(counter.Item{}, &bh.Query{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,12 +1,6 @@
|
|||
package countdown
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
bh "github.com/timshannon/bolthold"
|
||||
"github.com/velour/catbase/plugins/babbler"
|
||||
"github.com/velour/catbase/plugins/counter"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -15,19 +9,6 @@ import (
|
|||
"github.com/velour/catbase/bot"
|
||||
)
|
||||
|
||||
func init() {
|
||||
output := zerolog.ConsoleWriter{Out: os.Stdout}
|
||||
log.Logger = log.Output(output).With().Caller().Stack().Logger()
|
||||
}
|
||||
|
||||
func setup() (*bot.MockBot, *CountdownPlugin, func()) {
|
||||
mb := bot.NewMockBot()
|
||||
cd := New(mb)
|
||||
return mb, cd, func() {
|
||||
mb.TearDown()
|
||||
}
|
||||
}
|
||||
|
||||
func TestScheduleNewYears(t *testing.T) {
|
||||
mb := bot.NewMockBot()
|
||||
p := New(mb)
|
||||
|
@ -43,62 +24,3 @@ func TestScheduleNewYears(t *testing.T) {
|
|||
|
||||
assert.True(t, functionTripped)
|
||||
}
|
||||
|
||||
func TestClearBabbles(t *testing.T) {
|
||||
mb, cd, td := setup()
|
||||
defer td()
|
||||
b := babbler.Babbler{
|
||||
Name: "foo",
|
||||
}
|
||||
if err := mb.Store().Insert(bh.NextSequence(), &b); err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if err := mb.Store().Insert(bh.NextSequence(), &b); err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if err := mb.Store().Insert(bh.NextSequence(), &b); err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
count, err := mb.Store().Count(babbler.Babbler{}, &bh.Query{})
|
||||
assert.Nil(t, err)
|
||||
assert.Greater(t, count, 0)
|
||||
err = cd.clearBabblers()
|
||||
assert.Nil(t, err)
|
||||
count, err = mb.Store().Count(babbler.Babbler{}, &bh.Query{})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, count, 0)
|
||||
}
|
||||
|
||||
func TestArchiveCounters(t *testing.T) {
|
||||
mb, cd, td := setup()
|
||||
defer td()
|
||||
i := counter.Item{
|
||||
Nick: "foo",
|
||||
Item: "bar",
|
||||
Count: 8,
|
||||
UserID: "123",
|
||||
}
|
||||
if err := mb.Store().Insert(bh.NextSequence(), &i); err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if err := mb.Store().Insert(bh.NextSequence(), &i); err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
if err := mb.Store().Insert(bh.NextSequence(), &i); err != nil {
|
||||
t.Fail()
|
||||
}
|
||||
count, err := mb.Store().Count(counter.Item{}, &bh.Query{})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, count, 3)
|
||||
|
||||
err = cd.archiveCounters()
|
||||
assert.Nil(t, err)
|
||||
|
||||
count, err = mb.Store().Count(counter.Item{}, &bh.Query{})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, count, 0)
|
||||
|
||||
count, err = mb.Store().Count(counterArchive{}, &bh.Query{})
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, count, 3)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue