dashboards.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package models
  2. import (
  3. "errors"
  4. "regexp"
  5. "strings"
  6. "time"
  7. )
  8. // Typed errors
  9. var (
  10. ErrDashboardNotFound = errors.New("Account not found")
  11. ErrDashboardWithSameNameExists = errors.New("A dashboard with the same name already exists")
  12. )
  13. type Dashboard struct {
  14. Id int64
  15. Slug string `xorm:"index(IX_AccountIdSlug)"`
  16. AccountId int64 `xorm:"index(IX_AccountIdSlug)"`
  17. Created time.Time
  18. Updated time.Time
  19. Title string
  20. Data map[string]interface{}
  21. }
  22. type SearchResult struct {
  23. Dashboards []*DashboardSearchHit `json:"dashboards"`
  24. Tags []*DashboardTagCloudItem `json:"tags"`
  25. TagsOnly bool `json:"tagsOnly"`
  26. }
  27. type DashboardSearchHit struct {
  28. Title string `json:"title"`
  29. Slug string `json:"slug"`
  30. Tags []string `json:"tags"`
  31. }
  32. type DashboardTagCloudItem struct {
  33. Term string `json:"term"`
  34. Count int `json:"count"`
  35. }
  36. type SearchDashboardsQuery struct {
  37. Title string
  38. Tag string
  39. AccountId int64
  40. Result []*DashboardSearchHit
  41. }
  42. type GetDashboardTagsQuery struct {
  43. AccountId int64
  44. Result []*DashboardTagCloudItem
  45. }
  46. type SaveDashboardCommand struct {
  47. Dashboard map[string]interface{} `json:"dashboard"`
  48. AccountId int64 `json:"-"`
  49. Result *Dashboard
  50. }
  51. type DeleteDashboardCommand struct {
  52. Slug string
  53. AccountId int64
  54. }
  55. type GetDashboardQuery struct {
  56. Slug string
  57. AccountId int64
  58. Result *Dashboard
  59. }
  60. func NewDashboard(title string) *Dashboard {
  61. dash := &Dashboard{}
  62. dash.Data = make(map[string]interface{})
  63. dash.Data["title"] = title
  64. dash.Title = title
  65. dash.UpdateSlug()
  66. return dash
  67. }
  68. func (dash *Dashboard) GetTags() []string {
  69. jsonTags := dash.Data["tags"]
  70. if jsonTags == nil {
  71. return []string{}
  72. }
  73. arr := jsonTags.([]interface{})
  74. b := make([]string, len(arr))
  75. for i := range arr {
  76. b[i] = arr[i].(string)
  77. }
  78. return b
  79. }
  80. func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
  81. dash := &Dashboard{}
  82. dash.Data = cmd.Dashboard
  83. dash.Title = dash.Data["title"].(string)
  84. dash.AccountId = cmd.AccountId
  85. dash.UpdateSlug()
  86. if dash.Data["id"] != nil {
  87. dash.Id = int64(dash.Data["id"].(float64))
  88. }
  89. return dash
  90. }
  91. func (dash *Dashboard) GetString(prop string) string {
  92. return dash.Data[prop].(string)
  93. }
  94. func (dash *Dashboard) UpdateSlug() {
  95. title := strings.ToLower(dash.Data["title"].(string))
  96. re := regexp.MustCompile("[^\\w ]+")
  97. re2 := regexp.MustCompile("\\s")
  98. dash.Slug = re2.ReplaceAllString(re.ReplaceAllString(title, ""), "-")
  99. }