dashboards.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. func (d *Dashboard) SetId(id int64) {
  51. d.Id = id
  52. d.Data.Set("id", id)
  53. }
  54. // NewDashboard creates a new dashboard
  55. func NewDashboard(title string) *Dashboard {
  56. dash := &Dashboard{}
  57. dash.Data = simplejson.New()
  58. dash.Data.Set("title", title)
  59. dash.Title = title
  60. dash.Created = time.Now()
  61. dash.Updated = time.Now()
  62. dash.UpdateSlug()
  63. return dash
  64. }
  65. // NewDashboardFolder creates a new dashboard folder
  66. func NewDashboardFolder(title string) *Dashboard {
  67. folder := NewDashboard(title)
  68. folder.Data.Set("schemaVersion", 16)
  69. folder.Data.Set("editable", true)
  70. folder.Data.Set("hideControls", true)
  71. return folder
  72. }
  73. // GetTags turns the tags in data json into go string array
  74. func (dash *Dashboard) GetTags() []string {
  75. return dash.Data.Get("tags").MustStringArray()
  76. }
  77. func NewDashboardFromJson(data *simplejson.Json) *Dashboard {
  78. dash := &Dashboard{}
  79. dash.Data = data
  80. dash.Title = dash.Data.Get("title").MustString()
  81. dash.UpdateSlug()
  82. if id, err := dash.Data.Get("id").Float64(); err == nil {
  83. dash.Id = int64(id)
  84. if version, err := dash.Data.Get("version").Float64(); err == nil {
  85. dash.Version = int(version)
  86. dash.Updated = time.Now()
  87. }
  88. } else {
  89. dash.Data.Set("version", 0)
  90. dash.Created = time.Now()
  91. dash.Updated = time.Now()
  92. }
  93. if gnetId, err := dash.Data.Get("gnetId").Float64(); err == nil {
  94. dash.GnetId = int64(gnetId)
  95. }
  96. return dash
  97. }
  98. // GetDashboardModel turns the command into the savable model
  99. func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
  100. dash := NewDashboardFromJson(cmd.Dashboard)
  101. userId := cmd.UserId
  102. if userId == 0 {
  103. userId = -1
  104. }
  105. if dash.Data.Get("version").MustInt(0) == 0 {
  106. dash.CreatedBy = userId
  107. }
  108. dash.UpdatedBy = userId
  109. dash.OrgId = cmd.OrgId
  110. dash.PluginId = cmd.PluginId
  111. dash.IsFolder = cmd.IsFolder
  112. dash.FolderId = cmd.FolderId
  113. dash.UpdateSlug()
  114. return dash
  115. }
  116. // GetString a
  117. func (dash *Dashboard) GetString(prop string, defaultValue string) string {
  118. return dash.Data.Get(prop).MustString(defaultValue)
  119. }
  120. // UpdateSlug updates the slug
  121. func (dash *Dashboard) UpdateSlug() {
  122. title := dash.Data.Get("title").MustString()
  123. dash.Slug = SlugifyTitle(title)
  124. }
  125. func SlugifyTitle(title string) string {
  126. return slug.Make(strings.ToLower(title))
  127. }
  128. //
  129. // COMMANDS
  130. //
  131. type SaveDashboardCommand struct {
  132. Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
  133. UserId int64 `json:"userId"`
  134. Overwrite bool `json:"overwrite"`
  135. Message string `json:"message"`
  136. OrgId int64 `json:"-"`
  137. RestoredFrom int `json:"-"`
  138. PluginId string `json:"-"`
  139. FolderId int64 `json:"folderId"`
  140. IsFolder bool `json:"isFolder"`
  141. UpdatedAt time.Time
  142. Result *Dashboard
  143. }
  144. type DashboardProvisioning struct {
  145. Id int64
  146. DashboardId int64
  147. Name string
  148. ExternalId string
  149. Updated time.Time
  150. }
  151. type SaveProvisionedDashboardCommand struct {
  152. DashboardCmd *SaveDashboardCommand
  153. DashboardProvisioning *DashboardProvisioning
  154. Result *Dashboard
  155. }
  156. type DeleteDashboardCommand struct {
  157. Id int64
  158. OrgId int64
  159. }
  160. //
  161. // QUERIES
  162. //
  163. type GetDashboardQuery struct {
  164. Slug string // required if no Id is specified
  165. Id int64 // optional if slug is set
  166. OrgId int64
  167. Result *Dashboard
  168. }
  169. type DashboardTagCloudItem struct {
  170. Term string `json:"term"`
  171. Count int `json:"count"`
  172. }
  173. type GetDashboardTagsQuery struct {
  174. OrgId int64
  175. Result []*DashboardTagCloudItem
  176. }
  177. type GetDashboardsQuery struct {
  178. DashboardIds []int64
  179. Result []*Dashboard
  180. }
  181. type GetDashboardsByPluginIdQuery struct {
  182. OrgId int64
  183. PluginId string
  184. Result []*Dashboard
  185. }
  186. type GetDashboardSlugByIdQuery struct {
  187. Id int64
  188. Result string
  189. }
  190. type GetProvisionedDashboardDataQuery struct {
  191. Name string
  192. Result []*DashboardProvisioning
  193. }