dashboards.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. Title string
  31. Data map[string]interface{}
  32. }
  33. // NewDashboard creates a new dashboard
  34. func NewDashboard(title string) *Dashboard {
  35. dash := &Dashboard{}
  36. dash.Data = make(map[string]interface{})
  37. dash.Data["title"] = title
  38. dash.Title = title
  39. dash.Created = time.Now()
  40. dash.Updated = time.Now()
  41. dash.UpdateSlug()
  42. return dash
  43. }
  44. // GetTags turns the tags in data json into go string array
  45. func (dash *Dashboard) GetTags() []string {
  46. jsonTags := dash.Data["tags"]
  47. if jsonTags == nil || jsonTags == "" {
  48. return []string{}
  49. }
  50. arr := jsonTags.([]interface{})
  51. b := make([]string, len(arr))
  52. for i := range arr {
  53. b[i] = arr[i].(string)
  54. }
  55. return b
  56. }
  57. func NewDashboardFromJson(data map[string]interface{}) *Dashboard {
  58. dash := &Dashboard{}
  59. dash.Data = data
  60. dash.Title = dash.Data["title"].(string)
  61. dash.UpdateSlug()
  62. if dash.Data["id"] != nil {
  63. dash.Id = int64(dash.Data["id"].(float64))
  64. if dash.Data["version"] != nil {
  65. dash.Version = int(dash.Data["version"].(float64))
  66. dash.Updated = time.Now()
  67. }
  68. } else {
  69. dash.Data["version"] = 0
  70. dash.Created = time.Now()
  71. dash.Updated = time.Now()
  72. }
  73. return dash
  74. }
  75. // GetDashboardModel turns the command into the savable model
  76. func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
  77. dash := NewDashboardFromJson(cmd.Dashboard)
  78. dash.OrgId = cmd.OrgId
  79. dash.UpdatedBy = cmd.UpdatedBy
  80. dash.UpdateSlug()
  81. return dash
  82. }
  83. // GetString a
  84. func (dash *Dashboard) GetString(prop string) string {
  85. return dash.Data[prop].(string)
  86. }
  87. // UpdateSlug updates the slug
  88. func (dash *Dashboard) UpdateSlug() {
  89. title := strings.ToLower(dash.Data["title"].(string))
  90. dash.Slug = slug.Make(title)
  91. }
  92. //
  93. // COMMANDS
  94. //
  95. type SaveDashboardCommand struct {
  96. Dashboard map[string]interface{} `json:"dashboard" binding:"Required"`
  97. Overwrite bool `json:"overwrite"`
  98. OrgId int64 `json:"-"`
  99. UpdatedBy int64 `json:"-"`
  100. Result *Dashboard
  101. }
  102. type DeleteDashboardCommand struct {
  103. Slug string
  104. OrgId int64
  105. }
  106. //
  107. // QUERIES
  108. //
  109. type GetDashboardQuery struct {
  110. Slug string
  111. OrgId int64
  112. Result *Dashboard
  113. }
  114. type DashboardTagCloudItem struct {
  115. Term string `json:"term"`
  116. Count int `json:"count"`
  117. }
  118. type GetDashboardTagsQuery struct {
  119. OrgId int64
  120. Result []*DashboardTagCloudItem
  121. }
  122. type GetDashboardsQuery struct {
  123. DashboardIds []int64
  124. Result *[]Dashboard
  125. }