dashboards.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 := dash.Data.Get("title").MustString()
  119. dash.Slug = SlugifyTitle(title)
  120. }
  121. func SlugifyTitle(title string) string {
  122. return slug.Make(strings.ToLower(title))
  123. }
  124. //
  125. // COMMANDS
  126. //
  127. type SaveDashboardCommand struct {
  128. Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
  129. UserId int64 `json:"userId"`
  130. Overwrite bool `json:"overwrite"`
  131. Message string `json:"message"`
  132. OrgId int64 `json:"-"`
  133. RestoredFrom int `json:"-"`
  134. PluginId string `json:"-"`
  135. FolderId int64 `json:"folderId"`
  136. IsFolder bool `json:"isFolder"`
  137. UpdatedAt time.Time
  138. Result *Dashboard
  139. }
  140. type DashboardProvisioning struct {
  141. Id int64
  142. DashboardId int64
  143. Name string
  144. ExternalId string
  145. Updated time.Time
  146. }
  147. type SaveProvisionedDashboardCommand struct {
  148. DashboardCmd *SaveDashboardCommand
  149. DashboardProvisioning *DashboardProvisioning
  150. Result *Dashboard
  151. }
  152. type DeleteDashboardCommand struct {
  153. Id int64
  154. OrgId int64
  155. }
  156. //
  157. // QUERIES
  158. //
  159. type GetDashboardQuery struct {
  160. Slug string // required if no Id is specified
  161. Id int64 // optional if slug is set
  162. OrgId int64
  163. Result *Dashboard
  164. }
  165. type DashboardTagCloudItem struct {
  166. Term string `json:"term"`
  167. Count int `json:"count"`
  168. }
  169. type GetDashboardTagsQuery struct {
  170. OrgId int64
  171. Result []*DashboardTagCloudItem
  172. }
  173. type GetDashboardsQuery struct {
  174. DashboardIds []int64
  175. Result []*Dashboard
  176. }
  177. type GetDashboardsByPluginIdQuery struct {
  178. OrgId int64
  179. PluginId string
  180. Result []*Dashboard
  181. }
  182. type GetDashboardSlugByIdQuery struct {
  183. Id int64
  184. Result string
  185. }
  186. type GetProvisionedDashboardDataQuery struct {
  187. Name string
  188. Result []*DashboardProvisioning
  189. }