rethinkdb.go 2.8 KB

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