tldr: change history to be in order; clamp to 24h

This commit is contained in:
Chris Sexton 2019-04-12 11:53:40 -04:00
parent e82d84afeb
commit a865dfe2da
2 changed files with 65 additions and 38 deletions

View File

@ -119,32 +119,38 @@ func (p *TLDRPlugin) message(kind bot.Kind, message msg.Message, args ...interfa
return true return true
} }
if shouldKeepMessage(lowercaseMessage) { hist := history{
currentHistorySize := len(p.history) body: lowercaseMessage,
maxHistorySize := p.bot.Config().GetInt("TLDR.HistorySize", 1000) user: message.User.Name,
hist := history{ timestamp: time.Now(),
body: lowercaseMessage, }
user: message.User.Name, p.addHistory(hist)
timestamp: time.Now(),
}
if currentHistorySize < maxHistorySize {
p.history = append(p.history, hist)
p.index = 0
} else {
if currentHistorySize > maxHistorySize {
// We could resize this but we want to prune the oldest stuff, and
// I don't care to do this correctly so might as well not do it at all
}
if p.index >= currentHistorySize { return false
p.index = 0 }
}
p.history[p.index] = hist func (p *TLDRPlugin) addHistory(hist history) {
p.index++ p.history = append(p.history, hist)
sz := len(p.history)
max := p.bot.Config().GetInt("TLDR.HistorySize", 1000)
keepHrs := time.Duration(p.bot.Config().GetInt("TLDR.KeepHours", 24))
// Clamp the size of the history
if sz > max {
p.history = p.history[len(p.history)-max:]
}
// Remove old entries
yesterday := time.Now().Add(-keepHrs * time.Hour)
begin := 0
for i, m := range p.history {
if !m.timestamp.Before(yesterday) {
begin = i - 1 // should keep this message
if begin < 0 {
begin = 0
}
break
} }
} }
return false p.history = p.history[begin:]
} }
func (p *TLDRPlugin) getTopics() []string { func (p *TLDRPlugin) getTopics() []string {
@ -155,28 +161,12 @@ func (p *TLDRPlugin) getTopics() []string {
return hist return hist
} }
func (p *TLDRPlugin) pruneHistory() {
out := []history{}
yesterday := time.Now().Add(-24 * time.Hour)
for _, h := range p.history {
if yesterday.Before(h.timestamp) {
out = append(out, h)
}
}
p.history = out
p.index = len(out)
}
// Help responds to help requests. Every plugin must implement a help function. // Help responds to help requests. Every plugin must implement a help function.
func (p *TLDRPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { func (p *TLDRPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool {
p.bot.Send(bot.Message, message.Channel, "tl;dr") p.bot.Send(bot.Message, message.Channel, "tl;dr")
return true return true
} }
func shouldKeepMessage(message string) bool {
return true
}
func min(slice []float64) (float64, int) { func min(slice []float64) (float64, int) {
minVal := 1. minVal := 1.
minIndex := -1 minIndex := -1

View File

@ -2,8 +2,10 @@ package tldr
import ( import (
"os" "os"
"strconv"
"strings" "strings"
"testing" "testing"
"time"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -61,3 +63,38 @@ func TestDoubleUp(t *testing.T) {
assert.Len(t, mb.Messages, 2) assert.Len(t, mb.Messages, 2)
assert.Contains(t, mb.Messages[1], "Slow down, cowboy.") assert.Contains(t, mb.Messages[1], "Slow down, cowboy.")
} }
func TestAddHistoryLimitsMessages(t *testing.T) {
c, _ := setup(t)
max := 1000
c.bot.Config().Set("TLDR.HistorySize", strconv.Itoa(max))
c.bot.Config().Set("TLDR.KeepHours", "24")
t0 := time.Now().Add(-24 * time.Hour)
for i := 0; i < max*2; i++ {
hist := history{
body: "test",
user: "tester",
timestamp: t0.Add(time.Duration(i) * time.Second),
}
c.addHistory(hist)
}
assert.Len(t, c.history, max)
}
func TestAddHistoryLimitsDays(t *testing.T) {
c, _ := setup(t)
hrs := 24
expected := 24
c.bot.Config().Set("TLDR.HistorySize", "100")
c.bot.Config().Set("TLDR.KeepHours", strconv.Itoa(hrs))
t0 := time.Now().Add(-time.Duration(hrs*2) * time.Hour)
for i := 0; i < 48; i++ {
hist := history{
body: "test",
user: "tester",
timestamp: t0.Add(time.Duration(i) * time.Hour),
}
c.addHistory(hist)
}
assert.Len(t, c.history, expected, "%d != %d", len(c.history), expected)
}