спеціалізована СУБД для зберігання та обробки показань датчиків та лічильників
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
diploma/proto/proto.go

441 lines
9.2 KiB

3 months ago
package proto
import (
"fmt"
3 months ago
"time"
3 months ago
3 months ago
"gordenko.dev/dima/diploma"
3 months ago
"gordenko.dev/dima/diploma/bin"
"gordenko.dev/dima/diploma/bufreader"
)
const (
3 months ago
TypeDeleteMeasures byte = 1
TypeListCurrentValues byte = 2
TypeListInstantMeasures byte = 3
TypeListCumulativeMeasures byte = 33
TypeListInstantPeriods byte = 4
TypeListCumulativePeriods byte = 44
TypeGetMetric byte = 5
TypeAddMetric byte = 6
3 months ago
TypeListAllInstantMeasures byte = 8
TypeListAllCumulativeMeasures byte = 88
TypeAppendMeasure byte = 10
TypeAppendMeasures byte = 11
TypeDeleteMetric byte = 12
RespPartOfValue byte = 255
RespEndOfValue byte = 254
RespError byte = 253
RespSuccess byte = 252
RespValue byte = 251
ErrNoMetric = 1
ErrDuplicate = 2
ErrWrongMetricType = 3
ErrWrongFracDigits = 4
ErrExpiredMeasure = 5
ErrNonMonotonicValue = 6
ErrEmptyMetricID = 7
ErrInvalidRange = 8
ErrUnexpected = 9
)
func ErrorCodeToText(code uint16) string {
switch code {
case ErrNoMetric:
return "NoMetric"
case ErrDuplicate:
return "Duplicate"
case ErrWrongMetricType:
return "WrongMetricType"
case ErrWrongFracDigits:
return "WrongFracDigits"
case ErrExpiredMeasure:
return "ExpiredMeasure"
case ErrNonMonotonicValue:
return "NonMonotonicValue"
case ErrEmptyMetricID:
return "EmptyMetricID"
case ErrInvalidRange:
return "InvalidRange"
case ErrUnexpected:
return "Unexpected"
default:
return ""
}
}
3 months ago
// common
3 months ago
3 months ago
type Metric struct {
3 months ago
MetricID uint32
3 months ago
MetricType diploma.MetricType
3 months ago
FracDigits int
}
3 months ago
type AppendError struct {
MetricID uint32
ErrorCode uint16
3 months ago
}
3 months ago
type TimeBound struct {
Year int
Month time.Month
Day int
3 months ago
}
3 months ago
type CumulativeMeasure struct {
3 months ago
Timestamp uint32
Value float64
3 months ago
Total float64
3 months ago
}
3 months ago
type CumulativePeriod struct {
Period uint32
Since uint32
Until uint32
EndValue float64
Total float64
3 months ago
}
3 months ago
type InstantMeasure struct {
Timestamp uint32
Value float64
3 months ago
}
3 months ago
type InstantPeriod struct {
Period uint32
Since uint32
Until uint32
Min float64
Max float64
Avg float64
3 months ago
}
3 months ago
type CurrentValue struct {
MetricID uint32
Timestamp uint32
Value float64
3 months ago
}
3 months ago
// API reqs
3 months ago
3 months ago
type GetMetricReq struct {
MetricID uint32
3 months ago
}
3 months ago
func ReadGetMetricReq(r *bufreader.BufferedReader) (m GetMetricReq, err error) {
m.MetricID, err = bin.ReadUint32(r)
if err != nil {
err = fmt.Errorf("read req: %s", err)
return
}
return
3 months ago
}
3 months ago
type ListCurrentValuesReq struct {
MetricIDs []uint32
3 months ago
}
3 months ago
func ReadListCurrentValuesReq(r *bufreader.BufferedReader) (m ListCurrentValuesReq, err error) {
qty, err := bin.ReadUint16(r)
if err != nil {
err = fmt.Errorf("read req: %s", err)
return
3 months ago
}
3 months ago
for i := range int(qty) {
var metricID uint32
metricID, err = bin.ReadUint32(r)
if err != nil {
err = fmt.Errorf("read metricID (#%d): %s", i, err)
return
}
m.MetricIDs = append(m.MetricIDs, metricID)
3 months ago
}
3 months ago
return
3 months ago
}
3 months ago
type AddMetricReq struct {
MetricID uint32
MetricType diploma.MetricType
FracDigits int
3 months ago
}
3 months ago
func ReadAddMetricReq(r *bufreader.BufferedReader) (m AddMetricReq, err error) {
arr, err := r.ReadN(6)
if err != nil {
err = fmt.Errorf("read req: %s", err)
return
3 months ago
}
3 months ago
return UnpackAddMetricReq(arr), nil
3 months ago
}
func UnpackAddMetricReq(arr []byte) (m AddMetricReq) {
m.MetricID = bin.GetUint32(arr)
3 months ago
m.MetricType = diploma.MetricType(arr[4])
3 months ago
m.FracDigits = int(arr[5])
return
}
3 months ago
type DeleteMetricReq struct {
MetricID uint32
3 months ago
}
3 months ago
func ReadDeleteMetricReq(r *bufreader.BufferedReader) (m DeleteMetricReq, err error) {
m.MetricID, err = bin.ReadUint32(r)
if err != nil {
err = fmt.Errorf("read req: %s", err)
return
}
3 months ago
return
}
3 months ago
type DeleteMeasuresReq struct {
MetricID uint32
Since uint32 // timestamp (optional)
}
func ReadDeleteMeasuresReq(r *bufreader.BufferedReader) (m DeleteMeasuresReq, err error) {
arr, err := r.ReadN(8)
if err != nil {
err = fmt.Errorf("read req: %s", err)
return
}
return UnpackDeleteMeasuresReq(arr), nil
3 months ago
}
func UnpackDeleteMeasuresReq(arr []byte) (m DeleteMeasuresReq) {
m.MetricID = bin.GetUint32(arr)
m.Since = bin.GetUint32(arr[4:])
return
}
3 months ago
type AppendMeasureReq struct {
MetricID uint32
Timestamp uint32
Value float64
3 months ago
}
3 months ago
func ReadAppendMeasureReq(r *bufreader.BufferedReader) (m AppendMeasureReq, err error) {
arr, err := r.ReadN(16)
if err != nil {
err = fmt.Errorf("read req: %s", err)
return
}
return UnpackAppendMeasureReq(arr), nil
3 months ago
}
3 months ago
func UnpackAppendMeasureReq(arr []byte) (m AppendMeasureReq) {
3 months ago
m.MetricID = bin.GetUint32(arr)
3 months ago
m.Timestamp = bin.GetUint32(arr[4:])
m.Value = bin.GetFloat64(arr[8:])
3 months ago
return
}
3 months ago
type AppendMeasuresReq struct {
MetricID uint32
Measures []Measure
3 months ago
}
3 months ago
type Measure struct {
Timestamp uint32
Value float64
3 months ago
}
3 months ago
func ReadAppendMeasuresReq(r *bufreader.BufferedReader) (m AppendMeasuresReq, err error) {
prefix, err := bin.ReadN(r, 6) // metricID + measures qty
3 months ago
if err != nil {
3 months ago
err = fmt.Errorf("read prefix: %s", err)
3 months ago
return
}
3 months ago
m.MetricID = bin.GetUint32(prefix[0:])
qty := bin.GetUint16(prefix[4:])
3 months ago
3 months ago
for i := range int(qty) {
var measure Measure
measure.Timestamp, err = bin.ReadUint32(r)
if err != nil {
err = fmt.Errorf("read timestamp (#%d): %s", i, err)
return
}
measure.Value, err = bin.ReadFloat64(r)
if err != nil {
err = fmt.Errorf("read value (#%d): %s", i, err)
return
}
m.Measures = append(m.Measures, measure)
3 months ago
}
3 months ago
return
3 months ago
}
3 months ago
type MetricMeasure struct {
MetricID uint32
Timestamp uint32
Value float64
3 months ago
}
3 months ago
func ReadAppendMeasurePerMetricReq(r *bufreader.BufferedReader) (measures []MetricMeasure, err error) {
qty, err := bin.ReadUint16(r)
3 months ago
if err != nil {
return
}
3 months ago
var tmp = make([]byte, 16)
for range int(qty) {
err = bin.ReadNInto(r, tmp)
if err != nil {
return
}
measures = append(measures, MetricMeasure{
MetricID: bin.GetUint32(tmp[0:]),
Timestamp: bin.GetUint32(tmp[4:]),
Value: bin.GetFloat64(tmp[8:]),
})
}
return
3 months ago
}
3 months ago
type ListAllInstantMetricMeasuresReq struct {
MetricID uint32
3 months ago
}
func ReadListAllInstantMeasuresReq(r *bufreader.BufferedReader) (m ListAllInstantMetricMeasuresReq, err error) {
m.MetricID, err = bin.ReadUint32(r)
if err != nil {
err = fmt.Errorf("read req: %s", err)
return
}
return
}
3 months ago
type ListAllCumulativeMeasuresReq struct {
MetricID uint32
}
3 months ago
func ReadListAllCumulativeMeasuresReq(r *bufreader.BufferedReader) (m ListAllCumulativeMeasuresReq, err error) {
m.MetricID, err = bin.ReadUint32(r)
if err != nil {
err = fmt.Errorf("read req: %s", err)
return
}
return
}
3 months ago
type ListInstantMeasuresReq struct {
MetricID uint32
Since uint32
Until uint32
}
3 months ago
func ReadListInstantMeasuresReq(r *bufreader.BufferedReader) (m ListInstantMeasuresReq, err error) {
3 months ago
arr, err := r.ReadN(12)
3 months ago
if err != nil {
err = fmt.Errorf("read req: %s", err)
return
}
return UnpackListInstantMeasuresReq(arr), nil
}
3 months ago
func UnpackListInstantMeasuresReq(arr []byte) (m ListInstantMeasuresReq) {
m.MetricID = bin.GetUint32(arr[0:])
m.Since = bin.GetUint32(arr[4:])
m.Until = bin.GetUint32(arr[8:])
return
}
type ListCumulativeMeasuresReq struct {
MetricID uint32
Since uint32
Until uint32
}
3 months ago
func ReadListCumulativeMeasuresReq(r *bufreader.BufferedReader) (m ListCumulativeMeasuresReq, err error) {
3 months ago
arr, err := r.ReadN(12)
3 months ago
if err != nil {
err = fmt.Errorf("read req: %s", err)
return
}
return UnpackListCumulativeMeasuresReq(arr), nil
}
3 months ago
func UnpackListCumulativeMeasuresReq(arr []byte) (m ListCumulativeMeasuresReq) {
m.MetricID = bin.GetUint32(arr)
m.Since = bin.GetUint32(arr[4:])
m.Until = bin.GetUint32(arr[8:])
return
3 months ago
}
3 months ago
type ListInstantPeriodsReq struct {
MetricID uint32
Since TimeBound
Until TimeBound
GroupBy diploma.GroupBy
AggregateFuncs byte
FirstHourOfDay int
3 months ago
}
3 months ago
func ReadListInstantPeriodsReq(r *bufreader.BufferedReader) (m ListInstantPeriodsReq, err error) {
arr, err := r.ReadN(15)
3 months ago
if err != nil {
err = fmt.Errorf("read req: %s", err)
return
}
3 months ago
return UnpackListInstantPeriodsReq(arr), nil
3 months ago
}
3 months ago
func UnpackListInstantPeriodsReq(arr []byte) (m ListInstantPeriodsReq) {
m.MetricID = bin.GetUint32(arr)
m.Since = TimeBound{
Year: int(bin.GetUint16(arr[4:])),
Month: time.Month(arr[6]),
Day: int(arr[7]),
3 months ago
}
3 months ago
m.Until = TimeBound{
Year: int(bin.GetUint16(arr[8:])),
Month: time.Month(arr[10]),
Day: int(arr[11]),
3 months ago
}
3 months ago
m.GroupBy = diploma.GroupBy(arr[12])
m.AggregateFuncs = arr[13]
m.FirstHourOfDay = int(arr[14])
3 months ago
return
}
3 months ago
type ListCumulativePeriodsReq struct {
MetricID uint32
Since TimeBound
Until TimeBound
GroupBy diploma.GroupBy
FirstHourOfDay int
3 months ago
}
3 months ago
func ReadListCumulativePeriodsReq(r *bufreader.BufferedReader) (m ListCumulativePeriodsReq, err error) {
arr, err := r.ReadN(14)
3 months ago
if err != nil {
3 months ago
err = fmt.Errorf("read req: %s", err)
3 months ago
return
}
3 months ago
return UnpackListCumulativePeriodsReq(arr), nil
}
3 months ago
3 months ago
func UnpackListCumulativePeriodsReq(arr []byte) (m ListCumulativePeriodsReq) {
m.MetricID = bin.GetUint32(arr[0:])
m.Since = TimeBound{
Year: int(bin.GetUint16(arr[4:])),
Month: time.Month(arr[6]),
Day: int(arr[7]),
3 months ago
}
3 months ago
m.Until = TimeBound{
Year: int(bin.GetUint16(arr[8:])),
Month: time.Month(arr[10]),
Day: int(arr[11]),
}
m.GroupBy = diploma.GroupBy(arr[12])
m.FirstHourOfDay = int(arr[13])
3 months ago
return
}