dashboards.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package models
  2. import (
  3. "errors"
  4. "strings"
  5. "time"
  6. "github.com/gosimple/slug"
  7. "github.com/grafana/grafana/pkg/components/simplejson"
  8. )
  9. // Typed errors
  10. var (
  11. ErrDashboardNotFound = errors.New("Dashboard not found")
  12. ErrDashboardSnapshotNotFound = errors.New("Dashboard snapshot not found")
  13. ErrDashboardWithSameNameExists = errors.New("A dashboard with the same name already exists")
  14. ErrDashboardVersionMismatch = errors.New("The dashboard has been changed by someone else")
  15. ErrDashboardTitleEmpty = errors.New("Dashboard title cannot be empty")
  16. )
  17. type UpdatePluginDashboardError struct {
  18. PluginId string
  19. }
  20. func (d UpdatePluginDashboardError) Error() string {
  21. return "Dashboard belong to plugin"
  22. }
  23. var (
  24. DashTypeJson = "file"
  25. DashTypeDB = "db"
  26. DashTypeScript = "script"
  27. DashTypeSnapshot = "snapshot"
  28. )
  29. // Dashboard model
  30. type Dashboard struct {
  31. Id int64
  32. Slug string
  33. OrgId int64
  34. GnetId int64
  35. Version int
  36. PluginId string
  37. Created time.Time
  38. Updated time.Time
  39. UpdatedBy int64
  40. CreatedBy int64
  41. Title string
  42. Data *simplejson.Json
  43. }
  44. // NewDashboard creates a new dashboard
  45. func NewDashboard(title string) *Dashboard {
  46. dash := &Dashboard{}
  47. dash.Data = simplejson.New()
  48. dash.Data.Set("title", title)
  49. dash.Title = title
  50. dash.Created = time.Now()
  51. dash.Updated = time.Now()
  52. dash.UpdateSlug()
  53. return dash
  54. }
  55. // GetTags turns the tags in data json into go string array
  56. func (dash *Dashboard) GetTags() []string {
  57. return dash.Data.Get("tags").MustStringArray()
  58. }
  59. func NewDashboardFromJson(data *simplejson.Json) *Dashboard {
  60. dash := &Dashboard{}
  61. dash.Data = data
  62. dash.Title = dash.Data.Get("title").MustString()
  63. dash.UpdateSlug()
  64. if id, err := dash.Data.Get("id").Float64(); err == nil {
  65. dash.Id = int64(id)
  66. if version, err := dash.Data.Get("version").Float64(); err == nil {
  67. dash.Version = int(version)
  68. dash.Updated = time.Now()
  69. }
  70. } else {
  71. dash.Data.Set("version", 0)
  72. dash.Created = time.Now()
  73. dash.Updated = time.Now()
  74. }
  75. if gnetId, err := dash.Data.Get("gnetId").Float64(); err == nil {
  76. dash.GnetId = int64(gnetId)
  77. }
  78. return dash
  79. }
  80. // GetDashboardModel turns the command into the savable model
  81. func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
  82. dash := NewDashboardFromJson(cmd.Dashboard)
  83. if dash.Data.Get("version").MustInt(0) == 0 {
  84. dash.CreatedBy = cmd.UserId
  85. }
  86. dash.UpdatedBy = cmd.UserId
  87. dash.OrgId = cmd.OrgId
  88. dash.PluginId = cmd.PluginId
  89. dash.UpdateSlug()
  90. return dash
  91. }
  92. // GetString a
  93. func (dash *Dashboard) GetString(prop string, defaultValue string) string {
  94. return dash.Data.Get(prop).MustString(defaultValue)
  95. }
  96. // UpdateSlug updates the slug
  97. func (dash *Dashboard) UpdateSlug() {
  98. title := strings.ToLower(dash.Data.Get("title").MustString())
  99. dash.Slug = slug.Make(title)
  100. }
  101. //
  102. // COMMANDS
  103. //
  104. type SaveDashboardCommand struct {
  105. Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
  106. UserId int64 `json:"userId"`
  107. OrgId int64 `json:"-"`
  108. Overwrite bool `json:"overwrite"`
  109. PluginId string `json:"-"`
  110. Result *Dashboard
  111. }
  112. type DeleteDashboardCommand struct {
  113. Slug string
  114. OrgId int64
  115. }
  116. //
  117. // QUERIES
  118. //
  119. type GetDashboardQuery struct {
  120. Slug string
  121. OrgId int64
  122. Result *Dashboard
  123. }
  124. type DashboardTagCloudItem struct {
  125. Term string `json:"term"`
  126. Count int `json:"count"`
  127. }
  128. type GetDashboardTagsQuery struct {
  129. OrgId int64
  130. Result []*DashboardTagCloudItem
  131. }
  132. type GetDashboardsQuery struct {
  133. DashboardIds []int64
  134. Result []*Dashboard
  135. }
  136. type GetDashboardsByPluginIdQuery struct {
  137. OrgId int64
  138. PluginId string
  139. Result []*Dashboard
  140. }
  141. type GetDashboardSlugByIdQuery struct {
  142. Id int64
  143. Result string
  144. }