dashboards.go 4.2 KB

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