mirror of https://github.com/grafana/grafana
parent
19a35673fa
commit
cd9306df45
@ -1 +1 @@ |
||||
Subproject commit 639a44d99629ba38e67eb04df8c8c9622093de70 |
||||
Subproject commit e0dc530e943d52453aa06fc47596d3f6f0261f2c |
||||
@ -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 |
||||
} |
||||
@ -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) |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,29 @@ |
||||
package api |
||||
|
||||
type accountInfoDto struct { |
||||
Login string `json:"login"` |
||||
Email string `json:"email"` |
||||
AccountName string `json:"accountName"` |
||||
Collaborators []*collaboratorInfoDto `json:"collaborators"` |
||||
} |
||||
|
||||
type collaboratorInfoDto struct { |
||||
AccountId int `json:"accountId"` |
||||
Email string `json:"email"` |
||||
Role string `json:"role"` |
||||
} |
||||
|
||||
type addCollaboratorDto struct { |
||||
Email string `json:"email" binding:"required"` |
||||
} |
||||
|
||||
type removeCollaboratorDto struct { |
||||
AccountId int `json:"accountId" binding:"required"` |
||||
} |
||||
|
||||
type usingAccountDto struct { |
||||
AccountId int `json:"accountId"` |
||||
Email string `json:"email"` |
||||
Role string `json:"role"` |
||||
IsUsing bool |
||||
} |
||||
Loading…
Reference in new issue