dashboards.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package models
  2. import (
  3. "errors"
  4. "strings"
  5. "time"
  6. "github.com/gosimple/slug"
  7. )
  8. // Typed errors
  9. var (
  10. ErrDashboardNotFound = errors.New("Dashboard not found")
  11. ErrDashboardSnapshotNotFound = errors.New("Dashboard snapshot not found")
  12. ErrDashboardWithSameNameExists = errors.New("A dashboard with the same name already exists")
  13. ErrDashboardVersionMismatch = errors.New("The dashboard has been changed by someone else")
  14. )
  15. var (
  16. DashTypeJson = "file"
  17. DashTypeDB = "db"
  18. DashTypeScript = "script"
  19. DashTypeSnapshot = "snapshot"
  20. )
  21. // Dashboard model
  22. type Dashboard struct {
  23. Id int64
  24. Slug string
  25. OrgId int64
  26. Version int
  27. Created time.Time
  28. Updated time.Time
  29. UpdatedBy int64
  30. CreatedBy int64
  31. Title string
  32. Data map[string]interface{}
  33. }
  34. // NewDashboard creates a new dashboard
  35. func NewDashboard(title string) *Dashboard {
  36. dash := &Dashboard{}
  37. dash.Data = make(map[string]interface{})
  38. dash.Data["title"] = title
  39. dash.Title = title
  40. dash.Created = time.Now()
  41. dash.Updated = time.Now()
  42. dash.UpdateSlug()
  43. return dash
  44. }
  45. // GetTags turns the tags in data json into go string array
  46. func (dash *Dashboard) GetTags() []string {
  47. jsonTags := dash.Data["tags"]
  48. if jsonTags == nil || jsonTags == "" {
  49. return []string{}
  50. }
  51. arr := jsonTags.([]interface{})
  52. b := make([]string, len(arr))
  53. for i := range arr {
  54. b[i] = arr[i].(string)
  55. }
  56. return b
  57. }
  58. func NewDashboardFromJson(data map[string]interface{}) *Dashboard {
  59. dash := &Dashboard{}
  60. dash.Data = data
  61. dash.Title = dash.Data["title"].(string)
  62. dash.UpdateSlug()
  63. if dash.Data["id"] != nil {
  64. dash.Id = int64(dash.Data["id"].(float64))
  65. if dash.Data["version"] != nil {
  66. dash.Version = int(dash.Data["version"].(float64))
  67. dash.Updated = time.Now()
  68. }
  69. } else {
  70. dash.Data["version"] = 0
  71. dash.Created = time.Now()
  72. dash.Updated = time.Now()
  73. }
  74. return dash
  75. }
  76. // GetDashboardModel turns the command into the savable model
  77. func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
  78. dash := NewDashboardFromJson(cmd.Dashboard)
  79. if dash.Data["version"] == 0 {
  80. dash.CreatedBy = cmd.UserId
  81. }
  82. dash.UpdatedBy = cmd.UserId
  83. dash.OrgId = cmd.OrgId
  84. dash.UpdateSlug()
  85. return dash
  86. }
  87. // GetString a
  88. func (dash *Dashboard) GetString(prop string) string {
  89. return dash.Data[prop].(string)
  90. }
  91. // UpdateSlug updates the slug
  92. func (dash *Dashboard) UpdateSlug() {
  93. title := strings.ToLower(dash.Data["title"].(string))
  94. dash.Slug = slug.Make(title)
  95. }
  96. //
  97. // COMMANDS
  98. //
  99. type SaveDashboardCommand struct {
  100. Dashboard map[string]interface{} `json:"dashboard" binding:"Required"`
  101. UserId int64 `json:"userId"`
  102. OrgId int64 `json:"-"`
  103. Overwrite bool `json:"overwrite"`
  104. Result *Dashboard
  105. }
  106. type DeleteDashboardCommand struct {
  107. Slug string
  108. OrgId int64
  109. }
  110. //
  111. // QUERIES
  112. //
  113. type GetDashboardQuery struct {
  114. Slug string
  115. OrgId int64
  116. Result *Dashboard
  117. }
  118. type DashboardTagCloudItem struct {
  119. Term string `json:"term"`
  120. Count int `json:"count"`
  121. }
  122. type GetDashboardTagsQuery struct {
  123. OrgId int64
  124. Result []*DashboardTagCloudItem
  125. }