2016-09-22 23:22:41 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2016-11-04 00:44:28 +00:00
|
|
|
"encoding/json"
|
2016-09-22 23:22:41 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
2016-10-20 14:38:23 +00:00
|
|
|
"github.com/influxdata/chronograf"
|
2016-09-22 23:22:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:generate protoc --gogo_out=. internal.proto
|
|
|
|
|
|
|
|
// MarshalExploration encodes an exploration to binary protobuf format.
|
2016-10-20 14:38:23 +00:00
|
|
|
func MarshalExploration(e *chronograf.Exploration) ([]byte, error) {
|
2016-09-22 23:22:41 +00:00
|
|
|
return proto.Marshal(&Exploration{
|
|
|
|
ID: int64(e.ID),
|
|
|
|
Name: e.Name,
|
|
|
|
UserID: int64(e.UserID),
|
|
|
|
Data: e.Data,
|
|
|
|
CreatedAt: e.CreatedAt.UnixNano(),
|
|
|
|
UpdatedAt: e.UpdatedAt.UnixNano(),
|
2016-09-28 19:32:58 +00:00
|
|
|
Default: e.Default,
|
2016-09-22 23:22:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalExploration decodes an exploration from binary protobuf data.
|
2016-10-20 14:38:23 +00:00
|
|
|
func UnmarshalExploration(data []byte, e *chronograf.Exploration) error {
|
2016-09-22 23:22:41 +00:00
|
|
|
var pb Exploration
|
|
|
|
if err := proto.Unmarshal(data, &pb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-20 14:38:23 +00:00
|
|
|
e.ID = chronograf.ExplorationID(pb.ID)
|
2016-09-22 23:22:41 +00:00
|
|
|
e.Name = pb.Name
|
2016-10-20 14:38:23 +00:00
|
|
|
e.UserID = chronograf.UserID(pb.UserID)
|
2016-09-22 23:22:41 +00:00
|
|
|
e.Data = pb.Data
|
|
|
|
e.CreatedAt = time.Unix(0, pb.CreatedAt).UTC()
|
|
|
|
e.UpdatedAt = time.Unix(0, pb.UpdatedAt).UTC()
|
2016-09-28 19:32:58 +00:00
|
|
|
e.Default = pb.Default
|
2016-09-22 23:22:41 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-30 20:39:27 +00:00
|
|
|
|
2016-10-06 04:26:39 +00:00
|
|
|
// MarshalSource encodes a source to binary protobuf format.
|
2016-10-20 14:38:23 +00:00
|
|
|
func MarshalSource(s chronograf.Source) ([]byte, error) {
|
2016-09-30 20:39:27 +00:00
|
|
|
return proto.Marshal(&Source{
|
|
|
|
ID: int64(s.ID),
|
|
|
|
Name: s.Name,
|
|
|
|
Type: s.Type,
|
|
|
|
Username: s.Username,
|
|
|
|
Password: s.Password,
|
2016-10-25 15:20:06 +00:00
|
|
|
URL: s.URL,
|
2016-09-30 20:39:27 +00:00
|
|
|
Default: s.Default,
|
2016-11-18 19:13:32 +00:00
|
|
|
Telegraf: s.Telegraf,
|
2016-09-30 20:39:27 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-06 04:26:39 +00:00
|
|
|
// UnmarshalSource decodes a source from binary protobuf data.
|
2016-10-20 14:38:23 +00:00
|
|
|
func UnmarshalSource(data []byte, s *chronograf.Source) error {
|
2016-09-30 20:39:27 +00:00
|
|
|
var pb Source
|
|
|
|
if err := proto.Unmarshal(data, &pb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.ID = int(pb.ID)
|
|
|
|
s.Name = pb.Name
|
|
|
|
s.Type = pb.Type
|
|
|
|
s.Username = pb.Username
|
|
|
|
s.Password = pb.Password
|
2016-10-25 15:20:06 +00:00
|
|
|
s.URL = pb.URL
|
2016-09-30 20:39:27 +00:00
|
|
|
s.Default = pb.Default
|
2016-11-18 19:13:32 +00:00
|
|
|
s.Telegraf = pb.Telegraf
|
2016-09-30 20:39:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-09-30 23:46:28 +00:00
|
|
|
|
2016-10-06 04:26:39 +00:00
|
|
|
// MarshalServer encodes a server to binary protobuf format.
|
2016-10-20 14:38:23 +00:00
|
|
|
func MarshalServer(s chronograf.Server) ([]byte, error) {
|
2016-09-30 23:46:28 +00:00
|
|
|
return proto.Marshal(&Server{
|
|
|
|
ID: int64(s.ID),
|
2016-10-03 17:41:50 +00:00
|
|
|
SrcID: int64(s.SrcID),
|
2016-09-30 23:46:28 +00:00
|
|
|
Name: s.Name,
|
|
|
|
Username: s.Username,
|
|
|
|
Password: s.Password,
|
|
|
|
URL: s.URL,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-06 04:26:39 +00:00
|
|
|
// UnmarshalServer decodes a server from binary protobuf data.
|
2016-10-20 14:38:23 +00:00
|
|
|
func UnmarshalServer(data []byte, s *chronograf.Server) error {
|
2016-09-30 23:46:28 +00:00
|
|
|
var pb Server
|
|
|
|
if err := proto.Unmarshal(data, &pb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.ID = int(pb.ID)
|
2016-10-03 17:41:50 +00:00
|
|
|
s.SrcID = int(pb.SrcID)
|
2016-09-30 23:46:28 +00:00
|
|
|
s.Name = pb.Name
|
|
|
|
s.Username = pb.Username
|
|
|
|
s.Password = pb.Password
|
|
|
|
s.URL = pb.URL
|
|
|
|
return nil
|
|
|
|
}
|
2016-10-06 04:26:39 +00:00
|
|
|
|
|
|
|
// MarshalLayout encodes a layout to binary protobuf format.
|
2016-10-20 14:38:23 +00:00
|
|
|
func MarshalLayout(l chronograf.Layout) ([]byte, error) {
|
2016-10-06 04:26:39 +00:00
|
|
|
cells := make([]*Cell, len(l.Cells))
|
|
|
|
for i, c := range l.Cells {
|
|
|
|
queries := make([]*Query, len(c.Queries))
|
|
|
|
for j, q := range c.Queries {
|
2016-11-29 21:04:54 +00:00
|
|
|
r := new(Range)
|
|
|
|
if q.Range != nil {
|
|
|
|
r.Upper, r.Lower = q.Range.Upper, q.Range.Lower
|
|
|
|
}
|
2016-10-06 04:26:39 +00:00
|
|
|
queries[j] = &Query{
|
2016-11-07 22:29:39 +00:00
|
|
|
Command: q.Command,
|
|
|
|
DB: q.DB,
|
|
|
|
RP: q.RP,
|
|
|
|
GroupBys: q.GroupBys,
|
2016-11-08 02:35:46 +00:00
|
|
|
Wheres: q.Wheres,
|
2016-11-29 21:04:54 +00:00
|
|
|
Label: q.Label,
|
|
|
|
Range: r,
|
2016-10-06 04:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-22 17:27:27 +00:00
|
|
|
|
2016-10-06 04:26:39 +00:00
|
|
|
cells[i] = &Cell{
|
|
|
|
X: c.X,
|
|
|
|
Y: c.Y,
|
|
|
|
W: c.W,
|
|
|
|
H: c.H,
|
2016-10-28 20:43:06 +00:00
|
|
|
I: c.I,
|
|
|
|
Name: c.Name,
|
2016-10-06 04:26:39 +00:00
|
|
|
Queries: queries,
|
2016-12-06 05:46:30 +00:00
|
|
|
Type: c.Type,
|
2016-10-06 04:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return proto.Marshal(&Layout{
|
2016-10-10 22:00:27 +00:00
|
|
|
ID: l.ID,
|
2016-10-06 23:31:56 +00:00
|
|
|
Measurement: l.Measurement,
|
|
|
|
Application: l.Application,
|
2016-11-15 18:11:32 +00:00
|
|
|
Autoflow: l.Autoflow,
|
2016-10-06 23:31:56 +00:00
|
|
|
Cells: cells,
|
2016-10-06 04:26:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalLayout decodes a layout from binary protobuf data.
|
2016-10-20 14:38:23 +00:00
|
|
|
func UnmarshalLayout(data []byte, l *chronograf.Layout) error {
|
2016-10-06 04:26:39 +00:00
|
|
|
var pb Layout
|
|
|
|
if err := proto.Unmarshal(data, &pb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-10 22:00:27 +00:00
|
|
|
l.ID = pb.ID
|
2016-10-06 23:31:56 +00:00
|
|
|
l.Measurement = pb.Measurement
|
|
|
|
l.Application = pb.Application
|
2016-11-15 18:11:32 +00:00
|
|
|
l.Autoflow = pb.Autoflow
|
2016-10-20 14:38:23 +00:00
|
|
|
cells := make([]chronograf.Cell, len(pb.Cells))
|
2016-10-06 04:26:39 +00:00
|
|
|
for i, c := range pb.Cells {
|
2016-10-20 14:38:23 +00:00
|
|
|
queries := make([]chronograf.Query, len(c.Queries))
|
2016-10-06 04:26:39 +00:00
|
|
|
for j, q := range c.Queries {
|
2016-10-20 14:38:23 +00:00
|
|
|
queries[j] = chronograf.Query{
|
2016-11-07 22:29:39 +00:00
|
|
|
Command: q.Command,
|
|
|
|
DB: q.DB,
|
|
|
|
RP: q.RP,
|
|
|
|
GroupBys: q.GroupBys,
|
2016-11-08 02:35:46 +00:00
|
|
|
Wheres: q.Wheres,
|
2016-11-29 21:04:54 +00:00
|
|
|
Label: q.Label,
|
|
|
|
}
|
|
|
|
if q.Range.Upper != q.Range.Lower {
|
|
|
|
queries[j].Range = &chronograf.Range{
|
|
|
|
Upper: q.Range.Upper,
|
|
|
|
Lower: q.Range.Lower,
|
|
|
|
}
|
2016-10-06 04:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-22 17:27:27 +00:00
|
|
|
|
2016-10-20 14:38:23 +00:00
|
|
|
cells[i] = chronograf.Cell{
|
2016-10-06 04:26:39 +00:00
|
|
|
X: c.X,
|
|
|
|
Y: c.Y,
|
|
|
|
W: c.W,
|
|
|
|
H: c.H,
|
2016-10-28 20:43:06 +00:00
|
|
|
I: c.I,
|
|
|
|
Name: c.Name,
|
2016-10-06 04:26:39 +00:00
|
|
|
Queries: queries,
|
2016-12-06 05:46:30 +00:00
|
|
|
Type: c.Type,
|
2016-10-06 04:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
l.Cells = cells
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-04 00:44:28 +00:00
|
|
|
|
2016-11-04 01:56:42 +00:00
|
|
|
// ScopedAlert contains the source and the kapacitor id
|
|
|
|
type ScopedAlert struct {
|
|
|
|
chronograf.AlertRule
|
|
|
|
SrcID int
|
|
|
|
KapaID int
|
|
|
|
}
|
|
|
|
|
2016-11-04 00:44:28 +00:00
|
|
|
// MarshalAlertRule encodes an alert rule to binary protobuf format.
|
2016-11-04 01:56:42 +00:00
|
|
|
func MarshalAlertRule(r *ScopedAlert) ([]byte, error) {
|
|
|
|
j, err := json.Marshal(r.AlertRule)
|
2016-11-04 00:44:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return proto.Marshal(&AlertRule{
|
2016-11-04 01:56:42 +00:00
|
|
|
ID: r.ID,
|
|
|
|
SrcID: int64(r.SrcID),
|
|
|
|
KapaID: int64(r.KapaID),
|
|
|
|
JSON: string(j),
|
2016-11-04 00:44:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalAlertRule decodes an alert rule from binary protobuf data.
|
2016-11-04 01:56:42 +00:00
|
|
|
func UnmarshalAlertRule(data []byte, r *ScopedAlert) error {
|
2016-11-04 00:44:28 +00:00
|
|
|
var pb AlertRule
|
|
|
|
if err := proto.Unmarshal(data, &pb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-04 01:56:42 +00:00
|
|
|
err := json.Unmarshal([]byte(pb.JSON), &r.AlertRule)
|
2016-11-04 00:44:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-04 01:56:42 +00:00
|
|
|
r.SrcID = int(pb.SrcID)
|
|
|
|
r.KapaID = int(pb.KapaID)
|
2016-11-04 00:44:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-11-17 23:57:46 +00:00
|
|
|
|
|
|
|
// MarshalUser encodes a user to binary protobuf format.
|
|
|
|
func MarshalUser(u *chronograf.User) ([]byte, error) {
|
|
|
|
return proto.Marshal(&User{
|
|
|
|
ID: uint64(u.ID),
|
|
|
|
Email: u.Email,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalUser decodes a user from binary protobuf data.
|
|
|
|
func UnmarshalUser(data []byte, u *chronograf.User) error {
|
|
|
|
var pb User
|
|
|
|
if err := proto.Unmarshal(data, &pb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.ID = chronograf.UserID(pb.ID)
|
|
|
|
u.Email = pb.Email
|
|
|
|
return nil
|
|
|
|
}
|