This repository was archived by the owner on Apr 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbwsample.go
78 lines (65 loc) · 1.45 KB
/
bwsample.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
package main
import (
"errors"
"time"
"unsafe"
)
const (
bwSampleSize = 8 * 3 //3 64bit integers
)
var (
errBWTypeConversion = errors.New("type is not a BWSample")
errInvalidBufferSize = errors.New("Invalid buffer size")
)
type BWSample struct {
Ts time.Time //timestamp
BytesUp uint64 //bytes
BytesDown uint64 //bytes
}
func NewBwSample() Sample {
return &BWSample{}
}
func (s *BWSample) After(ts time.Time) bool {
return !s.Ts.Before(ts)
}
func (s *BWSample) Add(sn Sample) error {
x, ok := sn.(*BWSample)
if !ok {
return errBWTypeConversion
}
s.BytesUp += x.BytesUp
s.BytesDown += x.BytesDown
return nil
}
func (s *BWSample) Decode(b []byte) error {
if len(b) != bwSampleSize {
return errInvalidBufferSize
}
s.Ts = time.Unix(0, *(*int64)(unsafe.Pointer(&b[0])))
s.BytesUp = *(*uint64)(unsafe.Pointer(&b[8]))
s.BytesDown = *(*uint64)(unsafe.Pointer(&b[16]))
return nil
}
func (s *BWSample) Encode() []byte {
buff := make([]byte, bwSampleSize)
*(*int64)(unsafe.Pointer(&buff[0])) = s.Ts.UnixNano()
*(*uint64)(unsafe.Pointer(&buff[8])) = s.BytesUp
*(*uint64)(unsafe.Pointer(&buff[16])) = s.BytesDown
return buff
}
func (s *BWSample) TimeLabel(fmt string) []byte {
return []byte(s.Ts.Format(fmt))
}
func (s *BWSample) TS() time.Time {
return s.Ts
}
func (s *BWSample) SetTS(t time.Time) {
s.Ts = t
}
func (s *BWSample) Less(x Sample) bool {
sn, ok := x.(*BWSample)
if !ok {
return false
}
return s.Ts.Before(sn.Ts)
}