1st
This commit is contained in:
270
client/client.go
270
client/client.go
@@ -66,13 +66,7 @@ func (s *Connection) mustSuccess(reader *bufreader.BufferedReader) (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
type Metric struct {
|
||||
MetricID uint32
|
||||
MetricType diploma.MetricType
|
||||
FracDigits byte
|
||||
}
|
||||
|
||||
func (s *Connection) AddMetric(req Metric) error {
|
||||
func (s *Connection) AddMetric(req proto.AddMetricReq) error {
|
||||
arr := []byte{
|
||||
proto.TypeAddMetric,
|
||||
0, 0, 0, 0, //
|
||||
@@ -87,7 +81,22 @@ func (s *Connection) AddMetric(req Metric) error {
|
||||
return s.mustSuccess(s.src)
|
||||
}
|
||||
|
||||
func (s *Connection) GetMetric(metricID uint32) (*Metric, error) {
|
||||
//
|
||||
// func (s *Connection) UpdateMetric(req Metric) error {
|
||||
// arr := []byte{
|
||||
// proto.TypeUpdateMetric,
|
||||
// 0, 0, 0, 0, //
|
||||
// byte(req.FracDigits),
|
||||
// }
|
||||
// bin.PutUint32(arr[1:], req.MetricID)
|
||||
|
||||
// if _, err := s.conn.Write(arr); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return s.mustSuccess(s.src)
|
||||
// }
|
||||
|
||||
func (s *Connection) GetMetric(metricID uint32) (*proto.Metric, error) {
|
||||
arr := []byte{
|
||||
proto.TypeGetMetric,
|
||||
0, 0, 0, 0,
|
||||
@@ -110,10 +119,10 @@ func (s *Connection) GetMetric(metricID uint32) (*Metric, error) {
|
||||
return nil, fmt.Errorf("read body: %s", err)
|
||||
}
|
||||
|
||||
return &Metric{
|
||||
return &proto.Metric{
|
||||
MetricID: bin.GetUint32(arr),
|
||||
MetricType: diploma.MetricType(arr[4]),
|
||||
FracDigits: arr[5],
|
||||
FracDigits: int(arr[5]),
|
||||
}, nil
|
||||
|
||||
case proto.RespError:
|
||||
@@ -137,13 +146,7 @@ func (s *Connection) DeleteMetric(metricID uint32) error {
|
||||
return s.mustSuccess(s.src)
|
||||
}
|
||||
|
||||
type AppendMeasureReq struct {
|
||||
MetricID uint32
|
||||
Timestamp uint32
|
||||
Value float64
|
||||
}
|
||||
|
||||
func (s *Connection) AppendMeasure(req AppendMeasureReq) (err error) {
|
||||
func (s *Connection) AppendMeasure(req proto.AppendMeasureReq) (err error) {
|
||||
arr := []byte{
|
||||
proto.TypeAppendMeasure,
|
||||
0, 0, 0, 0, // metricID
|
||||
@@ -160,17 +163,7 @@ func (s *Connection) AppendMeasure(req AppendMeasureReq) (err error) {
|
||||
return s.mustSuccess(s.src)
|
||||
}
|
||||
|
||||
type AppendMeasuresReq struct {
|
||||
MetricID uint32
|
||||
Measures []Measure
|
||||
}
|
||||
|
||||
type Measure struct {
|
||||
Timestamp uint32
|
||||
Value float64
|
||||
}
|
||||
|
||||
func (s *Connection) AppendMeasures(req AppendMeasuresReq) (err error) {
|
||||
func (s *Connection) AppendMeasures(req proto.AppendMeasuresReq) (err error) {
|
||||
if len(req.Measures) > 65535 {
|
||||
return fmt.Errorf("wrong measures qty: %d", len(req.Measures))
|
||||
}
|
||||
@@ -191,15 +184,74 @@ func (s *Connection) AppendMeasures(req AppendMeasuresReq) (err error) {
|
||||
if _, err := s.conn.Write(arr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//fmt.Printf("encode measures: %d\n", len(req.Measures))
|
||||
return s.mustSuccess(s.src)
|
||||
}
|
||||
|
||||
type InstantMeasure struct {
|
||||
Timestamp uint32
|
||||
Value float64
|
||||
// type AppendMeasurePerMetricReq struct {
|
||||
// MetricID uint32
|
||||
// Measures []Measure
|
||||
// }
|
||||
|
||||
func (s *Connection) AppendMeasurePerMetric(list []proto.MetricMeasure) (_ []proto.AppendError, err error) {
|
||||
if len(list) > 65535 {
|
||||
return nil, fmt.Errorf("wrong measures qty: %d", len(list))
|
||||
}
|
||||
var (
|
||||
// 3 bytes: 1b message type + 2b records qty
|
||||
fixedSize = 3
|
||||
recordSize = 16
|
||||
arr = make([]byte, fixedSize+len(list)*recordSize)
|
||||
)
|
||||
arr[0] = proto.TypeAppendMeasures
|
||||
bin.PutUint16(arr[1:], uint16(len(list)))
|
||||
pos := fixedSize
|
||||
for _, item := range list {
|
||||
bin.PutUint32(arr[pos:], item.MetricID)
|
||||
bin.PutUint32(arr[pos+4:], item.Timestamp)
|
||||
bin.PutFloat64(arr[pos+8:], item.Value)
|
||||
pos += recordSize
|
||||
}
|
||||
if _, err := s.conn.Write(arr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
code, err := s.src.ReadByte()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response code: %s", err)
|
||||
}
|
||||
|
||||
switch code {
|
||||
case proto.RespValue:
|
||||
var (
|
||||
qty uint16
|
||||
appendErrors []proto.AppendError
|
||||
)
|
||||
qty, err = bin.ReadUint16(s.src)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for range qty {
|
||||
var ae proto.AppendError
|
||||
ae.MetricID, err = bin.ReadUint32(s.src)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ae.ErrorCode, err = bin.ReadUint16(s.src)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
appendErrors = append(appendErrors, ae)
|
||||
}
|
||||
return appendErrors, nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown reponse code %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Connection) ListAllInstantMeasures(metricID uint32) ([]InstantMeasure, error) {
|
||||
func (s *Connection) ListAllInstantMeasures(metricID uint32) ([]proto.InstantMeasure, error) {
|
||||
arr := []byte{
|
||||
proto.TypeListAllInstantMeasures,
|
||||
0, 0, 0, 0, // metricID
|
||||
@@ -211,7 +263,7 @@ func (s *Connection) ListAllInstantMeasures(metricID uint32) ([]InstantMeasure,
|
||||
}
|
||||
|
||||
var (
|
||||
result []InstantMeasure
|
||||
result []proto.InstantMeasure
|
||||
tmp = make([]byte, 12)
|
||||
)
|
||||
|
||||
@@ -221,6 +273,8 @@ func (s *Connection) ListAllInstantMeasures(metricID uint32) ([]InstantMeasure,
|
||||
return nil, fmt.Errorf("read response code: %s", err)
|
||||
}
|
||||
|
||||
//fmt.Printf("code: %d\n", code)
|
||||
|
||||
switch code {
|
||||
case proto.RespPartOfValue:
|
||||
q, err := bin.ReadUint32(s.src)
|
||||
@@ -228,13 +282,15 @@ func (s *Connection) ListAllInstantMeasures(metricID uint32) ([]InstantMeasure,
|
||||
return nil, fmt.Errorf("read records qty: %s", err)
|
||||
}
|
||||
|
||||
//fmt.Printf("q: %d\n", q)
|
||||
|
||||
for i := range int(q) {
|
||||
err = bin.ReadNInto(s.src, tmp)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read record #%d: %s", i, err)
|
||||
}
|
||||
|
||||
result = append(result, InstantMeasure{
|
||||
result = append(result, proto.InstantMeasure{
|
||||
Timestamp: bin.GetUint32(tmp),
|
||||
Value: bin.GetFloat64(tmp[4:]),
|
||||
})
|
||||
@@ -252,13 +308,12 @@ func (s *Connection) ListAllInstantMeasures(metricID uint32) ([]InstantMeasure,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Connection) ListInstantMeasures(req proto.ListInstantMeasuresReq) ([]InstantMeasure, error) {
|
||||
func (s *Connection) ListInstantMeasures(req proto.ListInstantMeasuresReq) ([]proto.InstantMeasure, error) {
|
||||
arr := []byte{
|
||||
proto.TypeListInstantMeasures,
|
||||
0, 0, 0, 0, // metricID
|
||||
0, 0, 0, 0, // since
|
||||
0, 0, 0, 0, // until
|
||||
byte(req.FirstHourOfDay),
|
||||
}
|
||||
bin.PutUint32(arr[1:], req.MetricID)
|
||||
bin.PutUint32(arr[5:], req.Since)
|
||||
@@ -269,7 +324,7 @@ func (s *Connection) ListInstantMeasures(req proto.ListInstantMeasuresReq) ([]In
|
||||
}
|
||||
|
||||
var (
|
||||
result []InstantMeasure
|
||||
result []proto.InstantMeasure
|
||||
tmp = make([]byte, 12)
|
||||
)
|
||||
|
||||
@@ -292,7 +347,7 @@ func (s *Connection) ListInstantMeasures(req proto.ListInstantMeasuresReq) ([]In
|
||||
return nil, fmt.Errorf("read record #%d: %s", i, err)
|
||||
}
|
||||
|
||||
result = append(result, InstantMeasure{
|
||||
result = append(result, proto.InstantMeasure{
|
||||
Timestamp: bin.GetUint32(tmp),
|
||||
Value: bin.GetFloat64(tmp[4:]),
|
||||
})
|
||||
@@ -310,13 +365,7 @@ func (s *Connection) ListInstantMeasures(req proto.ListInstantMeasuresReq) ([]In
|
||||
}
|
||||
}
|
||||
|
||||
type CumulativeMeasure struct {
|
||||
Timestamp uint32
|
||||
Value float64
|
||||
Total float64
|
||||
}
|
||||
|
||||
func (s *Connection) ListAllCumulativeMeasures(metricID uint32) ([]CumulativeMeasure, error) {
|
||||
func (s *Connection) ListAllCumulativeMeasures(metricID uint32) ([]proto.CumulativeMeasure, error) {
|
||||
arr := []byte{
|
||||
proto.TypeListAllCumulativeMeasures,
|
||||
0, 0, 0, 0, // metricID
|
||||
@@ -328,7 +377,7 @@ func (s *Connection) ListAllCumulativeMeasures(metricID uint32) ([]CumulativeMea
|
||||
}
|
||||
|
||||
var (
|
||||
result []CumulativeMeasure
|
||||
result []proto.CumulativeMeasure
|
||||
tmp = make([]byte, 20)
|
||||
)
|
||||
|
||||
@@ -338,6 +387,8 @@ func (s *Connection) ListAllCumulativeMeasures(metricID uint32) ([]CumulativeMea
|
||||
return nil, fmt.Errorf("read response code: %s", err)
|
||||
}
|
||||
|
||||
//fmt.Printf("code: %d\n", code)
|
||||
|
||||
switch code {
|
||||
case proto.RespPartOfValue:
|
||||
q, err := bin.ReadUint32(s.src)
|
||||
@@ -351,11 +402,19 @@ func (s *Connection) ListAllCumulativeMeasures(metricID uint32) ([]CumulativeMea
|
||||
return nil, fmt.Errorf("read record #%d: %s", i, err)
|
||||
}
|
||||
|
||||
result = append(result, CumulativeMeasure{
|
||||
//fmt.Printf("tmp: %d\n", tmp)
|
||||
|
||||
result = append(result, proto.CumulativeMeasure{
|
||||
Timestamp: bin.GetUint32(tmp),
|
||||
Value: bin.GetFloat64(tmp[4:]),
|
||||
Total: bin.GetFloat64(tmp[12:]),
|
||||
})
|
||||
|
||||
// pretty.PPrintln("measure", CumulativeMeasure{
|
||||
// Timestamp: bin.GetUint32(tmp),
|
||||
// Value: bin.GetFloat64(tmp[4:]),
|
||||
// Total: bin.GetFloat64(tmp[12:]),
|
||||
// })
|
||||
}
|
||||
|
||||
case proto.RespEndOfValue:
|
||||
@@ -370,13 +429,12 @@ func (s *Connection) ListAllCumulativeMeasures(metricID uint32) ([]CumulativeMea
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Connection) ListCumulativeMeasures(req proto.ListCumulativeMeasuresReq) ([]CumulativeMeasure, error) {
|
||||
func (s *Connection) ListCumulativeMeasures(req proto.ListCumulativeMeasuresReq) ([]proto.CumulativeMeasure, error) {
|
||||
arr := []byte{
|
||||
proto.TypeListCumulativeMeasures,
|
||||
0, 0, 0, 0, // metricID
|
||||
0, 0, 0, 0, // since
|
||||
0, 0, 0, 0, // until
|
||||
byte(req.FirstHourOfDay),
|
||||
}
|
||||
bin.PutUint32(arr[1:], req.MetricID)
|
||||
bin.PutUint32(arr[5:], req.Since)
|
||||
@@ -387,7 +445,7 @@ func (s *Connection) ListCumulativeMeasures(req proto.ListCumulativeMeasuresReq)
|
||||
}
|
||||
|
||||
var (
|
||||
result []CumulativeMeasure
|
||||
result []proto.CumulativeMeasure
|
||||
tmp = make([]byte, 20)
|
||||
)
|
||||
|
||||
@@ -410,7 +468,7 @@ func (s *Connection) ListCumulativeMeasures(req proto.ListCumulativeMeasuresReq)
|
||||
return nil, fmt.Errorf("read record #%d: %s", i, err)
|
||||
}
|
||||
|
||||
result = append(result, CumulativeMeasure{
|
||||
result = append(result, proto.CumulativeMeasure{
|
||||
Timestamp: bin.GetUint32(tmp),
|
||||
Value: bin.GetFloat64(tmp[4:]),
|
||||
Total: bin.GetFloat64(tmp[12:]),
|
||||
@@ -429,16 +487,7 @@ func (s *Connection) ListCumulativeMeasures(req proto.ListCumulativeMeasuresReq)
|
||||
}
|
||||
}
|
||||
|
||||
type InstantPeriod struct {
|
||||
Period uint32
|
||||
Since uint32
|
||||
Until uint32
|
||||
Min float64
|
||||
Max float64
|
||||
Avg float64
|
||||
}
|
||||
|
||||
func (s *Connection) ListInstantPeriods(req proto.ListInstantPeriodsReq) ([]InstantPeriod, error) {
|
||||
func (s *Connection) ListInstantPeriods(req proto.ListInstantPeriodsReq) ([]proto.InstantPeriod, error) {
|
||||
arr := []byte{
|
||||
proto.TypeListInstantPeriods,
|
||||
0, 0, 0, 0, // metricID
|
||||
@@ -447,11 +496,14 @@ func (s *Connection) ListInstantPeriods(req proto.ListInstantPeriodsReq) ([]Inst
|
||||
byte(req.GroupBy),
|
||||
req.AggregateFuncs,
|
||||
byte(req.FirstHourOfDay),
|
||||
byte(req.LastDayOfMonth),
|
||||
}
|
||||
bin.PutUint32(arr[1:], req.MetricID)
|
||||
bin.PutUint32(arr[5:], req.Since)
|
||||
bin.PutUint32(arr[9:], req.Until)
|
||||
bin.PutUint16(arr[5:], uint16(req.Since.Year))
|
||||
arr[7] = byte(req.Since.Month)
|
||||
arr[8] = byte(req.Since.Day)
|
||||
bin.PutUint16(arr[9:], uint16(req.Until.Year))
|
||||
arr[11] = byte(req.Until.Month)
|
||||
arr[12] = byte(req.Until.Day)
|
||||
|
||||
if _, err := s.conn.Write(arr); err != nil {
|
||||
return nil, err
|
||||
@@ -469,7 +521,7 @@ func (s *Connection) ListInstantPeriods(req proto.ListInstantPeriodsReq) ([]Inst
|
||||
}
|
||||
|
||||
var (
|
||||
result []InstantPeriod
|
||||
result []proto.InstantPeriod
|
||||
// 12 bytes - period, since, until
|
||||
// q * 8 bytes - min, max, avg
|
||||
tmp = make([]byte, 12+q*8)
|
||||
@@ -481,6 +533,8 @@ func (s *Connection) ListInstantPeriods(req proto.ListInstantPeriodsReq) ([]Inst
|
||||
return nil, fmt.Errorf("read response code: %s", err)
|
||||
}
|
||||
|
||||
//fmt.Printf("code: %d\n", code)
|
||||
|
||||
switch code {
|
||||
case proto.RespPartOfValue:
|
||||
q, err := bin.ReadUint32(s.src)
|
||||
@@ -495,7 +549,7 @@ func (s *Connection) ListInstantPeriods(req proto.ListInstantPeriodsReq) ([]Inst
|
||||
}
|
||||
|
||||
var (
|
||||
p = InstantPeriod{
|
||||
p = proto.InstantPeriod{
|
||||
Period: bin.GetUint32(tmp[0:]),
|
||||
Since: bin.GetUint32(tmp[4:]),
|
||||
Until: bin.GetUint32(tmp[8:]),
|
||||
@@ -530,15 +584,7 @@ func (s *Connection) ListInstantPeriods(req proto.ListInstantPeriodsReq) ([]Inst
|
||||
}
|
||||
}
|
||||
|
||||
type CumulativePeriod struct {
|
||||
Period uint32
|
||||
Since uint32
|
||||
Until uint32
|
||||
EndValue float64
|
||||
Total float64
|
||||
}
|
||||
|
||||
func (s *Connection) ListCumulativePeriods(req proto.ListCumulativePeriodsReq) ([]CumulativePeriod, error) {
|
||||
func (s *Connection) ListCumulativePeriods(req proto.ListCumulativePeriodsReq) ([]proto.CumulativePeriod, error) {
|
||||
arr := []byte{
|
||||
proto.TypeListCumulativePeriods,
|
||||
0, 0, 0, 0, // metricID
|
||||
@@ -546,18 +592,21 @@ func (s *Connection) ListCumulativePeriods(req proto.ListCumulativePeriodsReq) (
|
||||
0, 0, 0, 0, // until
|
||||
byte(req.GroupBy),
|
||||
byte(req.FirstHourOfDay),
|
||||
byte(req.LastDayOfMonth),
|
||||
}
|
||||
bin.PutUint32(arr[1:], req.MetricID)
|
||||
bin.PutUint32(arr[5:], req.Since)
|
||||
bin.PutUint32(arr[9:], req.Until)
|
||||
bin.PutUint16(arr[5:], uint16(req.Since.Year))
|
||||
arr[7] = byte(req.Since.Month)
|
||||
arr[8] = byte(req.Since.Day)
|
||||
bin.PutUint16(arr[9:], uint16(req.Until.Year))
|
||||
arr[11] = byte(req.Until.Month)
|
||||
arr[12] = byte(req.Until.Day)
|
||||
|
||||
if _, err := s.conn.Write(arr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
result []CumulativePeriod
|
||||
result []proto.CumulativePeriod
|
||||
tmp = make([]byte, 28)
|
||||
)
|
||||
|
||||
@@ -579,7 +628,7 @@ func (s *Connection) ListCumulativePeriods(req proto.ListCumulativePeriodsReq) (
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read record #%d: %s", i, err)
|
||||
}
|
||||
result = append(result, CumulativePeriod{
|
||||
result = append(result, proto.CumulativePeriod{
|
||||
Period: bin.GetUint32(tmp[0:]),
|
||||
Since: bin.GetUint32(tmp[4:]),
|
||||
Until: bin.GetUint32(tmp[8:]),
|
||||
@@ -600,13 +649,7 @@ func (s *Connection) ListCumulativePeriods(req proto.ListCumulativePeriodsReq) (
|
||||
}
|
||||
}
|
||||
|
||||
type CurrentValue struct {
|
||||
MetricID uint32
|
||||
Timestamp uint32
|
||||
Value float64
|
||||
}
|
||||
|
||||
func (s *Connection) ListCurrentValues(metricIDs []uint32) ([]CurrentValue, error) {
|
||||
func (s *Connection) ListCurrentValues(metricIDs []uint32) ([]proto.CurrentValue, error) {
|
||||
arr := make([]byte, 3+metricKeySize*len(metricIDs))
|
||||
arr[0] = proto.TypeListCurrentValues
|
||||
|
||||
@@ -623,7 +666,7 @@ func (s *Connection) ListCurrentValues(metricIDs []uint32) ([]CurrentValue, erro
|
||||
}
|
||||
|
||||
var (
|
||||
result []CurrentValue
|
||||
result []proto.CurrentValue
|
||||
tmp = make([]byte, 16)
|
||||
)
|
||||
|
||||
@@ -646,7 +689,7 @@ func (s *Connection) ListCurrentValues(metricIDs []uint32) ([]CurrentValue, erro
|
||||
return nil, fmt.Errorf("read record #%d: %s", i, err)
|
||||
}
|
||||
|
||||
result = append(result, CurrentValue{
|
||||
result = append(result, proto.CurrentValue{
|
||||
MetricID: bin.GetUint32(tmp),
|
||||
Timestamp: bin.GetUint32(tmp[4:]),
|
||||
Value: bin.GetFloat64(tmp[8:]),
|
||||
@@ -680,55 +723,6 @@ func (s *Connection) DeleteMeasures(req proto.DeleteMeasuresReq) (err error) {
|
||||
return s.mustSuccess(s.src)
|
||||
}
|
||||
|
||||
type RangeTotalResp struct {
|
||||
Since uint32
|
||||
SinceValue float64
|
||||
Until uint32
|
||||
UntilValue float64
|
||||
}
|
||||
|
||||
func (s *Connection) RangeTotal(req proto.RangeTotalReq) (*RangeTotalResp, error) {
|
||||
arr := []byte{
|
||||
proto.TypeGetMetric,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
}
|
||||
bin.PutUint32(arr[1:], req.MetricID)
|
||||
bin.PutUint32(arr[5:], req.Since)
|
||||
bin.PutUint32(arr[9:], req.MetricID)
|
||||
|
||||
if _, err := s.conn.Write(arr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
code, err := s.src.ReadByte()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response code: %s", err)
|
||||
}
|
||||
|
||||
switch code {
|
||||
case proto.RespValue:
|
||||
arr, err := s.src.ReadN(24)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read body: %s", err)
|
||||
}
|
||||
|
||||
return &RangeTotalResp{
|
||||
Since: bin.GetUint32(arr),
|
||||
SinceValue: bin.GetFloat64(arr[4:]),
|
||||
Until: bin.GetUint32(arr[12:]),
|
||||
UntilValue: bin.GetFloat64(arr[16:]),
|
||||
}, nil
|
||||
|
||||
case proto.RespError:
|
||||
return nil, s.onError()
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown reponse code %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Connection) onError() error {
|
||||
errorCode, err := bin.ReadUint16(s.src)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user