dashboards.go 4.2 KB

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