unknownnicks, logs: Changed unknownnicks counter to use posts instead of logs as a metric to make characters stick out more.
the build was successful
the build was successful
This commit is contained in:
@@ -16,17 +16,18 @@ import (
|
||||
var updateTask = task.New(time.Second*60, RunFullUpdate)
|
||||
|
||||
// UpdateCharacters updates the characters for the given log.
|
||||
func UpdateCharacters(log models.Log, addUnknowns bool) (models.Log, error) {
|
||||
func UpdateCharacters(log models.Log, unknowns map[string]int) (models.Log, error) {
|
||||
posts, err := posts.List(&posts.Filter{LogID: &log.ShortID, Kind: []string{"action", "text", "chars"}, Limit: 0})
|
||||
if err != nil {
|
||||
return models.Log{}, err
|
||||
}
|
||||
|
||||
counts := make(map[string]int)
|
||||
added := make(map[string]bool)
|
||||
removed := make(map[string]bool)
|
||||
for _, post := range posts {
|
||||
if post.Kind == "text" || post.Kind == "action" {
|
||||
if strings.HasPrefix(post.Text, "(") || strings.Contains(post.Nick, "(") || strings.Contains(post.Nick, "[E]") {
|
||||
if strings.HasPrefix(post.Text, "(") || strings.Contains(post.Nick, "(") || strings.Contains(post.Nick, "[E]") || strings.HasSuffix(post.Nick, "|") {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -38,6 +39,7 @@ func UpdateCharacters(log models.Log, addUnknowns bool) (models.Log, error) {
|
||||
}
|
||||
|
||||
added[post.Nick] = true
|
||||
counts[post.Nick]++
|
||||
}
|
||||
if post.Kind == "chars" {
|
||||
tokens := strings.Fields(post.Text)
|
||||
@@ -79,7 +81,7 @@ func UpdateCharacters(log models.Log, addUnknowns bool) (models.Log, error) {
|
||||
|
||||
log.CharacterIDs = characterIDs
|
||||
|
||||
if addUnknowns {
|
||||
if len(nicks) > 0 && unknowns != nil {
|
||||
NickLoop:
|
||||
for nick := range added {
|
||||
if !added[nick] {
|
||||
@@ -92,7 +94,7 @@ func UpdateCharacters(log models.Log, addUnknowns bool) (models.Log, error) {
|
||||
}
|
||||
}
|
||||
|
||||
unknownnicks.Add(nick)
|
||||
unknowns[nick] += counts[nick]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,27 +109,22 @@ func RunFullUpdate() error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = unknownnicks.BeginUpdate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
unknowns := make(map[string]int, 256)
|
||||
|
||||
log := models.Log{}
|
||||
for iter.Next(&log) {
|
||||
_, err = UpdateCharacters(log, true)
|
||||
_, err = UpdateCharacters(log, unknowns)
|
||||
if err != nil {
|
||||
unknownnicks.CancelUpdate()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err = iter.Err()
|
||||
if err != nil {
|
||||
unknownnicks.CancelUpdate()
|
||||
return err
|
||||
}
|
||||
|
||||
err = unknownnicks.CommitUpdate()
|
||||
err = unknownnicks.Update(unknowns)
|
||||
if err != nil {
|
||||
return errors.New("Failed to commit unknown nicks update: " + err.Error())
|
||||
}
|
||||
|
||||
@@ -1,49 +1,16 @@
|
||||
package unknownnicks
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
var updateMutex sync.Mutex
|
||||
var updateMap map[string]int
|
||||
|
||||
// Add adds a nick as unknown.
|
||||
func Add(nick string) {
|
||||
updateMap[nick] = updateMap[nick] + 1
|
||||
}
|
||||
|
||||
// BeginUpdate starts an add operation
|
||||
func BeginUpdate() error {
|
||||
updateMutex.Lock()
|
||||
|
||||
// Update updates the map, replacing it with the scores provided here.
|
||||
func Update(updateMap map[string]int) error {
|
||||
_, err := collection.RemoveAll(bson.M{})
|
||||
if err != nil {
|
||||
updateMutex.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
if updateMap == nil {
|
||||
updateMap = make(map[string]int)
|
||||
}
|
||||
|
||||
for key := range updateMap {
|
||||
delete(updateMap, key)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CancelUpdate cancels an add operation
|
||||
func CancelUpdate() {
|
||||
updateMutex.Unlock()
|
||||
}
|
||||
|
||||
// CommitUpdate commits an add operation to the database.
|
||||
func CommitUpdate() error {
|
||||
defer updateMutex.Unlock()
|
||||
|
||||
for nick, score := range updateMap {
|
||||
_, err := collection.UpsertId(nick, bson.M{"$set": bson.M{"score": score}})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user