/
ring.go
159 lines (131 loc) · 2.72 KB
/
ring.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package ring
import (
"bytes"
"log"
"text/template"
"time"
)
const ts = `[{{range $i, $a := .}} {{if $i}},{{end}}[new Date("{{$a.TimeStamp.Format "Mon Jan _2 15:04:05 2006"}}"),{{printf "%.2f" $a.Value}}]{{end}}]`
var (
t *template.Template
// DefaultCapacity is max size of ring
DefaultCapacity = 256
)
func init() {
t = template.Must(template.New("ring").Parse(ts))
}
// TimeMeasure compose Time and Measure
type TimeMeasure interface {
Time
Measure
}
// Measure value of the measure
type Measure interface {
//Value from the measure
Value() float64
}
//Time is the timestamp of enqueue
type Time interface {
//Timestamp from the measure
TimeStamp() time.Time
}
//Ring structure
type Ring struct {
head int
tail int
buff []TimeMeasure
}
/*SetCapacity fix the maximum capacity of the ring*/
func (r *Ring) SetCapacity(size int) {
r.init()
r.extends(size)
}
/*Capacity returns the capacity of ringbuffer*/
func (r *Ring) Capacity() int {
return len(r.buff)
}
//Enqueue enqueues measure of the ring
func (r *Ring) Enqueue(c TimeMeasure) {
if r == nil {
log.Fatal("Ring is nil, not initialised stores[ \"xx\"] = &ring.Ring{}")
}
r.init()
r.set(r.head+1, c)
old := r.head
r.head = r.mod(r.head + 1)
if old != -1 && r.head == r.tail {
r.tail = r.mod(r.tail + 1)
}
}
//Dequeue dequeues measure of the ring
func (r *Ring) Dequeue() Measure {
r.init()
if r.head == -1 {
return nil
}
v := r.get(r.tail)
if r.tail == r.head {
r.head = -1
r.tail = 0
} else {
r.tail = r.mod(r.tail + 1)
}
return v
}
//Values return array of timeMeasure
func (r *Ring) Values() []TimeMeasure {
if r.head == -1 {
return nil
}
arr := make([]TimeMeasure, 0, r.Capacity())
for i := 0; i < r.Capacity(); i++ {
idx := r.mod(i + r.tail)
arr = append(arr, r.get(idx))
if idx == r.head {
break
}
}
return arr
}
//DumpLine return String of ring
func (r *Ring) DumpLine() (string, error) {
var result bytes.Buffer
if err := t.Execute(&result, r.Values()); err != nil {
return "", err
}
return result.String(), nil
}
/*------------------------------------ */
func (r *Ring) mod(p int) int {
return p % len(r.buff)
}
func (r *Ring) init() {
if r.buff == nil {
r.buff = make([]TimeMeasure, DefaultCapacity)
for i := 0; i < len(r.buff); i++ {
r.buff[i] = nil
}
r.head = -1
r.tail = 0
}
}
func (r *Ring) extends(size int) {
if size == len(r.buff) {
return
}
if size < len(r.buff) {
r.buff = r.buff[0:size]
return
}
newbuffer := make([]TimeMeasure, size-len(r.buff))
for i := 0; i < len(newbuffer); i++ {
newbuffer[i] = nil
}
r.buff = append(r.buff, newbuffer...)
}
func (r *Ring) set(i int, b TimeMeasure) {
r.buff[r.mod(i)] = b
}
func (r *Ring) get(i int) TimeMeasure {
return r.buff[r.mod(i)]
}