dashboards.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. )
  18. type UpdatePluginDashboardError struct {
  19. PluginId string
  20. }
  21. func (d UpdatePluginDashboardError) Error() string {
  22. return "Dashboard belong to plugin"
  23. }
  24. var (
  25. DashTypeJson = "file"
  26. DashTypeDB = "db"
  27. DashTypeScript = "script"
  28. DashTypeSnapshot = "snapshot"
  29. )
  30. // Dashboard model
  31. type Dashboard struct {
  32. Id int64
  33. Slug string
  34. OrgId int64
  35. GnetId int64
  36. Version int
  37. PluginId string
  38. Created time.Time
  39. Updated time.Time
  40. UpdatedBy int64
  41. CreatedBy int64
  42. ParentId int64
  43. IsFolder bool
  44. HasAcl bool
  45. Title string
  46. Data *simplejson.Json
  47. }
  48. // NewDashboard creates a new dashboard
  49. func NewDashboard(title string) *Dashboard {
  50. dash := &Dashboard{}
  51. dash.Data = simplejson.New()
  52. dash.Data.Set("title", title)
  53. dash.Title = title
  54. dash.Created = time.Now()
  55. dash.Updated = time.Now()
  56. dash.UpdateSlug()
  57. return dash
  58. }
  59. // GetTags turns the tags in data json into go string array
  60. func (dash *Dashboard) GetTags() []string {
  61. return dash.Data.Get("tags").MustStringArray()
  62. }
  63. func NewDashboardFromJson(data *simplejson.Json) *Dashboard {
  64. dash := &Dashboard{}
  65. dash.Data = data
  66. dash.Title = dash.Data.Get("title").MustString()
  67. dash.UpdateSlug()
  68. if id, err := dash.Data.Get("id").Float64(); err == nil {
  69. dash.Id = int64(id)
  70. if version, err := dash.Data.Get("version").Float64(); err == nil {
  71. dash.Version = int(version)
  72. dash.Updated = time.Now()
  73. }
  74. } else {
  75. dash.Data.Set("version", 0)
  76. dash.Created = time.Now()
  77. dash.Updated = time.Now()
  78. }
  79. if gnetId, err := dash.Data.Get("gnetId").Float64(); err == nil {
  80. dash.GnetId = int64(gnetId)
  81. }
  82. return dash
  83. }
  84. // GetDashboardModel turns the command into the savable model
  85. func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
  86. dash := NewDashboardFromJson(cmd.Dashboard)
  87. userId := cmd.UserId
  88. if userId == 0 {
  89. userId = -1
  90. }
  91. if dash.Data.Get("version").MustInt(0) == 0 {
  92. dash.CreatedBy = userId
  93. }
  94. dash.UpdatedBy = userId
  95. dash.OrgId = cmd.OrgId
  96. dash.PluginId = cmd.PluginId
  97. dash.IsFolder = cmd.IsFolder
  98. dash.ParentId = cmd.ParentId
  99. dash.UpdateSlug()
  100. return dash
  101. }
  102. // GetString a
  103. func (dash *Dashboard) GetString(prop string, defaultValue string) string {
  104. return dash.Data.Get(prop).MustString(defaultValue)
  105. }
  106. // UpdateSlug updates the slug
  107. func (dash *Dashboard) UpdateSlug() {
  108. title := strings.ToLower(dash.Data.Get("title").MustString())
  109. dash.Slug = slug.Make(title)
  110. }
  111. //
  112. // COMMANDS
  113. //
  114. type SaveDashboardCommand struct {
  115. Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
  116. UserId int64 `json:"userId"`
  117. Overwrite bool `json:"overwrite"`
  118. Message string `json:"message"`
  119. OrgId int64 `json:"-"`
  120. RestoredFrom int `json:"-"`
  121. PluginId string `json:"-"`
  122. ParentId int64 `json:"parentId"`
  123. IsFolder bool `json:"isFolder"`
  124. Result *Dashboard
  125. }
  126. type DeleteDashboardCommand struct {
  127. Id int64
  128. OrgId int64
  129. }
  130. //
  131. // QUERIES
  132. //
  133. type GetDashboardQuery struct {
  134. Slug string // required if no Id is specified
  135. Id int64 // optional if slug is set
  136. OrgId int64
  137. Result *Dashboard
  138. }
  139. type DashboardTagCloudItem struct {
  140. Term string `json:"term"`
  141. Count int `json:"count"`
  142. }
  143. type GetDashboardTagsQuery struct {
  144. OrgId int64
  145. Result []*DashboardTagCloudItem
  146. }
  147. type GetDashboardsQuery struct {
  148. DashboardIds []int64
  149. Result []*Dashboard
  150. }
  151. type GetDashboardsByPluginIdQuery struct {
  152. OrgId int64
  153. PluginId string
  154. Result []*Dashboard
  155. }
  156. type GetDashboardSlugByIdQuery struct {
  157. Id int64
  158. Result string
  159. }
  160. type GetAllowedDashboardsQuery struct {
  161. UserId int64
  162. OrgId int64
  163. DashList []int64
  164. Result []int64
  165. }