dashboards.go 2.7 KB

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