dashboards.go 3.6 KB

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