rethinkdb.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package stores
  2. import (
  3. "errors"
  4. "time"
  5. log "github.com/alecthomas/log4go"
  6. r "github.com/dancannon/gorethink"
  7. "github.com/torkelo/grafana-pro/pkg/models"
  8. )
  9. type rethinkStore struct {
  10. session *r.Session
  11. }
  12. type RethinkCfg struct {
  13. DatabaseName string
  14. }
  15. type Account struct {
  16. Id int `gorethink:"id"`
  17. NextDashboardId int
  18. }
  19. func NewRethinkStore(config *RethinkCfg) *rethinkStore {
  20. log.Info("Initializing rethink storage")
  21. session, err := r.Connect(r.ConnectOpts{
  22. Address: "localhost:28015",
  23. Database: config.DatabaseName,
  24. MaxIdle: 10,
  25. IdleTimeout: time.Second * 10,
  26. })
  27. if err != nil {
  28. log.Crash("Failed to connect to rethink database %v", err)
  29. }
  30. r.DbCreate(config.DatabaseName).Exec(session)
  31. r.Db(config.DatabaseName).TableCreate("dashboards").Exec(session)
  32. r.Db(config.DatabaseName).TableCreate("accounts").Exec(session)
  33. r.Db(config.DatabaseName).TableCreate("master").Exec(session)
  34. r.Db(config.DatabaseName).Table("dashboards").IndexCreateFunc("AccountIdSlug", func(row r.Term) interface{} {
  35. return []interface{}{row.Field("AccountId"), row.Field("Slug")}
  36. }).Exec(session)
  37. r.Db(config.DatabaseName).Table("dashboards").IndexCreateFunc("AccountId", func(row r.Term) interface{} {
  38. return []interface{}{row.Field("AccountId")}
  39. }).Exec(session)
  40. r.Db(config.DatabaseName).Table("accounts").IndexCreateFunc("AccountLogin", func(row r.Term) interface{} {
  41. return []interface{}{row.Field("Login")}
  42. }).Exec(session)
  43. _, err = r.Table("master").Insert(map[string]interface{}{"id": "ids", "NextAccountId": 0}).RunWrite(session)
  44. if err != nil {
  45. log.Error("Failed to insert master ids row", err)
  46. }
  47. return &rethinkStore{
  48. session: session,
  49. }
  50. }
  51. func (self *rethinkStore) SaveDashboard(dash *models.Dashboard) error {
  52. resp, err := r.Table("dashboards").Insert(dash, r.InsertOpts{Conflict: "update"}).RunWrite(self.session)
  53. if err != nil {
  54. return err
  55. }
  56. log.Info("Inserted: %v, Errors: %v, Updated: %v", resp.Inserted, resp.Errors, resp.Updated)
  57. log.Info("First error:", resp.FirstError)
  58. if len(resp.GeneratedKeys) > 0 {
  59. dash.Id = resp.GeneratedKeys[0]
  60. }
  61. return nil
  62. }
  63. func (self *rethinkStore) GetDashboard(slug string, accountId int) (*models.Dashboard, error) {
  64. resp, err := r.Table("dashboards").GetAllByIndex("AccountIdSlug", []interface{}{accountId, slug}).Run(self.session)
  65. if err != nil {
  66. return nil, err
  67. }
  68. var dashboard models.Dashboard
  69. err = resp.One(&dashboard)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return &dashboard, nil
  74. }
  75. func (self *rethinkStore) DeleteDashboard(slug string, accountId int) error {
  76. resp, err := r.Table("dashboards").
  77. GetAllByIndex("AccountIdSlug", []interface{}{accountId, slug}).
  78. Delete().RunWrite(self.session)
  79. if err != nil {
  80. return err
  81. }
  82. if resp.Deleted != 1 {
  83. return errors.New("Did not find dashboard to delete")
  84. }
  85. return nil
  86. }
  87. func (self *rethinkStore) Query(query string, accountId int) ([]*models.SearchResult, error) {
  88. docs, err := r.Table("dashboards").GetAllByIndex("AccountId", []interface{}{accountId}).Filter(r.Row.Field("Title").Match(".*")).Run(self.session)
  89. if err != nil {
  90. return nil, err
  91. }
  92. results := make([]*models.SearchResult, 0, 50)
  93. var dashboard models.Dashboard
  94. for docs.Next(&dashboard) {
  95. results = append(results, &models.SearchResult{
  96. Title: dashboard.Title,
  97. Id: dashboard.Slug,
  98. })
  99. }
  100. return results, nil
  101. }
  102. func (self *rethinkStore) Close() {}