dashboards.go 5.1 KB

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