rethinkdb_dashboards.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package stores
  2. import (
  3. "errors"
  4. log "github.com/alecthomas/log4go"
  5. r "github.com/dancannon/gorethink"
  6. "github.com/torkelo/grafana-pro/pkg/models"
  7. )
  8. func (self *rethinkStore) SaveDashboard(dash *models.Dashboard) error {
  9. resp, err := r.Table("dashboards").Insert(dash, r.InsertOpts{Conflict: "update"}).RunWrite(self.session)
  10. if err != nil {
  11. return err
  12. }
  13. log.Info("Inserted: %v, Errors: %v, Updated: %v", resp.Inserted, resp.Errors, resp.Updated)
  14. log.Info("First error:", resp.FirstError)
  15. if len(resp.GeneratedKeys) > 0 {
  16. dash.Id = resp.GeneratedKeys[0]
  17. }
  18. return nil
  19. }
  20. func (self *rethinkStore) GetDashboard(slug string, accountId int) (*models.Dashboard, error) {
  21. resp, err := r.Table("dashboards").
  22. GetAllByIndex("AccountIdSlug", []interface{}{accountId, slug}).
  23. Run(self.session)
  24. if err != nil {
  25. return nil, err
  26. }
  27. var dashboard models.Dashboard
  28. err = resp.One(&dashboard)
  29. if err != nil {
  30. return nil, err
  31. }
  32. return &dashboard, nil
  33. }
  34. func (self *rethinkStore) DeleteDashboard(slug string, accountId int) error {
  35. resp, err := r.Table("dashboards").
  36. GetAllByIndex("AccountIdSlug", []interface{}{accountId, slug}).
  37. Delete().RunWrite(self.session)
  38. if err != nil {
  39. return err
  40. }
  41. if resp.Deleted != 1 {
  42. return errors.New("Did not find dashboard to delete")
  43. }
  44. return nil
  45. }
  46. func (self *rethinkStore) Query(query string, accountId int) ([]*models.SearchResult, error) {
  47. docs, err := r.Table("dashboards").
  48. GetAllByIndex("AccountId", []interface{}{accountId}).
  49. Filter(r.Row.Field("Title").Match(".*")).Run(self.session)
  50. if err != nil {
  51. return nil, err
  52. }
  53. results := make([]*models.SearchResult, 0, 50)
  54. var dashboard models.Dashboard
  55. for docs.Next(&dashboard) {
  56. results = append(results, &models.SearchResult{
  57. Title: dashboard.Title,
  58. Id: dashboard.Slug,
  59. })
  60. }
  61. return results, nil
  62. }