2014-08-08 05:35:15 -05:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2014-11-20 05:11:07 -06:00
|
|
|
"errors"
|
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"
|
2014-08-08 05:35:15 -05:00
|
|
|
)
|
|
|
|
|
2014-11-20 05:11:07 -06:00
|
|
|
// Typed errors
|
|
|
|
var (
|
2015-05-04 00:46:46 -05:00
|
|
|
ErrDashboardNotFound = errors.New("Dashboard not found")
|
2015-07-07 14:51:38 -05:00
|
|
|
ErrDashboardSnapshotNotFound = errors.New("Dashboard snapshot not found")
|
2015-01-05 10:04:29 -06:00
|
|
|
ErrDashboardWithSameNameExists = errors.New("A dashboard with the same name already exists")
|
2015-03-02 15:24:01 -06:00
|
|
|
ErrDashboardVersionMismatch = errors.New("The dashboard has been changed by someone else")
|
2014-10-06 14:31:54 -05:00
|
|
|
)
|
|
|
|
|
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 {
|
2015-02-23 13:07:49 -06:00
|
|
|
Id int64
|
|
|
|
Slug string
|
|
|
|
OrgId int64
|
|
|
|
Version int
|
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
|
2015-12-18 02:20:23 -06:00
|
|
|
|
2014-08-21 15:09:48 -05:00
|
|
|
Title string
|
|
|
|
Data map[string]interface{}
|
|
|
|
}
|
|
|
|
|
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{}
|
|
|
|
dash.Data = make(map[string]interface{})
|
|
|
|
dash.Data["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
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
jsonTags := dash.Data["tags"]
|
2015-06-12 02:04:10 -05:00
|
|
|
if jsonTags == nil || jsonTags == "" {
|
2015-01-07 05:37:24 -06:00
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
arr := jsonTags.([]interface{})
|
|
|
|
b := make([]string, len(arr))
|
|
|
|
for i := range arr {
|
|
|
|
b[i] = arr[i].(string)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-01-28 00:00:24 -06:00
|
|
|
func NewDashboardFromJson(data map[string]interface{}) *Dashboard {
|
2014-12-22 05:25:08 -06:00
|
|
|
dash := &Dashboard{}
|
2015-05-12 05:20:03 -05:00
|
|
|
dash.Data = data
|
2014-12-22 05:25:08 -06:00
|
|
|
dash.Title = dash.Data["title"].(string)
|
2015-11-20 06:52:50 -06:00
|
|
|
dash.UpdateSlug()
|
2014-08-08 05:35:15 -05:00
|
|
|
|
2014-12-22 05:25:08 -06:00
|
|
|
if dash.Data["id"] != nil {
|
|
|
|
dash.Id = int64(dash.Data["id"].(float64))
|
2015-03-02 15:24:01 -06:00
|
|
|
|
|
|
|
if dash.Data["version"] != nil {
|
|
|
|
dash.Version = int(dash.Data["version"].(float64))
|
2015-11-20 06:52:50 -06:00
|
|
|
dash.Updated = time.Now()
|
|
|
|
}
|
2015-03-19 09:32:47 -05:00
|
|
|
} else {
|
|
|
|
dash.Data["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
|
|
|
}
|
|
|
|
|
2014-12-22 05:25:08 -06:00
|
|
|
return dash
|
2014-08-08 05:35:15 -05:00
|
|
|
}
|
|
|
|
|
2015-05-12 05:20:03 -05:00
|
|
|
// GetDashboardModel turns the command into the savable model
|
|
|
|
func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
|
|
|
|
dash := NewDashboardFromJson(cmd.Dashboard)
|
2016-01-27 18:33:32 -06:00
|
|
|
if dash.Data["version"] == 0 {
|
2016-01-28 00:00:24 -06:00
|
|
|
dash.CreatedBy = cmd.UserId
|
|
|
|
}
|
|
|
|
dash.UpdatedBy = cmd.UserId
|
|
|
|
dash.OrgId = cmd.OrgId
|
2015-05-12 05:20:03 -05:00
|
|
|
dash.UpdateSlug()
|
|
|
|
return dash
|
|
|
|
}
|
|
|
|
|
2015-03-19 09:32:47 -05:00
|
|
|
// GetString a
|
2014-08-08 05:35:15 -05:00
|
|
|
func (dash *Dashboard) GetString(prop string) string {
|
|
|
|
return dash.Data[prop].(string)
|
|
|
|
}
|
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() {
|
|
|
|
title := strings.ToLower(dash.Data["title"].(string))
|
2015-04-15 05:36:02 -05:00
|
|
|
dash.Slug = slug.Make(title)
|
2014-08-22 08:32:42 -05:00
|
|
|
}
|
2015-01-09 04:01:37 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
// COMMANDS
|
|
|
|
//
|
|
|
|
|
|
|
|
type SaveDashboardCommand struct {
|
2015-03-02 15:24:01 -06:00
|
|
|
Dashboard map[string]interface{} `json:"dashboard" binding:"Required"`
|
2016-01-28 00:00:24 -06:00
|
|
|
UserId int64 `json:"userId"`
|
2015-02-23 13:07:49 -06:00
|
|
|
OrgId int64 `json:"-"`
|
2016-01-28 00:00:24 -06:00
|
|
|
Overwrite bool `json:"overwrite"`
|
2015-01-09 04:01:37 -06:00
|
|
|
|
|
|
|
Result *Dashboard
|
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteDashboardCommand struct {
|
2015-02-23 13:07:49 -06:00
|
|
|
Slug string
|
|
|
|
OrgId int64
|
2015-01-09 04:01:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// QUERIES
|
|
|
|
//
|
|
|
|
|
|
|
|
type GetDashboardQuery struct {
|
2015-02-23 13:07:49 -06:00
|
|
|
Slug string
|
|
|
|
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
|
|
|
|
Result *[]Dashboard
|
|
|
|
}
|