/
dashboard.go
154 lines (139 loc) · 3.25 KB
/
dashboard.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*************************************************************************
* Copyright 2021 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package types
import (
"encoding/json"
"time"
)
// Dashboard type used for relaying data back and forth to frontend.
type Dashboard struct {
ID uint64
Name string
UID int32
GIDs []int32
Global bool
Description string
Created time.Time
Updated time.Time
Data RawObject
Labels []string
GUID string `json:",omitempty"`
Trivial bool `json:",omitempty"`
Synced bool
}
// DashboardAdd is used to push new dashboards.
type DashboardAdd struct {
Name string
Description string
Data RawObject
Labels []string
UID int32
GIDs []int32
Global bool
}
// DashboardPost is used in sending a new dashboard to the marketplace.
type DashboardPost struct {
Name string
Desc string
JSON []byte
User string
Tags []string
}
// DashboardGet is used to get a dashboard from the marketplace.
type DashboardGet struct {
Name string
Desc string
JSON []byte
User string
Score int `json:",omitempty"`
Version int `json:",omitempty"`
GUID string
Created time.Time
Updated time.Time
Customer string
Tags []string
}
// DashboardComment is used to send and retrieve comments.
type DashboardComment struct {
ID int
UID int
Name string
Comment string
}
func EncodeDashboardAdd(name, desc string, obj interface{}) (*DashboardAdd, error) {
msg, err := json.Marshal(obj)
if err != nil {
return nil, err
}
return &DashboardAdd{
Name: name,
Description: desc,
Data: RawObject(msg),
}, nil
}
type Dashboards []Dashboard
func (d Dashboard) Equal(v Dashboard) bool {
if d.ID != v.ID || d.Name != v.Name || d.UID != v.UID {
return false
}
if d.Description != v.Description || d.GUID != v.GUID {
return false
}
if len(d.GIDs) != len(v.GIDs) || len(d.Labels) != len(v.Labels) {
return false
}
for i, l := range d.Labels {
if l != v.Labels[i] {
return false
}
}
for i, g := range d.GIDs {
if g != v.GIDs[i] {
return false
}
}
return true
}
// JSON custom marshallers
type dbaddMarshaler struct {
Name string
Description string
Data RawObject
UID int32
GIDs []int32
Global bool
}
func (d DashboardAdd) MarshalJSON() ([]byte, error) {
var data RawObject
if d.Data == nil || len(d.Data) == 0 {
data = emptyRawObj
} else {
data = RawObject(d.Data)
}
dba := dbaddMarshaler{
Name: d.Name,
Description: d.Description,
Data: data,
UID: d.UID,
GIDs: d.GIDs,
Global: d.Global,
}
return json.Marshal(dba)
}
func (d *DashboardAdd) UnmarshalObject(obj interface{}) error {
return json.Unmarshal(d.Data, obj)
}
func (d *Dashboard) UnmarshalObject(obj interface{}) error {
return json.Unmarshal(d.Data, obj)
}
func (d Dashboards) MarshalJSON() ([]byte, error) {
if len(d) == 0 {
return emptyList, nil
}
return json.Marshal([]Dashboard(d))
}