graph2: Added change type and query.
the build was successful

This commit is contained in:
2018-10-31 21:38:45 +01:00
parent 138b3e3eab
commit ffec008820
14 changed files with 278 additions and 9 deletions
+9
View File
@@ -6,12 +6,21 @@ import (
"time"
"git.aiterp.net/rpdata/api/internal/store"
"git.aiterp.net/rpdata/api/models"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
var collection *mgo.Collection
var submitMutex sync.Mutex
func list(query bson.M, limit int) ([]models.Change, error) {
changes := make([]models.Change, 0, 64)
err := collection.Find(query).Limit(limit).Sort("date").All(&changes)
return changes, err
}
func init() {
store.HandleInit(func(db *mgo.Database) {
collection = db.C("common.changes")
+42
View File
@@ -0,0 +1,42 @@
package changes
import (
"time"
"git.aiterp.net/rpdata/api/models"
"github.com/globalsign/mgo/bson"
)
// Filter is a filter for changes.List.
type Filter struct {
Keys []models.ChangeKey
EarliestDate *time.Time
Limit *int
}
// List lists changes.
func List(filter *Filter) ([]models.Change, error) {
query := bson.M{}
limit := 0
if filter != nil {
if filter.Limit != nil {
limit = *filter.Limit
}
if filter.Keys != nil {
query["keys"] = bson.M{"$in": filter.Keys}
} else {
query["listed"] = true
}
if filter.EarliestDate != nil {
query["date"] = bson.M{"$gt": *filter.EarliestDate}
}
} else {
query["listed"] = true
limit = 256
}
return list(query, limit)
}
+23 -1
View File
@@ -18,9 +18,31 @@ func Submit(model, op, author string, listed bool, keys []models.ChangeKey, obje
return models.Change{}, err
}
if !models.ChangeModel(model).IsValid() {
panic("Invalid model")
}
// Silently discard * keys on unlisted changes.
if !listed {
keysCopy := make([]models.ChangeKey, len(keys))
copy(keysCopy, keys)
keys = keysCopy
deletes := make([]int, 0, 1)
for i, key := range keys {
if key.ID == "*" {
deletes = append(deletes, i-len(deletes))
}
}
for _, index := range deletes {
keys = append(keys[:index], keys[index+1:]...)
}
}
change := models.Change{
ID: "Change_" + strconv.Itoa(id),
Model: model,
Model: models.ChangeModel(model),
Date: time.Now(),
Op: op,
Author: author,