dashboards.go 5.6 KB

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