dashboards.go 3.2 KB

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