Lots of progress on account management

This commit is contained in:
Torkel Ödegaard
2014-09-21 15:01:59 +02:00
parent 19a35673fa
commit cd9306df45
12 changed files with 317 additions and 110 deletions
+1 -88
View File
@@ -1,12 +1,10 @@
package stores
import (
"errors"
"time"
log "github.com/alecthomas/log4go"
r "github.com/dancannon/gorethink"
"github.com/torkelo/grafana-pro/pkg/models"
)
type rethinkStore struct {
@@ -36,96 +34,11 @@ func NewRethinkStore(config *RethinkCfg) *rethinkStore {
log.Crash("Failed to connect to rethink database %v", err)
}
r.DbCreate(config.DatabaseName).Exec(session)
r.Db(config.DatabaseName).TableCreate("dashboards").Exec(session)
r.Db(config.DatabaseName).TableCreate("accounts").Exec(session)
r.Db(config.DatabaseName).TableCreate("master").Exec(session)
r.Db(config.DatabaseName).Table("dashboards").IndexCreateFunc("AccountIdSlug", func(row r.Term) interface{} {
return []interface{}{row.Field("AccountId"), row.Field("Slug")}
}).Exec(session)
r.Db(config.DatabaseName).Table("dashboards").IndexCreateFunc("AccountId", func(row r.Term) interface{} {
return []interface{}{row.Field("AccountId")}
}).Exec(session)
r.Db(config.DatabaseName).Table("accounts").IndexCreateFunc("AccountLogin", func(row r.Term) interface{} {
return []interface{}{row.Field("Login")}
}).Exec(session)
_, err = r.Table("master").Insert(map[string]interface{}{"id": "ids", "NextAccountId": 0}).RunWrite(session)
if err != nil {
log.Error("Failed to insert master ids row", err)
}
createRethinkDBTablesAndIndices(config, session)
return &rethinkStore{
session: session,
}
}
func (self *rethinkStore) SaveDashboard(dash *models.Dashboard) error {
resp, err := r.Table("dashboards").Insert(dash, r.InsertOpts{Conflict: "update"}).RunWrite(self.session)
if err != nil {
return err
}
log.Info("Inserted: %v, Errors: %v, Updated: %v", resp.Inserted, resp.Errors, resp.Updated)
log.Info("First error:", resp.FirstError)
if len(resp.GeneratedKeys) > 0 {
dash.Id = resp.GeneratedKeys[0]
}
return nil
}
func (self *rethinkStore) GetDashboard(slug string, accountId int) (*models.Dashboard, error) {
resp, err := r.Table("dashboards").GetAllByIndex("AccountIdSlug", []interface{}{accountId, slug}).Run(self.session)
if err != nil {
return nil, err
}
var dashboard models.Dashboard
err = resp.One(&dashboard)
if err != nil {
return nil, err
}
return &dashboard, nil
}
func (self *rethinkStore) DeleteDashboard(slug string, accountId int) error {
resp, err := r.Table("dashboards").
GetAllByIndex("AccountIdSlug", []interface{}{accountId, slug}).
Delete().RunWrite(self.session)
if err != nil {
return err
}
if resp.Deleted != 1 {
return errors.New("Did not find dashboard to delete")
}
return nil
}
func (self *rethinkStore) Query(query string, accountId int) ([]*models.SearchResult, error) {
docs, err := r.Table("dashboards").GetAllByIndex("AccountId", []interface{}{accountId}).Filter(r.Row.Field("Title").Match(".*")).Run(self.session)
if err != nil {
return nil, err
}
results := make([]*models.SearchResult, 0, 50)
var dashboard models.Dashboard
for docs.Next(&dashboard) {
results = append(results, &models.SearchResult{
Title: dashboard.Title,
Id: dashboard.Slug,
})
}
return results, nil
}
func (self *rethinkStore) Close() {}
+29 -3
View File
@@ -47,7 +47,7 @@ func (self *rethinkStore) CreateAccount(account *models.Account) error {
}
func (self *rethinkStore) GetAccountByLogin(emailOrName string) (*models.Account, error) {
resp, err := r.Table("accounts").GetAllByIndex("AccountLogin", []interface{}{emailOrName}).Run(self.session)
resp, err := r.Table("accounts").GetAllByIndex("Login", emailOrName).Run(self.session)
if err != nil {
return nil, err
@@ -84,8 +84,8 @@ func (self *rethinkStore) UpdateAccount(account *models.Account) error {
return err
}
if resp.Replaced != 1 {
return errors.New("Could not fund account to uodate")
if resp.Replaced == 0 && resp.Unchanged == 0 {
return errors.New("Could not find account to update")
}
return nil
@@ -108,3 +108,29 @@ func (self *rethinkStore) getNextDashboardNumber(accountId int) (int, error) {
return int(change.NewValue.(map[string]interface{})["NextDashboardId"].(float64)), nil
}
func (self *rethinkStore) GetOtherAccountsFor(accountId int) ([]*models.OtherAccount, error) {
resp, err := r.Table("accounts").
GetAllByIndex("CollaboratorAccountId", accountId).
Map(func(row r.Term) interface{} {
return map[string]interface{}{
"id": row.Field("id"),
"Name": row.Field("Email"),
"Role": row.Field("Collaborators").Filter(map[string]interface{}{
"AccountId": accountId,
}).Nth(0).Field("Role"),
}
}).Run(self.session)
if err != nil {
return nil, err
}
var list []*models.OtherAccount
err = resp.All(&list)
if err != nil {
return nil, errors.New("Failed to read available accounts")
}
return list, nil
}
+79
View File
@@ -0,0 +1,79 @@
package stores
import (
"errors"
log "github.com/alecthomas/log4go"
r "github.com/dancannon/gorethink"
"github.com/torkelo/grafana-pro/pkg/models"
)
func (self *rethinkStore) SaveDashboard(dash *models.Dashboard) error {
resp, err := r.Table("dashboards").Insert(dash, r.InsertOpts{Conflict: "update"}).RunWrite(self.session)
if err != nil {
return err
}
log.Info("Inserted: %v, Errors: %v, Updated: %v", resp.Inserted, resp.Errors, resp.Updated)
log.Info("First error:", resp.FirstError)
if len(resp.GeneratedKeys) > 0 {
dash.Id = resp.GeneratedKeys[0]
}
return nil
}
func (self *rethinkStore) GetDashboard(slug string, accountId int) (*models.Dashboard, error) {
resp, err := r.Table("dashboards").
GetAllByIndex("AccountIdSlug", []interface{}{accountId, slug}).
Run(self.session)
if err != nil {
return nil, err
}
var dashboard models.Dashboard
err = resp.One(&dashboard)
if err != nil {
return nil, err
}
return &dashboard, nil
}
func (self *rethinkStore) DeleteDashboard(slug string, accountId int) error {
resp, err := r.Table("dashboards").
GetAllByIndex("AccountIdSlug", []interface{}{accountId, slug}).
Delete().RunWrite(self.session)
if err != nil {
return err
}
if resp.Deleted != 1 {
return errors.New("Did not find dashboard to delete")
}
return nil
}
func (self *rethinkStore) Query(query string, accountId int) ([]*models.SearchResult, error) {
docs, err := r.Table("dashboards").
GetAllByIndex("AccountId", []interface{}{accountId}).
Filter(r.Row.Field("Title").Match(".*")).Run(self.session)
if err != nil {
return nil, err
}
results := make([]*models.SearchResult, 0, 50)
var dashboard models.Dashboard
for docs.Next(&dashboard) {
results = append(results, &models.SearchResult{
Title: dashboard.Title,
Id: dashboard.Slug,
})
}
return results, nil
}
+39
View File
@@ -0,0 +1,39 @@
package stores
import (
log "github.com/alecthomas/log4go"
r "github.com/dancannon/gorethink"
)
func createRethinkDBTablesAndIndices(config *RethinkCfg, session *r.Session) {
r.DbCreate(config.DatabaseName).Exec(session)
// create tables
r.Db(config.DatabaseName).TableCreate("dashboards").Exec(session)
r.Db(config.DatabaseName).TableCreate("accounts").Exec(session)
r.Db(config.DatabaseName).TableCreate("master").Exec(session)
// create dashboard accountId + slug index
r.Db(config.DatabaseName).Table("dashboards").IndexCreateFunc("AccountIdSlug", func(row r.Term) interface{} {
return []interface{}{row.Field("AccountId"), row.Field("Slug")}
}).Exec(session)
r.Db(config.DatabaseName).Table("dashboards").IndexCreate("AccountId").Exec(session)
r.Db(config.DatabaseName).Table("accounts").IndexCreate("Login").Exec(session)
// create account collaborator index
r.Db(config.DatabaseName).Table("accounts").
IndexCreateFunc("CollaboratorAccountId", func(row r.Term) interface{} {
return row.Field("Collaborators").Map(func(row r.Term) interface{} {
return row.Field("AccountId")
})
}, r.IndexCreateOpts{Multi: true}).Exec(session)
// make sure master ids row exists
_, err := r.Table("master").Insert(map[string]interface{}{"id": "ids", "NextAccountId": 0}).RunWrite(session)
if err != nil {
log.Error("Failed to insert master ids row", err)
}
}
+2 -1
View File
@@ -12,7 +12,8 @@ type Store interface {
CreateAccount(acccount *models.Account) error
UpdateAccount(acccount *models.Account) error
GetAccountByLogin(emailOrName string) (*models.Account, error)
GetAccount(id int) (*models.Account, error)
GetAccount(accountId int) (*models.Account, error)
GetOtherAccountsFor(accountId int) ([]*models.OtherAccount, error)
Close()
}