grafana/pkg/models/dashboards.go

300 lines
7.2 KiB
Go
Raw Normal View History

2014-08-08 05:35:15 -05:00
package models
import (
2014-11-20 05:11:07 -06:00
"errors"
"fmt"
"strings"
2014-08-21 15:09:48 -05:00
"time"
"github.com/gosimple/slug"
2016-03-11 17:13:06 -06:00
"github.com/grafana/grafana/pkg/components/simplejson"
"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 (
ErrDashboardNotFound = errors.New("Dashboard 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")
ErrDashboardContainsInvalidAlertData = errors.New("Invalid alert data. Cannot save dashboard")
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")
2014-10-06 14:31:54 -05:00
)
type UpdatePluginDashboardError struct {
PluginId string
}
func (d UpdatePluginDashboardError) Error() string {
return "Dashboard belong to plugin"
}
var (
DashTypeJson = "file"
DashTypeDB = "db"
DashTypeScript = "script"
DashTypeSnapshot = "snapshot"
)
// Dashboard model
2014-08-08 05:35:15 -05:00
type Dashboard struct {
Id int64
Uid string
Slug string
OrgId int64
GnetId int64
Version int
PluginId string
2014-11-20 05:11:07 -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
IsFolder bool
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
}
// 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)
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
}
// NewDashboardFolder creates a new dashboard folder
func NewDashboardFolder(title string) *Dashboard {
folder := NewDashboard(title)
folder.Data.Set("schemaVersion", 16)
folder.Data.Set("editable", true)
folder.Data.Set("hideControls", true)
return folder
}
// GetTags turns the tags in data json into go string array
func (dash *Dashboard) GetTags() []string {
2016-03-11 17:13:06 -06:00
return dash.Data.Get("tags").MustStringArray()
}
2016-03-11 17:13:06 -06:00
func NewDashboardFromJson(data *simplejson.Json) *Dashboard {
2014-12-22 05:25:08 -06:00
dash := &Dashboard{}
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()
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)
2016-03-11 17:13:06 -06:00
if version, err := dash.Data.Get("version").Float64(); err == nil {
dash.Version = int(version)
2015-11-20 06:52:50 -06:00
dash.Updated = time.Now()
}
} 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
}
if gnetId, err := dash.Data.Get("gnetId").Float64(); err == nil {
dash.GnetId = int64(gnetId)
}
if uid, err := dash.Data.Get("uid").String(); err == nil {
dash.Uid = uid
}
2014-12-22 05:25:08 -06:00
return dash
2014-08-08 05:35:15 -05:00
}
// GetDashboardModel turns the command into the savable model
func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
dash := NewDashboardFromJson(cmd.Dashboard)
userId := cmd.UserId
if userId == 0 {
userId = -1
}
2016-03-11 17:13:06 -06:00
if dash.Data.Get("version").MustInt(0) == 0 {
dash.CreatedBy = userId
2016-01-28 00:00:24 -06:00
}
2016-03-11 17:13:06 -06:00
dash.UpdatedBy = userId
2016-01-28 00:00:24 -06:00
dash.OrgId = cmd.OrgId
dash.PluginId = cmd.PluginId
dash.IsFolder = cmd.IsFolder
2017-06-23 15:00:26 -05:00
dash.FolderId = cmd.FolderId
dash.UpdateSlug()
return dash
}
// GetString a
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
}
// UpdateSlug updates the slug
func (dash *Dashboard) UpdateSlug() {
title := dash.Data.Get("title").MustString()
dash.Slug = SlugifyTitle(title)
}
func SlugifyTitle(title string) string {
return slug.Make(strings.ToLower(title))
}
// 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)
}
// Return the html url for a dashboard
func (dash *Dashboard) GenerateUrl() string {
return GetDashboardUrl(dash.Uid, dash.Slug)
}
// 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-02-01 06:32:00 -06:00
// Return the html url for a dashboard
func GetDashboardUrl(uid string, slug string) string {
return fmt.Sprintf("%s/d/%s/%s", setting.AppSubUrl, uid, slug)
}
2018-02-01 06:32:00 -06:00
// Return the full url for a dashboard
func GetFullDashboardUrl(uid string, slug string) string {
return fmt.Sprintf("%s%s", setting.AppUrl, GetDashboardUrl(uid, slug))
}
// GetFolderUrl return the html url for a folder
func GetFolderUrl(folderUid string, slug string) string {
return fmt.Sprintf("%s/dashboards/f/%s/%s", setting.AppSubUrl, folderUid, slug)
}
//
// COMMANDS
//
type SaveDashboardCommand struct {
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"`
UpdatedAt time.Time
Result *Dashboard
}
type DeleteDashboardCommand struct {
2017-06-17 17:24:38 -05:00
Id int64
OrgId int64
}
//
// QUERIES
//
type GetDashboardQuery struct {
Slug string // required if no Id or Uid is specified
Id int64 // optional if slug is set
Uid string // optional if slug is set
OrgId int64
Result *Dashboard
}
type DashboardTagCloudItem struct {
Term string `json:"term"`
Count int `json:"count"`
}
type GetDashboardTagsQuery struct {
OrgId int64
Result []*DashboardTagCloudItem
}
type GetDashboardsQuery struct {
DashboardIds []int64
Result []*Dashboard
}
type GetDashboardPermissionsForUserQuery struct {
DashboardIds []int64
OrgId int64
UserId int64
OrgRole RoleType
Result []*DashboardPermissionForUser
}
type GetDashboardsByPluginIdQuery struct {
OrgId int64
PluginId string
Result []*Dashboard
}
2016-03-17 03:01:58 -05:00
type GetDashboardSlugByIdQuery struct {
Id int64
Result string
}
type GetDashboardsBySlugQuery struct {
OrgId int64
Slug string
Result []*Dashboard
}
type GetFoldersForSignedInUserQuery struct {
OrgId int64
SignedInUser *SignedInUser
Title string
Result []*DashboardFolder
}
type DashboardFolder struct {
Id int64 `json:"id"`
Title string `json:"title"`
}
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
}
type GetDashboardRefByIdQuery struct {
2018-02-01 06:32:00 -06:00
Id int64
Result *DashboardRef
}