dashboards.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 `xorm:"CREATED"`
  18. Updated time.Time `xorm:"UPDATED"`
  19. Title string
  20. Tags []string
  21. Data map[string]interface{}
  22. }
  23. type SearchResult struct {
  24. Dashboards []DashboardSearchHit `json:"dashboards"`
  25. Tags []DashboardTagCloudItem `json:"tags"`
  26. TagsOnly bool `json:"tagsOnly"`
  27. }
  28. type DashboardSearchHit struct {
  29. Id string `json:"id"`
  30. Title string `json:"title"`
  31. Slug string `json:"slug"`
  32. Tags []string `json:"tags"`
  33. }
  34. type DashboardTagCloudItem struct {
  35. Term string `json:"term"`
  36. Count int `json:"count"`
  37. }
  38. type SearchDashboardsQuery struct {
  39. Query string
  40. AccountId int64
  41. Result []DashboardSearchHit
  42. }
  43. type GetDashboardTagsQuery struct {
  44. AccountId int64
  45. Result []DashboardTagCloudItem
  46. }
  47. type SaveDashboardCommand struct {
  48. Dashboard map[string]interface{} `json:"dashboard"`
  49. AccountId int64 `json:"-"`
  50. Result *Dashboard
  51. }
  52. type DeleteDashboardCommand struct {
  53. Slug string
  54. AccountId int64
  55. }
  56. type GetDashboardQuery struct {
  57. Slug string
  58. AccountId int64
  59. Result *Dashboard
  60. }
  61. func convertToStringArray(arr []interface{}) []string {
  62. b := make([]string, len(arr))
  63. for i := range arr {
  64. b[i] = arr[i].(string)
  65. }
  66. return b
  67. }
  68. func NewDashboard(title string) *Dashboard {
  69. dash := &Dashboard{}
  70. dash.Data = make(map[string]interface{})
  71. dash.Data["title"] = title
  72. dash.Title = title
  73. dash.UpdateSlug()
  74. return dash
  75. }
  76. func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
  77. dash := &Dashboard{}
  78. dash.Data = cmd.Dashboard
  79. dash.Title = dash.Data["title"].(string)
  80. dash.AccountId = cmd.AccountId
  81. dash.Tags = convertToStringArray(dash.Data["tags"].([]interface{}))
  82. dash.UpdateSlug()
  83. if dash.Data["id"] != nil {
  84. dash.Id = int64(dash.Data["id"].(float64))
  85. }
  86. return dash
  87. }
  88. func (dash *Dashboard) GetString(prop string) string {
  89. return dash.Data[prop].(string)
  90. }
  91. func (dash *Dashboard) UpdateSlug() {
  92. title := strings.ToLower(dash.Data["title"].(string))
  93. re := regexp.MustCompile("[^\\w ]+")
  94. re2 := regexp.MustCompile("\\s")
  95. dash.Slug = re2.ReplaceAllString(re.ReplaceAllString(title, ""), "-")
  96. }