package logs import ( "fmt" "log" "time" "git.aiterp.net/rpdata/api/internal/store" "git.aiterp.net/rpdata/api/models" "github.com/globalsign/mgo" ) var collection *mgo.Collection var postCollection *mgo.Collection func find(query interface{}) (models.Log, error) { log := models.Log{} err := collection.Find(query).One(&log) if err != nil { return models.Log{}, err } return log, nil } func list(query interface{}, limit int) ([]models.Log, error) { logs := make([]models.Log, 0, 64) err := collection.Find(query).Limit(limit).Sort("-date").All(&logs) if err != nil { return nil, err } return logs, nil } func iter(query interface{}, limit int) *mgo.Iter { return collection.Find(query).Sort("-date").Limit(limit).Batch(8).Iter() } func makeLogID(date time.Time, channel string) string { return fmt.Sprintf("%s%03d_%s", date.UTC().Format("2006-01-02_150405"), (date.Nanosecond() / int(time.Millisecond/time.Nanosecond)), channel[1:]) } func init() { store.HandleInit(func(db *mgo.Database) { collection = db.C("logbot3.logs") postCollection = db.C("logbot3.posts") collection.EnsureIndexKey("date") collection.EnsureIndexKey("channel") collection.EnsureIndexKey("characterIds") collection.EnsureIndexKey("event") collection.EnsureIndex(mgo.Index{ Key: []string{"channel", "open"}, }) err := collection.EnsureIndex(mgo.Index{ Key: []string{"shortId"}, Unique: true, DropDups: true, }) if err != nil { log.Fatalln("init logbot3.logs:", err) } }) }