dashboards.go 4.1 KB

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