dashboards.go 3.1 KB

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