dashboards.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. ErrDashboardWithSameUIDExists = errors.New("A dashboard with the same uid 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. )
  23. type UpdatePluginDashboardError struct {
  24. PluginId string
  25. }
  26. func (d UpdatePluginDashboardError) Error() string {
  27. return "Dashboard belong to plugin"
  28. }
  29. var (
  30. DashTypeJson = "file"
  31. DashTypeDB = "db"
  32. DashTypeScript = "script"
  33. DashTypeSnapshot = "snapshot"
  34. )
  35. // Dashboard model
  36. type Dashboard struct {
  37. Id int64
  38. Uid string
  39. Slug string
  40. OrgId int64
  41. GnetId int64
  42. Version int
  43. PluginId string
  44. Created time.Time
  45. Updated time.Time
  46. UpdatedBy int64
  47. CreatedBy int64
  48. FolderId int64
  49. IsFolder bool
  50. HasAcl bool
  51. Title string
  52. Data *simplejson.Json
  53. }
  54. // NewDashboard creates a new dashboard
  55. func NewDashboard(title string) *Dashboard {
  56. dash := &Dashboard{}
  57. dash.Uid = util.GenerateShortUid()
  58. dash.Data = simplejson.New()
  59. dash.Data.Set("title", title)
  60. dash.Title = title
  61. dash.Created = time.Now()
  62. dash.Updated = time.Now()
  63. dash.UpdateSlug()
  64. return dash
  65. }
  66. // NewDashboardFolder creates a new dashboard folder
  67. func NewDashboardFolder(title string) *Dashboard {
  68. folder := NewDashboard(title)
  69. folder.Data.Set("schemaVersion", 16)
  70. folder.Data.Set("editable", true)
  71. folder.Data.Set("hideControls", true)
  72. return folder
  73. }
  74. // GetTags turns the tags in data json into go string array
  75. func (dash *Dashboard) GetTags() []string {
  76. return dash.Data.Get("tags").MustStringArray()
  77. }
  78. func NewDashboardFromJson(data *simplejson.Json) *Dashboard {
  79. dash := &Dashboard{}
  80. dash.Data = data
  81. dash.Title = dash.Data.Get("title").MustString()
  82. dash.UpdateSlug()
  83. if id, err := dash.Data.Get("id").Float64(); err == nil {
  84. dash.Id = int64(id)
  85. if version, err := dash.Data.Get("version").Float64(); err == nil {
  86. dash.Version = int(version)
  87. dash.Updated = time.Now()
  88. }
  89. } else {
  90. dash.Data.Set("version", 0)
  91. dash.Created = time.Now()
  92. dash.Updated = time.Now()
  93. }
  94. if gnetId, err := dash.Data.Get("gnetId").Float64(); err == nil {
  95. dash.GnetId = int64(gnetId)
  96. }
  97. if uid, err := dash.Data.Get("uid").String(); err == nil {
  98. dash.Uid = uid
  99. } else {
  100. dash.Uid = util.GenerateShortUid()
  101. dash.Data.Set("uid", dash.Uid)
  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/f/%v/%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. }