dashboards.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. ErrDashboardFolderCannotHaveParent = errors.New("A Dashboard Folder cannot be added to another folder")
  17. )
  18. type UpdatePluginDashboardError struct {
  19. PluginId string
  20. }
  21. func (d UpdatePluginDashboardError) Error() string {
  22. return "Dashboard belong to plugin"
  23. }
  24. var (
  25. DashTypeJson = "file"
  26. DashTypeDB = "db"
  27. DashTypeScript = "script"
  28. DashTypeSnapshot = "snapshot"
  29. )
  30. // Dashboard model
  31. type Dashboard struct {
  32. Id int64
  33. Slug string
  34. OrgId int64
  35. GnetId int64
  36. Version int
  37. PluginId string
  38. Created time.Time
  39. Updated time.Time
  40. UpdatedBy int64
  41. CreatedBy int64
  42. FolderId int64
  43. IsFolder bool
  44. HasAcl bool
  45. Title string
  46. Data *simplejson.Json
  47. }
  48. // NewDashboard creates a new dashboard
  49. func NewDashboard(title string) *Dashboard {
  50. dash := &Dashboard{}
  51. dash.Data = simplejson.New()
  52. dash.Data.Set("title", title)
  53. dash.Title = title
  54. dash.Created = time.Now()
  55. dash.Updated = time.Now()
  56. dash.UpdateSlug()
  57. return dash
  58. }
  59. // NewDashboardFolder creates a new dashboard folder
  60. func NewDashboardFolder(title string) *Dashboard {
  61. folder := NewDashboard(title)
  62. folder.Data.Set("schemaVersion", 16)
  63. folder.Data.Set("editable", true)
  64. folder.Data.Set("hideControls", true)
  65. return folder
  66. }
  67. // GetTags turns the tags in data json into go string array
  68. func (dash *Dashboard) GetTags() []string {
  69. return dash.Data.Get("tags").MustStringArray()
  70. }
  71. func NewDashboardFromJson(data *simplejson.Json) *Dashboard {
  72. dash := &Dashboard{}
  73. dash.Data = data
  74. dash.Title = dash.Data.Get("title").MustString()
  75. dash.UpdateSlug()
  76. if id, err := dash.Data.Get("id").Float64(); err == nil {
  77. dash.Id = int64(id)
  78. if version, err := dash.Data.Get("version").Float64(); err == nil {
  79. dash.Version = int(version)
  80. dash.Updated = time.Now()
  81. }
  82. } else {
  83. dash.Data.Set("version", 0)
  84. dash.Created = time.Now()
  85. dash.Updated = time.Now()
  86. }
  87. if gnetId, err := dash.Data.Get("gnetId").Float64(); err == nil {
  88. dash.GnetId = int64(gnetId)
  89. }
  90. return dash
  91. }
  92. // GetDashboardModel turns the command into the savable model
  93. func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
  94. dash := NewDashboardFromJson(cmd.Dashboard)
  95. userId := cmd.UserId
  96. if userId == 0 {
  97. userId = -1
  98. }
  99. if dash.Data.Get("version").MustInt(0) == 0 {
  100. dash.CreatedBy = userId
  101. }
  102. dash.UpdatedBy = userId
  103. dash.OrgId = cmd.OrgId
  104. dash.PluginId = cmd.PluginId
  105. dash.IsFolder = cmd.IsFolder
  106. dash.FolderId = cmd.FolderId
  107. dash.UpdateSlug()
  108. return dash
  109. }
  110. // GetString a
  111. func (dash *Dashboard) GetString(prop string, defaultValue string) string {
  112. return dash.Data.Get(prop).MustString(defaultValue)
  113. }
  114. // UpdateSlug updates the slug
  115. func (dash *Dashboard) UpdateSlug() {
  116. title := strings.ToLower(dash.Data.Get("title").MustString())
  117. dash.Slug = slug.Make(title)
  118. }
  119. //
  120. // COMMANDS
  121. //
  122. type SaveDashboardCommand struct {
  123. Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
  124. UserId int64 `json:"userId"`
  125. Overwrite bool `json:"overwrite"`
  126. Message string `json:"message"`
  127. OrgId int64 `json:"-"`
  128. RestoredFrom int `json:"-"`
  129. PluginId string `json:"-"`
  130. FolderId int64 `json:"folderId"`
  131. IsFolder bool `json:"isFolder"`
  132. Result *Dashboard
  133. }
  134. type DeleteDashboardCommand struct {
  135. Id int64
  136. OrgId int64
  137. }
  138. //
  139. // QUERIES
  140. //
  141. type GetDashboardQuery struct {
  142. Slug string // required if no Id is specified
  143. Id int64 // optional if slug is set
  144. OrgId int64
  145. Result *Dashboard
  146. }
  147. type DashboardTagCloudItem struct {
  148. Term string `json:"term"`
  149. Count int `json:"count"`
  150. }
  151. type GetDashboardTagsQuery struct {
  152. OrgId int64
  153. Result []*DashboardTagCloudItem
  154. }
  155. type GetDashboardsQuery struct {
  156. DashboardIds []int64
  157. Result []*Dashboard
  158. }
  159. type GetDashboardsByPluginIdQuery struct {
  160. OrgId int64
  161. PluginId string
  162. Result []*Dashboard
  163. }
  164. type GetDashboardSlugByIdQuery struct {
  165. Id int64
  166. Result string
  167. }