forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboards.go
115 lines (93 loc) · 2.21 KB
/
dashboards.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package models
import (
"errors"
"strings"
"time"
"github.com/gosimple/slug"
)
// Typed errors
var (
ErrDashboardNotFound = errors.New("Account not found")
ErrDashboardWithSameNameExists = errors.New("A dashboard with the same name already exists")
ErrDashboardVersionMismatch = errors.New("The dashboard has been changed by someone else")
)
// Dashboard model
type Dashboard struct {
Id int64
Slug string
OrgId int64
Version int
Created time.Time
Updated time.Time
Title string
Data map[string]interface{}
}
// NewDashboard creates a new dashboard
func NewDashboard(title string) *Dashboard {
dash := &Dashboard{}
dash.Data = make(map[string]interface{})
dash.Data["title"] = title
dash.Title = title
dash.UpdateSlug()
return dash
}
// GetTags turns the tags in data json into go string array
func (dash *Dashboard) GetTags() []string {
jsonTags := dash.Data["tags"]
if jsonTags == nil {
return []string{}
}
arr := jsonTags.([]interface{})
b := make([]string, len(arr))
for i := range arr {
b[i] = arr[i].(string)
}
return b
}
// GetDashboardModel turns the command into the savable model
func (cmd *SaveDashboardCommand) GetDashboardModel() *Dashboard {
dash := &Dashboard{}
dash.Data = cmd.Dashboard
dash.Title = dash.Data["title"].(string)
dash.OrgId = cmd.OrgId
dash.UpdateSlug()
if dash.Data["id"] != nil {
dash.Id = int64(dash.Data["id"].(float64))
if dash.Data["version"] != nil {
dash.Version = int(dash.Data["version"].(float64))
}
} else {
dash.Data["version"] = 0
}
return dash
}
// GetString a
func (dash *Dashboard) GetString(prop string) string {
return dash.Data[prop].(string)
}
// UpdateSlug updates the slug
func (dash *Dashboard) UpdateSlug() {
title := strings.ToLower(dash.Data["title"].(string))
dash.Slug = slug.Make(title)
}
//
// COMMANDS
//
type SaveDashboardCommand struct {
Dashboard map[string]interface{} `json:"dashboard" binding:"Required"`
Overwrite bool `json:"overwrite"`
OrgId int64 `json:"-"`
Result *Dashboard
}
type DeleteDashboardCommand struct {
Slug string
OrgId int64
}
//
// QUERIES
//
type GetDashboardQuery struct {
Slug string
OrgId int64
Result *Dashboard
}