dashboards.go 4.7 KB

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