models: Painstakingly converted half the model package to a models & repostiry stucture. All binaries are probably dead at this point.
the build failed
the build failed
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
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("logbo3.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)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package logs
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
// FindID finds a log either by it's ID or short ID.
|
||||
func FindID(id string) (models.Log, error) {
|
||||
return find(bson.M{
|
||||
"$or": []bson.M{
|
||||
bson.M{"_id": id},
|
||||
bson.M{"shortId": id},
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package logs
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
// Filter for the List() function
|
||||
type Filter struct {
|
||||
Search *string
|
||||
Characters *[]string
|
||||
Channels *[]string
|
||||
Events *[]string
|
||||
Open *bool
|
||||
Limit int
|
||||
}
|
||||
|
||||
// List lists all logs
|
||||
func List(filter *Filter) ([]models.Log, error) {
|
||||
query := bson.M{}
|
||||
limit := 0
|
||||
|
||||
if filter != nil {
|
||||
// Run a text search
|
||||
if filter.Search != nil {
|
||||
searchResults, err := search(*filter.Search)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Posts always use shortId to refer to the log
|
||||
query["shortId"] = bson.M{"$in": searchResults}
|
||||
}
|
||||
|
||||
// Find logs including any of the specified events and channels
|
||||
if filter.Channels != nil {
|
||||
query["channel"] = bson.M{"$in": *filter.Channels}
|
||||
}
|
||||
if filter.Events != nil {
|
||||
query["event"] = bson.M{"$in": *filter.Events}
|
||||
}
|
||||
|
||||
// Find logs including all of the specified character IDs.
|
||||
if filter.Characters != nil {
|
||||
query["characterIds"] = bson.M{"$all": *filter.Characters}
|
||||
}
|
||||
|
||||
// Limit to only open logs
|
||||
if filter.Open != nil {
|
||||
query["open"] = *filter.Open
|
||||
}
|
||||
|
||||
// Set the limit from the filter
|
||||
limit = filter.Limit
|
||||
}
|
||||
|
||||
return list(query, limit)
|
||||
}
|
||||
|
||||
func search(text string) ([]string, error) {
|
||||
query := bson.M{
|
||||
"$text": bson.M{"$search": text},
|
||||
"logId": bson.M{"$ne": nil},
|
||||
}
|
||||
|
||||
ids := make([]string, 0, 64)
|
||||
err := postCollection.Find(query).Distinct("logId", ids)
|
||||
|
||||
return ids, err
|
||||
}
|
||||
Reference in New Issue
Block a user