-
Notifications
You must be signed in to change notification settings - Fork 18
/
subscription.go
174 lines (161 loc) · 3.93 KB
/
subscription.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"fmt"
"log"
"math"
"sync"
"github.com/gtfierro/ob"
"github.com/pkg/errors"
bw2 "gopkg.in/immesys/bw2bind.v5"
)
type subscription struct {
c chan *bw2.SimpleMessage
valueExpr []ob.Operation
values []float64
size int
num int
uri string
uuid string
sync.Mutex
}
func (sub *subscription) getValues() []float64 {
sub.Lock()
defer sub.Unlock()
var f = make([]float64, len(sub.values))
copy(f, sub.values)
return f
}
func (sub *subscription) addValues(f []float64) {
sub.Lock()
defer sub.Unlock()
if len(f) > sub.size {
sub.values = f[:sub.size]
sub.num = len(f)
} else if sub.num == sub.size {
sub.values = append(sub.values[len(f):], f...)
} else {
sub.values = append(sub.values, f...)
sub.num += len(f)
}
}
type subscriptionParams struct {
uri string
ponum string
extract string
size int
uuid string
}
func (d *dash) newSubscription(params subscriptionParams) (*subscription, error) {
c, err := d.client.Subscribe(&bw2.SubscribeParams{
URI: params.uri,
})
if err != nil {
return nil, err
}
sub := &subscription{
c: c,
valueExpr: ob.Parse(params.extract),
num: 0,
size: params.size,
uuid: params.uuid,
uri: params.uri,
}
if sub.uuid != "" {
_, ts, _, err := d.archiver.Query(fmt.Sprintf("select data in (now, now -30min) where uuid = \"%s\"", sub.uuid), 30)
if err != nil {
return nil, err
}
sub.Lock()
if len(ts.Data) > 0 {
vals := ts.Data[0].Values
if len(vals) > sub.size {
sub.values = vals[:sub.size]
sub.num = len(vals[:sub.size])
} else if len(vals) < sub.size {
sub.values = vals
sub.num = len(vals)
}
}
sub.Unlock()
}
go func() {
for msg := range sub.c {
po := msg.GetOnePODF(params.ponum)
if po == nil {
continue
}
var thing interface{}
err := po.(bw2.MsgPackPayloadObject).ValueInto(&thing)
if err != nil {
log.Println(errors.Wrap(err, "Could not unmarshal msgpack object"))
panic(err)
continue
}
// extract the possible value
value := ob.Eval(sub.valueExpr, thing)
if value == nil {
//log.Printf("Could not extract value from %+v", thing)
continue
}
f64s, err := getFloat64(value)
if err != nil {
panic(err)
log.Println(errors.Wrapf(err, "Could not get float64 values from %+v", value))
continue
}
sub.addValues(f64s)
}
}()
return sub, nil
}
func getFloat64(value interface{}) ([]float64, error) {
var values []float64
// generate the timeseries values from our extracted value, and then save it
// test if the value is a list
if value_list, ok := value.([]interface{}); ok {
for _, _val := range value_list {
value_f64, ok := _val.(float64)
if !ok {
if value_u64, ok := value.(uint64); ok {
value_f64 = float64(value_u64)
} else if value_i64, ok := value.(int64); ok {
value_f64 = float64(value_i64)
} else if value_bool, ok := value.(bool); ok {
if value_bool {
value_f64 = float64(1)
} else {
value_f64 = float64(0)
}
} else {
return values, fmt.Errorf("Value %+v was not a float64 (was %T)", value, value)
}
}
if math.IsInf(value_f64, 0) || math.IsNaN(value_f64) {
return values, fmt.Errorf("Invalid number %f", value_f64)
}
values = append(values, value_f64)
}
} else {
value_f64, ok := value.(float64)
if !ok {
if value_u64, ok := value.(uint64); ok {
value_f64 = float64(value_u64)
} else if value_i64, ok := value.(int64); ok {
value_f64 = float64(value_i64)
} else if value_bool, ok := value.(bool); ok {
if value_bool {
value_f64 = float64(1)
} else {
value_f64 = float64(0)
}
} else {
return values, fmt.Errorf("Value %+v was not a float64 (was %T)", value, value)
}
}
if math.IsInf(value_f64, 0) || math.IsNaN(value_f64) {
return values, fmt.Errorf("Invalid number %f", value_f64)
}
values = append(values, value_f64)
}
return values, nil
}