2014-08-08 12:35:15 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
2014-11-20 12:11:07 +01:00
|
|
|
"errors"
|
2014-08-22 15:32:42 +02:00
|
|
|
"strings"
|
2014-08-21 22:09:48 +02:00
|
|
|
"time"
|
2015-04-15 12:36:02 +02:00
|
|
|
|
2015-06-12 08:53:20 +02:00
|
|
|
"github.com/gosimple/slug"
|
2016-03-12 00:13:06 +01:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2014-08-08 12:35:15 +02:00
|
|
|
)
|
|
|
|
|
|
2014-11-20 12:11:07 +01:00
|
|
|
// Typed errors
|
|
|
|
|
var (
|
2015-05-04 07:46:46 +02:00
|
|
|
ErrDashboardNotFound = errors.New("Dashboard not found")
|
2015-07-07 12:51:38 -07:00
|
|
|
ErrDashboardSnapshotNotFound = errors.New("Dashboard snapshot not found")
|
2015-01-05 17:04:29 +01:00
|
|
|
ErrDashboardWithSameNameExists = errors.New("A dashboard with the same name already exists")
|
2015-03-02 22:24:01 +01:00
|
|
|
ErrDashboardVersionMismatch = errors.New("The dashboard has been changed by someone else")
|
2014-10-06 15:31:54 -04:00
|
|
|
)
|
|
|
|
|
|
2015-05-12 14:11:30 +02:00
|
|
|
var (
|
2015-05-13 09:58:45 +02:00
|
|
|
DashTypeJson = "file"
|
|
|
|
|
DashTypeDB = "db"
|
|
|
|
|
DashTypeScript = "script"
|
|
|
|
|
DashTypeSnapshot = "snapshot"
|
2015-05-12 14:11:30 +02:00
|
|
|
)
|
|
|
|
|
|
2015-03-19 10:32:47 -04:00
|
|
|
// Dashboard model
|
2014-08-08 12:35:15 +02:00
|
|
|
type Dashboard struct {
|
2015-02-23 20:07:49 +01:00
|
|
|
Id int64
|
|
|
|
|
Slug string
|
|
|
|
|
OrgId int64
|
|
|
|
|
Version int
|
2014-11-20 12:11:07 +01:00
|
|
|
|
2015-01-07 12:37:24 +01:00
|
|
|
Created time.Time
|
|
|
|
|
Updated time.Time
|
2014-08-21 22:09:48 +02:00
|
|
|
|
2015-12-18 01:52:05 -08:00
|
|
|
UpdatedBy int64
|
2016-01-27 22:00:24 -08:00
|
|
|
CreatedBy int64
|
2015-12-18 00:20:23 -08:00
|
|
|
|
2014-08-21 22:09:48 +02:00
|
|
|
Title string
|
2016-03-12 00:13:06 +01:00
|
|
|
Data *simplejson.Json
|
2014-08-21 22:09:48 +02:00
|
|
|
}
|
|
|
|
|
|
2015-03-19 10:32:47 -04:00
|
|
|
// NewDashboard creates a new dashboard
|
2014-08-08 12:35:15 +02:00
|
|
|
func NewDashboard(title string) *Dashboard {
|
|
|
|
|
dash := &Dashboard{}
|
2016-03-12 00:13:06 +01:00
|
|
|
dash.Data = simplejson.New()
|
|
|
|
|
dash.Data.Set("title", title)
|
2014-08-22 15:32:42 +02:00
|
|
|
dash.Title = title
|
2015-11-20 04:37:24 -08:00
|
|
|
dash.Created = time.Now()
|
2015-11-20 04:52:50 -08:00
|
|
|
dash.Updated = time.Now()
|
|
|
|
|
dash.UpdateSlug()
|
2014-08-08 12:35:15 +02:00
|
|
|
return dash
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 10:32:47 -04:00
|
|
|
// GetTags turns the tags in data json into go string array
|
2015-01-07 12:37:24 +01:00
|
|
|
func (dash *Dashboard) GetTags() []string {
|
2016-03-12 00:13:06 +01:00
|
|
|
return dash.Data.Get("tags").MustStringArray()
|
2015-01-07 12:37:24 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-12 00:13:06 +01:00
|
|
|
func NewDashboardFromJson(data *simplejson.Json) *Dashboard {
|
2014-12-22 12:25:08 +01:00
|
|
|
dash := &Dashboard{}
|
2015-05-12 12:20:03 +02:00
|
|
|
dash.Data = data
|
2016-03-12 00:13:06 +01:00
|
|
|
dash.Title = dash.Data.Get("title").MustString()
|
2015-11-20 04:52:50 -08:00
|
|
|
dash.UpdateSlug()
|
2014-08-08 12:35:15 +02:00
|
|
|
|
2016-03-12 00:13:06 +01:00
|
|
|
if id, err := dash.Data.Get("id").Float64(); err == nil {
|
|
|
|
|
dash.Id = int64(id)
|
2015-03-02 22:24:01 +01:00
|
|
|
|
2016-03-12 00:13:06 +01:00
|
|
|
if version, err := dash.Data.Get("version").Float64(); err == nil {
|
|
|
|
|
dash.Version = int(version)
|
2015-11-20 04:52:50 -08:00
|
|
|
dash.Updated = time.Now()
|
|
|
|
|
}
|
2015-03-19 10:32:47 -04:00
|
|
|
} else {
|
2016-03-12 00:13:06 +01:00
|
|
|
dash.Data.Set("version", 0)
|
2015-11-20 04:52:50 -08:00
|
|
|
dash.Created = time.Now()
|
|
|
|
|
dash.Updated = time.Now()
|
2014-08-08 12:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
2014-12-22 12:25:08 +01:00
|
|
|
return dash
|
2014-08-08 12:35:15 +02:00
|
|
|
}
|
|
|
|
|
|
2015-05-12 12:20:03 +02:00
|
|
|
// GetDashboardModel turns the command into the savable model
|
|
|
|
|
func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
|
|
|
|
|
dash := NewDashboardFromJson(cmd.Dashboard)
|
2016-03-12 00:13:06 +01:00
|
|
|
|
|
|
|
|
if dash.Data.Get("version").MustInt(0) == 0 {
|
2016-01-27 22:00:24 -08:00
|
|
|
dash.CreatedBy = cmd.UserId
|
|
|
|
|
}
|
2016-03-12 00:13:06 +01:00
|
|
|
|
2016-01-27 22:00:24 -08:00
|
|
|
dash.UpdatedBy = cmd.UserId
|
|
|
|
|
dash.OrgId = cmd.OrgId
|
2015-05-12 12:20:03 +02:00
|
|
|
dash.UpdateSlug()
|
|
|
|
|
return dash
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 10:32:47 -04:00
|
|
|
// GetString a
|
2016-03-10 19:57:48 +01:00
|
|
|
func (dash *Dashboard) GetString(prop string, defaultValue string) string {
|
2016-03-12 00:13:06 +01:00
|
|
|
return dash.Data.Get(prop).MustString(defaultValue)
|
2014-08-08 12:35:15 +02:00
|
|
|
}
|
2014-08-22 15:32:42 +02:00
|
|
|
|
2015-03-19 10:32:47 -04:00
|
|
|
// UpdateSlug updates the slug
|
2014-08-22 15:32:42 +02:00
|
|
|
func (dash *Dashboard) UpdateSlug() {
|
2016-03-12 00:13:06 +01:00
|
|
|
title := strings.ToLower(dash.Data.Get("title").MustString())
|
2015-04-15 12:36:02 +02:00
|
|
|
dash.Slug = slug.Make(title)
|
2014-08-22 15:32:42 +02:00
|
|
|
}
|
2015-01-09 11:01:37 +01:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// COMMANDS
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
type SaveDashboardCommand struct {
|
2016-03-12 00:13:06 +01:00
|
|
|
Dashboard *simplejson.Json `json:"dashboard" binding:"Required"`
|
|
|
|
|
UserId int64 `json:"userId"`
|
|
|
|
|
OrgId int64 `json:"-"`
|
|
|
|
|
Overwrite bool `json:"overwrite"`
|
2015-01-09 11:01:37 +01:00
|
|
|
|
|
|
|
|
Result *Dashboard
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DeleteDashboardCommand struct {
|
2015-02-23 20:07:49 +01:00
|
|
|
Slug string
|
|
|
|
|
OrgId int64
|
2015-01-09 11:01:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// QUERIES
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
type GetDashboardQuery struct {
|
2015-02-23 20:07:49 +01:00
|
|
|
Slug string
|
|
|
|
|
OrgId int64
|
2015-01-09 11:01:37 +01:00
|
|
|
|
|
|
|
|
Result *Dashboard
|
|
|
|
|
}
|
2015-05-13 13:36:13 +02:00
|
|
|
|
|
|
|
|
type DashboardTagCloudItem struct {
|
|
|
|
|
Term string `json:"term"`
|
|
|
|
|
Count int `json:"count"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GetDashboardTagsQuery struct {
|
|
|
|
|
OrgId int64
|
|
|
|
|
Result []*DashboardTagCloudItem
|
|
|
|
|
}
|
2016-01-29 01:41:23 +01:00
|
|
|
|
|
|
|
|
type GetDashboardsQuery struct {
|
|
|
|
|
DashboardIds []int64
|
|
|
|
|
Result *[]Dashboard
|
|
|
|
|
}
|
2016-03-17 01:01:58 -07:00
|
|
|
|
|
|
|
|
type GetDashboardSlugByIdQuery struct {
|
|
|
|
|
Id int64
|
|
|
|
|
Result string
|
|
|
|
|
}
|