dashboards.go 3.9 KB

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