Browse Source

unknownnicks, logs: Changed unknownnicks counter to use posts instead of logs as a metric to make characters stick out more.

module-madness-pointers
Gisle Aune 6 years ago
parent
commit
b60897a597
  1. 2
      graph2/queries/log.go
  2. 6
      graph2/queries/post.go
  3. 21
      models/logs/update-characters.go
  4. 37
      models/unknownnicks/update.go

2
graph2/queries/log.go

@ -130,7 +130,7 @@ func (r *mutationResolver) ImportLog(ctx context.Context, input input.LogImportI
changes.Submit("Post", "add", token.UserID, true, changekeys.Many(result.Log, result.Posts), result.Posts)
}()
log, err := logs.UpdateCharacters(result.Log, false)
log, err := logs.UpdateCharacters(result.Log, nil)
if err != nil {
log = result.Log
}

6
graph2/queries/post.go

@ -59,7 +59,7 @@ func (r *mutationResolver) AddPost(ctx context.Context, input input.PostAddInput
return models.Post{}, err
}
go logs.UpdateCharacters(log, false)
go logs.UpdateCharacters(log, nil)
go changes.Submit("Post", "add", token.UserID, true, changekeys.Many(log, post), post)
return post, nil
@ -83,7 +83,7 @@ func (r *mutationResolver) EditPost(ctx context.Context, input input.PostEditInp
return
}
logs.UpdateCharacters(log, false)
logs.UpdateCharacters(log, nil)
}()
}
@ -154,7 +154,7 @@ func (r *mutationResolver) RemovePost(ctx context.Context, input input.PostRemov
return
}
logs.UpdateCharacters(log, false)
logs.UpdateCharacters(log, nil)
changes.Submit("Post", "remove", token.UserID, true, changekeys.Many(log, post), post)
}()

21
models/logs/update-characters.go

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

37
models/unknownnicks/update.go

@ -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 {

Loading…
Cancel
Save