dashboards.go 4.0 KB

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