2014-08-08 05:35:15 -05:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2014-11-20 05:11:07 -06:00
|
|
|
"errors"
|
2018-01-30 08:24:14 -06:00
|
|
|
"fmt"
|
2014-08-22 08:32:42 -05:00
|
|
|
"strings"
|
2014-08-21 15:09:48 -05:00
|
|
|
"time"
|
2015-04-15 05:36:02 -05:00
|
|
|
|
2015-06-12 01:53:20 -05:00
|
|
|
"github.com/gosimple/slug"
|
2016-03-11 17:13:06 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2018-01-30 08:24:14 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2014-08-08 05:35:15 -05:00
|
|
|
)
|
|
|
|
|
2014-11-20 05:11:07 -06:00
|
|
|
// Typed errors
|
|
|
|
var (
|
2018-03-27 08:12:47 -05:00
|
|
|
ErrDashboardNotFound = errors.New("Dashboard not found")
|
|
|
|
ErrDashboardFolderNotFound = errors.New("Folder not found")
|
|
|
|
ErrDashboardSnapshotNotFound = errors.New("Dashboard snapshot not found")
|
|
|
|
ErrDashboardWithSameUIDExists = errors.New("A dashboard with the same uid already exists")
|
|
|
|
ErrDashboardWithSameNameInFolderExists = errors.New("A dashboard with the same name in the folder already exists")
|
|
|
|
ErrDashboardVersionMismatch = errors.New("The dashboard has been changed by someone else")
|
|
|
|
ErrDashboardTitleEmpty = errors.New("Dashboard title cannot be empty")
|
|
|
|
ErrDashboardFolderCannotHaveParent = errors.New("A Dashboard Folder cannot be added to another folder")
|
|
|
|
ErrDashboardFailedToUpdateAlertData = errors.New("Failed to save alert data")
|
|
|
|
ErrDashboardsWithSameSlugExists = errors.New("Multiple dashboards with the same slug exists")
|
|
|
|
ErrDashboardFailedGenerateUniqueUid = errors.New("Failed to generate unique dashboard id")
|
|
|
|
ErrDashboardTypeMismatch = errors.New("Dashboard cannot be changed to a folder")
|
|
|
|
ErrDashboardFolderWithSameNameAsDashboard = errors.New("Folder name cannot be the same as one of its dashboards")
|
|
|
|
ErrDashboardWithSameNameAsFolder = errors.New("Dashboard name cannot be the same as folder")
|
|
|
|
ErrDashboardFolderNameExists = errors.New("A folder with that name already exists")
|
|
|
|
ErrDashboardUpdateAccessDenied = errors.New("Access denied to save dashboard")
|
|
|
|
ErrDashboardInvalidUid = errors.New("uid contains illegal characters")
|
|
|
|
ErrDashboardUidToLong = errors.New("uid to long. max 40 characters")
|
2018-04-10 02:32:05 -05:00
|
|
|
ErrDashboardCannotSaveProvisionedDashboard = errors.New("Cannot save provisioned dashboard")
|
2018-04-13 10:11:52 -05:00
|
|
|
RootFolderName = "General"
|
2014-10-06 14:31:54 -05:00
|
|
|
)
|
|
|
|
|
2016-07-08 06:41:46 -05:00
|
|
|
type UpdatePluginDashboardError struct {
|
|
|
|
PluginId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d UpdatePluginDashboardError) Error() string {
|
|
|
|
return "Dashboard belong to plugin"
|
|
|
|
}
|
|
|
|
|
2015-05-12 07:11:30 -05:00
|
|
|
var (
|
2015-05-13 02:58:45 -05:00
|
|
|
DashTypeJson = "file"
|
|
|
|
DashTypeDB = "db"
|
|
|
|
DashTypeScript = "script"
|
|
|
|
DashTypeSnapshot = "snapshot"
|
2015-05-12 07:11:30 -05:00
|
|
|
)
|
|
|
|
|
2015-03-19 09:32:47 -05:00
|
|
|
// Dashboard model
|
2014-08-08 05:35:15 -05:00
|
|
|
type Dashboard struct {
|
2016-07-08 02:35:06 -05:00
|
|
|
Id int64
|
2018-01-29 10:20:18 -06:00
|
|
|
Uid string
|
2016-07-08 02:35:06 -05:00
|
|
|
Slug string
|
|
|
|
OrgId int64
|
|
|
|
GnetId int64
|
|
|
|
Version int
|
|
|
|
PluginId string
|
2014-11-20 05:11:07 -06:00
|
|
|
|
2015-01-07 05:37:24 -06:00
|
|
|
Created time.Time
|
|
|
|
Updated time.Time
|
2014-08-21 15:09:48 -05:00
|
|
|
|
2015-12-18 03:52:05 -06:00
|
|
|
UpdatedBy int64
|
2016-01-28 00:00:24 -06:00
|
|
|
CreatedBy int64
|
2017-06-23 15:00:26 -05:00
|
|
|
FolderId int64
|
2017-03-27 07:36:28 -05:00
|
|
|
IsFolder bool
|
2017-04-28 14:22:53 -05:00
|
|
|
HasAcl bool
|
2015-12-18 02:20:23 -06:00
|
|
|
|
2014-08-21 15:09:48 -05:00
|
|
|
Title string
|
2016-03-11 17:13:06 -06:00
|
|
|
Data *simplejson.Json
|
2014-08-21 15:09:48 -05:00
|
|
|
}
|
|
|
|
|
2018-01-25 08:25:07 -06:00
|
|
|
func (d *Dashboard) SetId(id int64) {
|
|
|
|
d.Id = id
|
|
|
|
d.Data.Set("id", id)
|
|
|
|
}
|
|
|
|
|
2018-02-19 04:12:56 -06:00
|
|
|
func (d *Dashboard) SetUid(uid string) {
|
|
|
|
d.Uid = uid
|
|
|
|
d.Data.Set("uid", uid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dashboard) SetVersion(version int) {
|
|
|
|
d.Version = version
|
|
|
|
d.Data.Set("version", version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDashboardIdForSavePermissionCheck return the dashboard id to be used for checking permission of dashboard
|
|
|
|
func (d *Dashboard) GetDashboardIdForSavePermissionCheck() int64 {
|
|
|
|
if d.Id == 0 {
|
|
|
|
return d.FolderId
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.Id
|
|
|
|
}
|
|
|
|
|
2015-03-19 09:32:47 -05:00
|
|
|
// NewDashboard creates a new dashboard
|
2014-08-08 05:35:15 -05:00
|
|
|
func NewDashboard(title string) *Dashboard {
|
|
|
|
dash := &Dashboard{}
|
2016-03-11 17:13:06 -06:00
|
|
|
dash.Data = simplejson.New()
|
|
|
|
dash.Data.Set("title", title)
|
2014-08-22 08:32:42 -05:00
|
|
|
dash.Title = title
|
2015-11-20 06:37:24 -06:00
|
|
|
dash.Created = time.Now()
|
2015-11-20 06:52:50 -06:00
|
|
|
dash.Updated = time.Now()
|
|
|
|
dash.UpdateSlug()
|
2014-08-08 05:35:15 -05:00
|
|
|
return dash
|
|
|
|
}
|
|
|
|
|
2017-11-28 05:49:37 -06:00
|
|
|
// NewDashboardFolder creates a new dashboard folder
|
|
|
|
func NewDashboardFolder(title string) *Dashboard {
|
|
|
|
folder := NewDashboard(title)
|
2018-01-29 06:51:01 -06:00
|
|
|
folder.IsFolder = true
|
2017-11-28 05:49:37 -06:00
|
|
|
folder.Data.Set("schemaVersion", 16)
|
2018-01-29 06:51:01 -06:00
|
|
|
folder.Data.Set("version", 0)
|
2018-02-19 04:12:56 -06:00
|
|
|
folder.IsFolder = true
|
2017-11-28 05:49:37 -06:00
|
|
|
return folder
|
|
|
|
}
|
|
|
|
|
2015-03-19 09:32:47 -05:00
|
|
|
// GetTags turns the tags in data json into go string array
|
2015-01-07 05:37:24 -06:00
|
|
|
func (dash *Dashboard) GetTags() []string {
|
2016-03-11 17:13:06 -06:00
|
|
|
return dash.Data.Get("tags").MustStringArray()
|
2015-01-07 05:37:24 -06:00
|
|
|
}
|
|
|
|
|
2016-03-11 17:13:06 -06:00
|
|
|
func NewDashboardFromJson(data *simplejson.Json) *Dashboard {
|
2014-12-22 05:25:08 -06:00
|
|
|
dash := &Dashboard{}
|
2015-05-12 05:20:03 -05:00
|
|
|
dash.Data = data
|
2016-03-11 17:13:06 -06:00
|
|
|
dash.Title = dash.Data.Get("title").MustString()
|
2015-11-20 06:52:50 -06:00
|
|
|
dash.UpdateSlug()
|
2018-02-08 05:48:38 -06:00
|
|
|
update := false
|
2014-08-08 05:35:15 -05:00
|
|
|
|
2016-03-11 17:13:06 -06:00
|
|
|
if id, err := dash.Data.Get("id").Float64(); err == nil {
|
|
|
|
dash.Id = int64(id)
|
2018-02-08 05:48:38 -06:00
|
|
|
update = true
|
|
|
|
}
|
2015-03-02 15:24:01 -06:00
|
|
|
|
2018-02-08 05:48:38 -06:00
|
|
|
if uid, err := dash.Data.Get("uid").String(); err == nil {
|
|
|
|
dash.Uid = uid
|
|
|
|
update = true
|
|
|
|
}
|
2015-03-02 15:24:01 -06:00
|
|
|
|
2018-02-08 05:48:38 -06:00
|
|
|
if version, err := dash.Data.Get("version").Float64(); err == nil && update {
|
|
|
|
dash.Version = int(version)
|
|
|
|
dash.Updated = time.Now()
|
2015-03-19 09:32:47 -05:00
|
|
|
} else {
|
2016-03-11 17:13:06 -06:00
|
|
|
dash.Data.Set("version", 0)
|
2015-11-20 06:52:50 -06:00
|
|
|
dash.Created = time.Now()
|
|
|
|
dash.Updated = time.Now()
|
2014-08-08 05:35:15 -05:00
|
|
|
}
|
|
|
|
|
2016-05-27 09:42:32 -05:00
|
|
|
if gnetId, err := dash.Data.Get("gnetId").Float64(); err == nil {
|
|
|
|
dash.GnetId = int64(gnetId)
|
|
|
|
}
|
|
|
|
|
2014-12-22 05:25:08 -06:00
|
|
|
return dash
|
2014-08-08 05:35:15 -05:00
|
|
|
}
|
|
|
|
|
2018-04-13 11:40:14 -05:00
|
|
|
// GetDashboardModel turns the command into the saveable model
|
2015-05-12 05:20:03 -05:00
|
|
|
func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
|
|
|
|
dash := NewDashboardFromJson(cmd.Dashboard)
|
2017-06-05 09:34:32 -05:00
|
|
|
userId := cmd.UserId
|
|
|
|
|
|
|
|
if userId == 0 {
|
|
|
|
userId = -1
|
|
|
|
}
|
2016-03-11 17:13:06 -06:00
|
|
|
|
2017-06-05 09:34:32 -05:00
|
|
|
dash.UpdatedBy = userId
|
2016-01-28 00:00:24 -06:00
|
|
|
dash.OrgId = cmd.OrgId
|
2016-07-08 05:26:51 -05:00
|
|
|
dash.PluginId = cmd.PluginId
|
2017-03-27 07:36:28 -05:00
|
|
|
dash.IsFolder = cmd.IsFolder
|
2017-06-23 15:00:26 -05:00
|
|
|
dash.FolderId = cmd.FolderId
|
2015-05-12 05:20:03 -05:00
|
|
|
dash.UpdateSlug()
|
|
|
|
return dash
|
|
|
|
}
|
|
|
|
|
2015-03-19 09:32:47 -05:00
|
|
|
// GetString a
|
2016-03-10 12:57:48 -06:00
|
|
|
func (dash *Dashboard) GetString(prop string, defaultValue string) string {
|
2016-03-11 17:13:06 -06:00
|
|
|
return dash.Data.Get(prop).MustString(defaultValue)
|
2014-08-08 05:35:15 -05:00
|
|
|
}
|
2014-08-22 08:32:42 -05:00
|
|
|
|
2015-03-19 09:32:47 -05:00
|
|
|
// UpdateSlug updates the slug
|
2014-08-22 08:32:42 -05:00
|
|
|
func (dash *Dashboard) UpdateSlug() {
|
2017-12-27 09:32:39 -06:00
|
|
|
title := dash.Data.Get("title").MustString()
|
|
|
|
dash.Slug = SlugifyTitle(title)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SlugifyTitle(title string) string {
|
|
|
|
return slug.Make(strings.ToLower(title))
|
2014-08-22 08:32:42 -05:00
|
|
|
}
|
2015-01-09 04:01:37 -06:00
|
|
|
|
2018-01-31 16:14:48 -06:00
|
|
|
// GetUrl return the html url for a folder if it's folder, otherwise for a dashboard
|
|
|
|
func (dash *Dashboard) GetUrl() string {
|
|
|
|
return GetDashboardFolderUrl(dash.IsFolder, dash.Uid, dash.Slug)
|
|
|
|
}
|
|
|
|
|
2018-02-01 06:49:34 -06:00
|
|
|
// Return the html url for a dashboard
|
|
|
|
func (dash *Dashboard) GenerateUrl() string {
|
|
|
|
return GetDashboardUrl(dash.Uid, dash.Slug)
|
|
|
|
}
|
|
|
|
|
2018-01-31 16:14:48 -06:00
|
|
|
// GetDashboardFolderUrl return the html url for a folder if it's folder, otherwise for a dashboard
|
|
|
|
func GetDashboardFolderUrl(isFolder bool, uid string, slug string) string {
|
|
|
|
if isFolder {
|
|
|
|
return GetFolderUrl(uid, slug)
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetDashboardUrl(uid, slug)
|
|
|
|
}
|
|
|
|
|
2018-03-27 13:24:11 -05:00
|
|
|
// GetDashboardUrl return the html url for a dashboard
|
2018-01-30 08:24:14 -06:00
|
|
|
func GetDashboardUrl(uid string, slug string) string {
|
|
|
|
return fmt.Sprintf("%s/d/%s/%s", setting.AppSubUrl, uid, slug)
|
|
|
|
}
|
|
|
|
|
2018-03-27 13:24:11 -05:00
|
|
|
// GetFullDashboardUrl return the full url for a dashboard
|
2018-02-01 06:32:00 -06:00
|
|
|
func GetFullDashboardUrl(uid string, slug string) string {
|
2018-03-27 13:24:11 -05:00
|
|
|
return fmt.Sprintf("%sd/%s/%s", setting.AppUrl, uid, slug)
|
2018-02-01 06:32:00 -06:00
|
|
|
}
|
|
|
|
|
2018-01-30 08:24:14 -06:00
|
|
|
// GetFolderUrl return the html url for a folder
|
|
|
|
func GetFolderUrl(folderUid string, slug string) string {
|
2018-01-31 09:51:06 -06:00
|
|
|
return fmt.Sprintf("%s/dashboards/f/%s/%s", setting.AppSubUrl, folderUid, slug)
|
2018-01-30 08:24:14 -06:00
|
|
|
}
|
|
|
|
|
2018-04-10 17:23:50 -05:00
|
|
|
type ValidateDashboardBeforeSaveResult struct {
|
|
|
|
IsParentFolderChanged bool
|
|
|
|
}
|
|
|
|
|
2015-01-09 04:01:37 -06:00
|
|
|
//
|
|
|
|
// COMMANDS
|
|
|
|
//
|
|
|
|
|
|
|
|
type SaveDashboardCommand struct {
|
2017-06-05 15:59:04 -05:00
|
|
|
Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
|
|
|
|
UserId int64 `json:"userId"`
|
|
|
|
Overwrite bool `json:"overwrite"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
OrgId int64 `json:"-"`
|
|
|
|
RestoredFrom int `json:"-"`
|
|
|
|
PluginId string `json:"-"`
|
2017-06-23 15:00:26 -05:00
|
|
|
FolderId int64 `json:"folderId"`
|
2017-06-09 16:19:58 -05:00
|
|
|
IsFolder bool `json:"isFolder"`
|
2015-01-09 04:01:37 -06:00
|
|
|
|
2017-11-23 04:29:06 -06:00
|
|
|
UpdatedAt time.Time
|
|
|
|
|
2015-01-09 04:01:37 -06:00
|
|
|
Result *Dashboard
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:28:56 -06:00
|
|
|
type DashboardProvisioning struct {
|
|
|
|
Id int64
|
|
|
|
DashboardId int64
|
|
|
|
Name string
|
|
|
|
ExternalId string
|
2018-05-31 07:13:34 -05:00
|
|
|
CheckSum string
|
2018-02-14 08:28:30 -06:00
|
|
|
Updated int64
|
2018-01-23 05:28:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type SaveProvisionedDashboardCommand struct {
|
|
|
|
DashboardCmd *SaveDashboardCommand
|
|
|
|
DashboardProvisioning *DashboardProvisioning
|
|
|
|
|
|
|
|
Result *Dashboard
|
|
|
|
}
|
|
|
|
|
2015-01-09 04:01:37 -06:00
|
|
|
type DeleteDashboardCommand struct {
|
2017-06-17 17:24:38 -05:00
|
|
|
Id int64
|
2015-02-23 13:07:49 -06:00
|
|
|
OrgId int64
|
2015-01-09 04:01:37 -06:00
|
|
|
}
|
|
|
|
|
2018-02-19 04:12:56 -06:00
|
|
|
type ValidateDashboardBeforeSaveCommand struct {
|
|
|
|
OrgId int64
|
|
|
|
Dashboard *Dashboard
|
|
|
|
Overwrite bool
|
2018-04-10 17:23:50 -05:00
|
|
|
Result *ValidateDashboardBeforeSaveResult
|
2018-02-19 04:12:56 -06:00
|
|
|
}
|
|
|
|
|
2015-01-09 04:01:37 -06:00
|
|
|
//
|
|
|
|
// QUERIES
|
|
|
|
//
|
|
|
|
|
|
|
|
type GetDashboardQuery struct {
|
2018-01-29 14:23:07 -06:00
|
|
|
Slug string // required if no Id or Uid is specified
|
2017-06-05 10:45:27 -05:00
|
|
|
Id int64 // optional if slug is set
|
2018-01-29 14:23:07 -06:00
|
|
|
Uid string // optional if slug is set
|
2015-02-23 13:07:49 -06:00
|
|
|
OrgId int64
|
2015-01-09 04:01:37 -06:00
|
|
|
|
|
|
|
Result *Dashboard
|
|
|
|
}
|
2015-05-13 06:36:13 -05:00
|
|
|
|
|
|
|
type DashboardTagCloudItem struct {
|
|
|
|
Term string `json:"term"`
|
|
|
|
Count int `json:"count"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetDashboardTagsQuery struct {
|
|
|
|
OrgId int64
|
|
|
|
Result []*DashboardTagCloudItem
|
|
|
|
}
|
2016-01-28 18:41:23 -06:00
|
|
|
|
|
|
|
type GetDashboardsQuery struct {
|
|
|
|
DashboardIds []int64
|
2016-07-08 02:35:06 -05:00
|
|
|
Result []*Dashboard
|
|
|
|
}
|
|
|
|
|
2018-01-30 16:31:02 -06:00
|
|
|
type GetDashboardPermissionsForUserQuery struct {
|
|
|
|
DashboardIds []int64
|
|
|
|
OrgId int64
|
|
|
|
UserId int64
|
|
|
|
OrgRole RoleType
|
|
|
|
Result []*DashboardPermissionForUser
|
|
|
|
}
|
|
|
|
|
2016-07-08 02:35:06 -05:00
|
|
|
type GetDashboardsByPluginIdQuery struct {
|
|
|
|
OrgId int64
|
|
|
|
PluginId string
|
|
|
|
Result []*Dashboard
|
2016-01-28 18:41:23 -06:00
|
|
|
}
|
2016-03-17 03:01:58 -05:00
|
|
|
|
|
|
|
type GetDashboardSlugByIdQuery struct {
|
|
|
|
Id int64
|
|
|
|
Result string
|
|
|
|
}
|
2018-01-31 09:51:06 -06:00
|
|
|
|
2018-04-10 05:30:37 -05:00
|
|
|
type IsDashboardProvisionedQuery struct {
|
2018-02-27 11:51:04 -06:00
|
|
|
DashboardId int64
|
|
|
|
|
2018-04-10 05:30:37 -05:00
|
|
|
Result bool
|
2018-02-27 11:51:04 -06:00
|
|
|
}
|
|
|
|
|
2018-01-23 05:28:56 -06:00
|
|
|
type GetProvisionedDashboardDataQuery struct {
|
|
|
|
Name string
|
|
|
|
|
|
|
|
Result []*DashboardProvisioning
|
|
|
|
}
|
2018-02-08 04:01:09 -06:00
|
|
|
|
2018-01-31 09:51:06 -06:00
|
|
|
type GetDashboardsBySlugQuery struct {
|
|
|
|
OrgId int64
|
|
|
|
Slug string
|
|
|
|
|
|
|
|
Result []*Dashboard
|
|
|
|
}
|
2018-02-01 04:08:39 -06:00
|
|
|
|
2018-01-30 16:31:02 -06:00
|
|
|
type DashboardPermissionForUser struct {
|
|
|
|
DashboardId int64 `json:"dashboardId"`
|
|
|
|
Permission PermissionType `json:"permission"`
|
|
|
|
PermissionName string `json:"permissionName"`
|
|
|
|
}
|
2018-02-01 06:32:00 -06:00
|
|
|
|
|
|
|
type DashboardRef struct {
|
|
|
|
Uid string
|
|
|
|
Slug string
|
|
|
|
}
|
|
|
|
|
2018-02-05 03:24:48 -06:00
|
|
|
type GetDashboardRefByIdQuery struct {
|
2018-02-01 06:32:00 -06:00
|
|
|
Id int64
|
|
|
|
Result *DashboardRef
|
|
|
|
}
|