Gisle Aune
6 years ago
7 changed files with 225 additions and 14 deletions
-
19graph2/queries/channel.go
-
48graph2/queries/character.go
-
28graph2/queries/log.go
-
32graph2/queries/post.go
-
33models/change.go
-
31models/changes/db.go
-
48models/changes/submit.go
@ -0,0 +1,33 @@ |
|||||
|
package models |
||||
|
|
||||
|
import "time" |
||||
|
|
||||
|
// Change represents a change in the rpdata history through the API.
|
||||
|
type Change struct { |
||||
|
ID string `bson:"_id"` |
||||
|
Model string `bson:"model"` |
||||
|
Op string `bson:"op"` |
||||
|
Author string `bson:"author"` |
||||
|
Date time.Time `bson:"date"` |
||||
|
|
||||
|
Logs []Log `bson:"logs"` |
||||
|
Characters []Character `bson:"characters"` |
||||
|
Posts []Post `bson:"posts"` |
||||
|
} |
||||
|
|
||||
|
// Data makes a combined, mixed array of all the models stored in this change.
|
||||
|
func (change *Change) Data() []interface{} { |
||||
|
data := make([]interface{}, 0, len(change.Logs)+len(change.Characters)+len(change.Posts)) |
||||
|
|
||||
|
for _, log := range change.Logs { |
||||
|
data = append(data, log) |
||||
|
} |
||||
|
for _, character := range change.Characters { |
||||
|
data = append(data, character) |
||||
|
} |
||||
|
for _, post := range change.Posts { |
||||
|
data = append(data, post) |
||||
|
} |
||||
|
|
||||
|
return data |
||||
|
} |
@ -0,0 +1,31 @@ |
|||||
|
package changes |
||||
|
|
||||
|
import ( |
||||
|
"log" |
||||
|
"sync" |
||||
|
"time" |
||||
|
|
||||
|
"git.aiterp.net/rpdata/api/internal/store" |
||||
|
"github.com/globalsign/mgo" |
||||
|
) |
||||
|
|
||||
|
var collection *mgo.Collection |
||||
|
var submitMutex sync.Mutex |
||||
|
|
||||
|
func init() { |
||||
|
store.HandleInit(func(db *mgo.Database) { |
||||
|
collection = db.C("common.changes") |
||||
|
|
||||
|
collection.EnsureIndexKey("date") |
||||
|
collection.EnsureIndexKey("author") |
||||
|
|
||||
|
err := collection.EnsureIndex(mgo.Index{ |
||||
|
Name: "expiry", |
||||
|
Key: []string{"date"}, |
||||
|
ExpireAfter: time.Hour * 2400, // 100 days
|
||||
|
}) |
||||
|
if err != nil { |
||||
|
log.Fatalln(err) |
||||
|
} |
||||
|
}) |
||||
|
} |
@ -0,0 +1,48 @@ |
|||||
|
package changes |
||||
|
|
||||
|
import ( |
||||
|
"strconv" |
||||
|
"time" |
||||
|
|
||||
|
"git.aiterp.net/rpdata/api/internal/counter" |
||||
|
"git.aiterp.net/rpdata/api/models" |
||||
|
) |
||||
|
|
||||
|
// Submit a change to the database
|
||||
|
func Submit(model, op, author string, objects ...interface{}) (models.Change, error) { |
||||
|
submitMutex.Lock() |
||||
|
defer submitMutex.Unlock() |
||||
|
|
||||
|
id, err := counter.Next("auto_increment", "Change") |
||||
|
if err != nil { |
||||
|
return models.Change{}, err |
||||
|
} |
||||
|
|
||||
|
change := models.Change{ |
||||
|
ID: "Change_" + strconv.Itoa(id), |
||||
|
Model: model, |
||||
|
Date: time.Now(), |
||||
|
Op: op, |
||||
|
Author: author, |
||||
|
} |
||||
|
|
||||
|
for _, object := range objects { |
||||
|
switch object := object.(type) { |
||||
|
case models.Log: |
||||
|
change.Logs = append(change.Logs, object) |
||||
|
case models.Character: |
||||
|
change.Characters = append(change.Characters, object) |
||||
|
case models.Post: |
||||
|
change.Posts = append(change.Posts, object) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// TODO: Push to subscribers
|
||||
|
|
||||
|
err = collection.Insert(&change) |
||||
|
if err != nil { |
||||
|
return models.Change{}, err |
||||
|
} |
||||
|
|
||||
|
return change, nil |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue