dashboards.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package sqlstore
  2. import (
  3. "github.com/go-xorm/xorm"
  4. "github.com/torkelo/grafana-pro/pkg/bus"
  5. m "github.com/torkelo/grafana-pro/pkg/models"
  6. )
  7. func init() {
  8. bus.AddHandler("sql", SaveDashboard2)
  9. }
  10. func SaveDashboard2(cmd *m.SaveDashboardCommand) error {
  11. return inTransaction(func(sess *xorm.Session) error {
  12. dash := cmd.GetDashboardModel()
  13. var err error
  14. if dash.Id == 0 {
  15. _, err = sess.Insert(dash)
  16. } else {
  17. _, err = sess.Id(dash.Id).Update(dash)
  18. }
  19. cmd.Result = dash
  20. return err
  21. })
  22. }
  23. func GetDashboard(slug string, accountId int64) (*m.Dashboard, error) {
  24. dashboard := m.Dashboard{Slug: slug, AccountId: accountId}
  25. has, err := x.Get(&dashboard)
  26. if err != nil {
  27. return nil, err
  28. } else if has == false {
  29. return nil, m.ErrDashboardNotFound
  30. }
  31. return &dashboard, nil
  32. }
  33. func SearchQuery(query string, accountId int64) ([]*m.SearchResult, error) {
  34. sess := x.Limit(100, 0).Where("account_id=?", accountId)
  35. sess.Table("Dashboard")
  36. results := make([]*m.SearchResult, 0)
  37. err := sess.Find(&results)
  38. return results, err
  39. }
  40. func DeleteDashboard(slug string, accountId int64) error {
  41. sess := x.NewSession()
  42. defer sess.Close()
  43. rawSql := "DELETE FROM Dashboard WHERE account_id=? and slug=?"
  44. _, err := sess.Exec(rawSql, accountId, slug)
  45. return err
  46. }