dashboards.go 3.4 KB

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