Gisle Aune
6 years ago
8 changed files with 151 additions and 6 deletions
-
2graph2/queries/log.go
-
6graph2/queries/post.go
-
39models/logs/update-characters.go
-
7models/unknown-nick.go
-
37models/unknownnicks/db.go
-
10models/unknownnicks/list.go
-
1models/unknownnicks/remove.go
-
55models/unknownnicks/update.go
@ -0,0 +1,7 @@ |
|||
package models |
|||
|
|||
// UnknownNick represents an unknown nick name.
|
|||
type UnknownNick struct { |
|||
Nick string `bson:"_id"` |
|||
Weight int `bson:"weight"` |
|||
} |
@ -0,0 +1,37 @@ |
|||
package unknownnicks |
|||
|
|||
import ( |
|||
"log" |
|||
|
|||
"git.aiterp.net/rpdata/api/internal/store" |
|||
"git.aiterp.net/rpdata/api/models" |
|||
"github.com/globalsign/mgo" |
|||
) |
|||
|
|||
var collection *mgo.Collection |
|||
|
|||
func list(query interface{}, limit int) ([]models.UnknownNick, error) { |
|||
size := 64 |
|||
if limit > 0 { |
|||
size = limit |
|||
} |
|||
posts := make([]models.UnknownNick, 0, size) |
|||
|
|||
err := collection.Find(query).Limit(limit).Sort("weight").All(&posts) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return posts, nil |
|||
} |
|||
|
|||
func init() { |
|||
store.HandleInit(func(db *mgo.Database) { |
|||
collection = db.C("logbot3.unkownnicks") |
|||
|
|||
err := collection.EnsureIndexKey("weight") |
|||
if err != nil { |
|||
log.Fatalln("init logbot3.unkownnicks:", err) |
|||
} |
|||
}) |
|||
} |
@ -0,0 +1,10 @@ |
|||
package unknownnicks |
|||
|
|||
import ( |
|||
"git.aiterp.net/rpdata/api/models" |
|||
) |
|||
|
|||
// List lists the top N unknown nicks.
|
|||
func List(limit int) ([]models.UnknownNick, error) { |
|||
return list(nil, limit) |
|||
} |
@ -0,0 +1 @@ |
|||
package unknownnicks |
@ -0,0 +1,55 @@ |
|||
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() |
|||
|
|||
_, 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, weight := range updateMap { |
|||
_, err := collection.UpsertId(nick, bson.M{"$set": bson.M{"weight": weight}}) |
|||
if err != nil { |
|||
return err |
|||
} |
|||
} |
|||
|
|||
return nil |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue