influxdb/bolt/internal/internal.go

160 lines
3.6 KiB
Go
Raw Normal View History

package internal
import (
"time"
"github.com/gogo/protobuf/proto"
2016-10-20 14:38:23 +00:00
"github.com/influxdata/chronograf"
)
//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) {
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,
})
}
// UnmarshalExploration decodes an exploration from binary protobuf data.
2016-10-20 14:38:23 +00:00
func UnmarshalExploration(data []byte, e *chronograf.Exploration) error {
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)
e.Name = pb.Name
2016-10-20 14:38:23 +00:00
e.UserID = chronograf.UserID(pb.UserID)
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
return nil
}
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) {
return proto.Marshal(&Source{
ID: int64(s.ID),
Name: s.Name,
Type: s.Type,
Username: s.Username,
Password: s.Password,
URLs: s.URL,
Default: s.Default,
})
}
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 {
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
s.URL = pb.URLs
s.Default = pb.Default
return nil
}
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) {
return proto.Marshal(&Server{
ID: int64(s.ID),
SrcID: int64(s.SrcID),
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 {
var pb Server
if err := proto.Unmarshal(data, &pb); err != nil {
return err
}
s.ID = int(pb.ID)
s.SrcID = int(pb.SrcID)
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 {
queries[j] = &Query{
Command: q.Command,
DB: q.DB,
RP: q.RP,
}
}
cells[i] = &Cell{
X: c.X,
Y: c.Y,
W: c.W,
H: c.H,
Queries: queries,
}
}
return proto.Marshal(&Layout{
ID: l.ID,
Measurement: l.Measurement,
Application: l.Application,
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
}
l.ID = pb.ID
l.Measurement = pb.Measurement
l.Application = pb.Application
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-10-06 04:26:39 +00:00
Command: q.Command,
DB: q.DB,
RP: q.RP,
}
}
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,
Queries: queries,
}
}
l.Cells = cells
return nil
}