chawk 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,274 +2,250 @@ require 'test_helper'
2
2
 
3
3
  describe Chawk do
4
4
  before do
5
- #@board = Chawk::Board.new()
5
+ Chawk.clear_all_data!
6
6
  @agent = Chawk::Models::Agent.first || Chawk::Models::Agent.create(:name=>"Test User")
7
- @addr = Chawk.addr(@agent,'a:b')
8
- @addr.points.destroy_all
7
+ @node = Chawk.node(@agent,'a:b')
9
8
  end
10
9
 
11
10
  it "has length" do
12
- @addr.points.must_respond_to(:length)
11
+ @node.points.must_respond_to(:length)
13
12
  end
14
13
 
15
14
  it "calculates length" do
16
- @addr.points.destroy_all
17
- @addr.points.length.must_equal(0)
18
- @addr.add_points 2
19
- @addr.points.length.must_equal(1)
20
- @addr.add_points 2
21
- @addr.points.length.must_equal(2)
22
- @addr.add_points 2
23
- @addr.points.length.must_equal(3)
24
- @addr.add_points [1,2,3,4]
25
- @addr.points.length.must_equal(7)
26
- end
27
-
28
- # it "has destroy_all" do
29
- # @addr.points.must_respond_to(:destroy_all)
30
- # end
15
+ @node.clear_points!
16
+ @node.points.length.must_equal(0)
17
+ @node.add_points 2
18
+ @node.points.length.must_equal(1)
19
+ @node.add_points 2
20
+ @node.points.length.must_equal(2)
21
+ @node.add_points 2
22
+ @node.points.length.must_equal(3)
23
+ @node.add_points [1,2,3,4]
24
+ @node.points.length.must_equal(7)
25
+ end
26
+
27
+ it "clears points" do
28
+ @node.must_respond_to(:clear_points!)
29
+ end
31
30
 
32
31
  it "clears history" do
33
- @addr.add_points [1,2,3,4]
34
- @addr.points.length.must_equal(4)
35
- @addr.points.destroy_all
36
- @addr.points.length.must_equal(0)
32
+ @node.add_points [1,2,3,4]
33
+ @node.points.length.must_equal(4)
34
+ @node.clear_points!
35
+ @node.points.length.must_equal(0)
37
36
  end
38
37
 
39
38
  it "doesn't clear the wrong history" do
40
- addr2 = Chawk.addr(@agent,'a:b')
41
- addr2.points.destroy_all
42
- @addr.points.destroy_all
43
- addr2.add_points [1,2,3,4]
44
- @addr.add_points [1,2,3,4]
45
- addr2.points.destroy_all
46
- addr2.points.length.must_equal(0)
47
- @addr.points.length.must_equal(4)
39
+ node2 = Chawk.node(@agent,'a:b')
40
+ node2.clear_points!
41
+ @node.clear_points!
42
+ node2.add_points [1,2,3,4]
43
+ @node.add_points [1,2,3,4]
44
+ node2.clear_points!
45
+ node2.points.length.must_equal(0)
46
+ @node.points.length.must_equal(4)
48
47
  end
49
48
 
50
49
  it "accepts _insert_point" do
51
- @addr._insert_point(100,Time.now)
52
- @addr.points.length.must_equal(1)
50
+ @node._insert_point(100,Time.now)
51
+ @node.points.length.must_equal(1)
53
52
  end
54
53
 
55
54
  it "accepts add_points" do
56
- @addr.must_respond_to(:"add_points")
55
+ @node.must_respond_to(:"add_points")
57
56
  end
58
57
 
59
58
  it "accepts integers" do
60
- @addr.add_points 10
61
- @addr.points.length.must_equal(1)
62
- @addr.add_points 0
63
- @addr.points.length.must_equal(2)
64
- @addr.add_points 190
65
- @addr.add_points 10002
66
- @addr.add_points [10,0,190,100]
59
+ @node.add_points 10
60
+ @node.points.length.must_equal(1)
61
+ @node.add_points 0
62
+ @node.points.length.must_equal(2)
63
+ @node.add_points 190
64
+ @node.add_points 10002
65
+ @node.add_points [10,0,190,100]
67
66
  dt = Time.now.to_f
68
- @addr.add_points [[10,dt],[0,dt],[190,dt],[100,dt]]
69
- @addr.add_points [{"v"=>10},{"v"=>0},{"v"=>190},{"v"=>100,"t"=>dt}]
70
- @addr.add_points [{"v"=>10,"t"=>dt},{"v"=>0,"t"=>dt},{"v"=>190,"t"=>dt},{"v"=>100,"t"=>dt}]
71
- @addr.add_points [{"t"=>dt,"v"=>10},{"v"=>0,"t"=>dt},{"t"=>dt,"v"=>190},{"v"=>100,"t"=>dt}]
67
+ @node.add_points [[10,dt],[0,dt],[190,dt],[100,dt]]
68
+ @node.add_points [{"v"=>10},{"v"=>0},{"v"=>190},{"v"=>100,"t"=>dt}]
69
+ @node.add_points [{"v"=>10,"t"=>dt},{"v"=>0,"t"=>dt},{"v"=>190,"t"=>dt},{"v"=>100,"t"=>dt}]
70
+ @node.add_points [{"t"=>dt,"v"=>10},{"v"=>0,"t"=>dt},{"t"=>dt,"v"=>190},{"v"=>100,"t"=>dt}]
72
71
  end
73
72
 
74
73
  it "does increment" do
75
- @addr.points.destroy_all
76
- @addr.add_points 99
77
- @addr.must_respond_to(:increment)
78
- @addr.increment 10
79
- @addr.increment
80
- @addr.points.last.value.must_equal(110)
81
- @addr.increment -10
82
- @addr.points.last.value.must_equal(100)
83
- @addr.increment
84
- @addr.points.last.value.must_equal(101)
74
+ @node.clear_points!
75
+ @node.add_points 99
76
+ @node.must_respond_to(:increment)
77
+ @node.increment 10
78
+ @node.increment
79
+ @node.points.last.value.must_equal(110)
80
+ @node.increment -10
81
+ @node.points.last.value.must_equal(100)
82
+ @node.increment
83
+ @node.points.last.value.must_equal(101)
85
84
  end
86
85
 
87
86
  it "should only increment integers" do
88
- lambda {@addr.increment 'A'}.must_raise(ArgumentError)
89
- lambda {@addr.increment nil}.must_raise(ArgumentError)
87
+ lambda {@node.increment 'A'}.must_raise(ArgumentError)
88
+ lambda {@node.increment nil}.must_raise(ArgumentError)
90
89
  end
91
90
 
92
91
  it "does -" do
93
- @addr.points.must_respond_to(:"-")
94
- @addr.add_points 10
95
- @addr.decrement 100
96
- @addr.points.last.value.must_equal(-90)
97
- @addr.decrement -10
98
- @addr.points.last.value.must_equal(-80)
99
- @addr.decrement
100
- @addr.points.last.value.must_equal(-81)
92
+ @node.points.must_respond_to(:"-")
93
+ @node.add_points 10
94
+ @node.decrement 100
95
+ @node.points.last.value.must_equal(-90)
96
+ @node.decrement -10
97
+ @node.points.last.value.must_equal(-80)
98
+ @node.decrement
99
+ @node.points.last.value.must_equal(-81)
101
100
  end
102
101
 
103
102
  it "should only - integers" do
104
- lambda {@addr.decrement 'A'}.must_raise(ArgumentError)
105
- lambda {@addr.decrement nil}.must_raise(ArgumentError)
103
+ lambda {@node.decrement 'A'}.must_raise(ArgumentError)
104
+ lambda {@node.decrement nil}.must_raise(ArgumentError)
106
105
  end
107
106
 
108
107
  it "only accepts integers in proper formats" do
109
- lambda {@addr.add_points 10.0}.must_raise(ArgumentError)
110
- lambda {@addr.add_points nil}.must_raise(ArgumentError)
111
- lambda {@addr.add_points [10.0,:x]}.must_raise(ArgumentError)
112
- lambda {@addr.add_points [[10,10,10],[10,10,20]]}.must_raise(ArgumentError)
108
+ lambda {@node.add_points 10.0}.must_raise(ArgumentError)
109
+ lambda {@node.add_points nil}.must_raise(ArgumentError)
110
+ lambda {@node.add_points [10.0,:x]}.must_raise(ArgumentError)
111
+ lambda {@node.add_points [[10,10,10],[10,10,20]]}.must_raise(ArgumentError)
113
112
  dt = Time.now.to_f
114
- lambda {@addr.add_points [{"x"=>10,"t"=>dt},{"x"=>0,"t"=>dt}]}.must_raise(ArgumentError)
113
+ lambda {@node.add_points [{"x"=>10,"t"=>dt},{"x"=>0,"t"=>dt}]}.must_raise(ArgumentError)
115
114
  end
116
115
 
117
116
  it "accepts string integers in proper formats" do
118
- lambda {@addr.add_points "X"}.must_raise(ArgumentError)
119
- @addr.add_points "10"
120
- @addr.points.length.must_equal(1)
121
- @addr.add_points "0"
122
- @addr.points.length.must_equal(2)
123
- @addr.add_points "190"
124
- @addr.add_points "10002"
125
- @addr.add_points ["10","0","190","100"]
126
- @addr.points.length.must_equal(8)
117
+ lambda {@node.add_points "X"}.must_raise(ArgumentError)
118
+ @node.add_points "10"
119
+ @node.points.length.must_equal(1)
120
+ @node.add_points "0"
121
+ @node.points.length.must_equal(2)
122
+ @node.add_points "190"
123
+ @node.add_points "10002"
124
+ @node.add_points ["10","0","190","100"]
125
+ @node.points.length.must_equal(8)
127
126
  end
128
127
 
129
128
  it "does bulk add points" do
130
129
  dt = Time.now.to_f
131
130
  Chawk.bulk_add_points(@agent, {"xxx"=>[1,2,3,4,5,6], "yyy"=>[[10,dt],[10,dt]], "zzz"=>[{"t"=>dt,"v"=>10},{"v"=>0,"t"=>dt}]})
132
131
 
133
- Chawk.addr(@agent,"xxx").points.length.must_equal 6
134
- Chawk.addr(@agent,"zzz").points.length.must_equal 2
135
- Chawk.addr(@agent,"zzz").points.last.value.must_equal 0
132
+ Chawk.node(@agent,"xxx").points.length.must_equal 6
133
+ Chawk.node(@agent,"zzz").points.length.must_equal 2
134
+ Chawk.node(@agent,"zzz").points.last.value.must_equal 0
136
135
 
137
136
  end
138
137
 
139
138
  it "has last()" do
140
- @addr.points.must_respond_to(:last)
139
+ @node.points.must_respond_to(:last)
141
140
  end
142
141
 
143
142
  it "remembers last value" do
144
- @addr.add_points 10
145
- @addr.points.last.value.must_equal(10)
146
- @addr.add_points 1000
147
- @addr.points.last.value.must_equal(1000)
148
- @addr.add_points 99
149
- @addr.points.last.value.must_equal(99)
150
- @addr.add_points [10,0,190,100]
151
- @addr.points.last.value.must_equal(100)
143
+ @node.add_points 10
144
+ @node.points.last.value.must_equal(10)
145
+ @node.add_points 1000
146
+ @node.points.last.value.must_equal(1000)
147
+ @node.add_points 99
148
+ @node.points.last.value.must_equal(99)
149
+ @node.add_points [10,0,190,100]
150
+ @node.points.last.value.must_equal(100)
152
151
  end
153
152
 
154
153
  it "stores meta information" do
155
154
 
156
155
  metadata = {"info"=>"this is a test"}
157
- @addr.add_points([1,2,3,4],{:meta=>metadata})
158
- @addr.points.last.value.must_equal(4)
159
- JSON.parse(@addr.points.last.meta).must_equal(metadata)
156
+ @node.add_points([1,2,3,4],{:meta=>metadata})
157
+ @node.points.last.value.must_equal(4)
158
+ meta = @node.points.last.meta
159
+ JSON.parse(meta).must_equal(metadata)
160
160
 
161
161
  metadata = {"number"=>123}
162
- @addr.add_points([1,2,3,4],{:meta=>metadata})
163
- @addr.points.last.value.must_equal(4)
164
- JSON.parse(@addr.points.last.meta).must_equal(metadata)
162
+ @node.add_points([1,2,3,4],{:meta=>metadata})
163
+ @node.points.last.value.must_equal(4)
164
+ JSON.parse(@node.points.last.meta).must_equal(metadata)
165
+
166
+ metadata = ["completely wrong"]
167
+ lambda {@node.add_points([1,2,3,4],{:meta=>metadata})}.must_raise(ArgumentError)
165
168
 
166
169
  end
167
170
 
168
171
 
169
172
  it "returns ordinal last" do
170
- @addr.add_points [10,9,8,7,6,5,4,3,2,1,0]
171
- @addr.points.last(5).length.must_equal(5)
173
+ @node.add_points [10,9,8,7,6,5,4,3,2,1,0]
174
+ @node.points.last(5).length.must_equal(5)
172
175
  end
173
176
 
174
- it "has max()" do
175
- @addr.must_respond_to(:max)
176
- end
177
+ # it "has max()" do
178
+ # @node.must_respond_to(:max)
179
+ # end
177
180
 
178
- it "does max()" do
179
- @addr.points.destroy_all
180
- @addr.add_points [1,2,3,4,5]
181
- @addr.max.must_equal(5)
182
- @addr.add_points 100
183
- @addr.max.must_equal(100)
184
- @addr.add_points 100
185
- @addr.max.must_equal(100)
186
- @addr.add_points 99
187
- @addr.max.must_equal(100)
188
- @addr.add_points 0
189
- @addr.max.must_equal(100)
190
- end
181
+ # it "does max()" do
182
+ # @node.clear_points!
183
+ # @node.add_points [1,2,3,4,5]
184
+ # @node.max.must_equal(5)
185
+ # @node.add_points 100
186
+ # @node.max.must_equal(100)
187
+ # @node.add_points 100
188
+ # @node.max.must_equal(100)
189
+ # @node.add_points 99
190
+ # @node.max.must_equal(100)
191
+ # @node.add_points 0
192
+ # @node.max.must_equal(100)
193
+ # end
191
194
 
192
- it "does min()" do
193
- @addr.add_points [11,12,13,14,15]
194
- @addr.min.must_equal(11)
195
- @addr.add_points 100
196
- @addr.min.must_equal(11)
197
- @addr.add_points 10
198
- @addr.min.must_equal(10)
199
- @addr.add_points 99
200
- @addr.min.must_equal(10)
201
- @addr.add_points 0
202
- @addr.min.must_equal(0)
203
- end
195
+ # it "does min()" do
196
+ # @node.add_points [11,12,13,14,15]
197
+ # @node.min.must_equal(11)
198
+ # @node.add_points 100
199
+ # @node.min.must_equal(11)
200
+ # @node.add_points 10
201
+ # @node.min.must_equal(10)
202
+ # @node.add_points 99
203
+ # @node.min.must_equal(10)
204
+ # @node.add_points 0
205
+ # @node.min.must_equal(0)
206
+ # end
204
207
 
205
208
  it :does_range do
206
- @addr.must_respond_to(:points_range)
209
+ @node.must_respond_to(:points_range)
207
210
 
208
211
  ts = Time.now
209
212
 
210
- @addr._insert_point(0,ts-1000)
211
- @addr._insert_point(1,ts-1000)
212
- @addr._insert_point(2,ts-1000)
213
- @addr._insert_point(3,ts-1000)
214
- @addr._insert_point(4,ts-1000)
215
- @addr._insert_point(5,ts-800)
216
- @addr._insert_point(6,ts-800)
217
- @addr._insert_point(7,ts-800)
218
- @addr._insert_point(8,ts-200)
219
- @addr._insert_point(9,ts-10)
220
- @addr._insert_point(10,ts-5)
221
- @addr.points_range(ts-1001,ts).length.must_equal 11
222
- @addr.points_range(ts-801,ts).length.must_equal 6
223
- @addr.points_range(ts-201,ts).length.must_equal 3
224
- @addr.points_range(ts-11,ts).length.must_equal 2
225
- @addr.points_range(ts-6,ts).length.must_equal 1
226
- @addr.points_range(ts-201,ts-11).length.must_equal 1
227
- @addr.points_range(ts-1001,ts-999).length.must_equal 5
228
-
229
- @addr._insert_point(0,ts-101)
230
- @addr.points_range(ts-201,ts).length.must_equal 4
213
+ @node._insert_point(0,ts-1000)
214
+ @node._insert_point(1,ts-1000)
215
+ @node._insert_point(2,ts-1000)
216
+ @node._insert_point(3,ts-1000)
217
+ @node._insert_point(4,ts-1000)
218
+ @node._insert_point(5,ts-800)
219
+ @node._insert_point(6,ts-800)
220
+ @node._insert_point(7,ts-800)
221
+ @node._insert_point(8,ts-200)
222
+ @node._insert_point(9,ts-10)
223
+ @node._insert_point(10,ts-5)
224
+ @node.points_range(ts-1001,ts).length.must_equal 11
225
+ @node.points_range(ts-801,ts).length.must_equal 6
226
+ @node.points_range(ts-201,ts).length.must_equal 3
227
+ @node.points_range(ts-11,ts).length.must_equal 2
228
+ @node.points_range(ts-6,ts).length.must_equal 1
229
+ @node.points_range(ts-201,ts-11).length.must_equal 1
230
+ @node.points_range(ts-1001,ts-999).length.must_equal 5
231
+
232
+ @node._insert_point(0,ts-101)
233
+ @node.points_range(ts-201,ts).length.must_equal 4
231
234
  end
232
235
 
233
236
  it "does since" do
234
- @addr.points.destroy_all
237
+ @node.clear_points!
235
238
 
236
239
 
237
240
  ts = Time.now
238
241
 
239
- @addr._insert_point(0,ts-1000)
240
- @addr._insert_point(7,ts-800)
241
- @addr._insert_point(8,ts-200)
242
- @addr._insert_point(10,ts-5)
243
- @addr.must_respond_to(:points_since)
244
- @addr.points_since(ts-1001).length.must_equal(4)
245
- @addr.points_since(ts-301).length.must_equal(2)
242
+ @node._insert_point(0,ts-1000)
243
+ @node._insert_point(7,ts-800)
244
+ @node._insert_point(8,ts-200)
245
+ @node._insert_point(10,ts-5)
246
+ @node.must_respond_to(:points_since)
247
+ @node.points_since(ts-1001).length.must_equal(4)
248
+ @node.points_since(ts-301).length.must_equal(2)
246
249
  end
247
250
 
248
- # it :acts_like_an_integer do
249
- # @addr.add_points 36878
250
- # last = @addr.points.last
251
- # last.to_i.must_equal 36878
252
- # end
253
-
254
- # it "does mq" do
255
- # @board.flush_notification_queue
256
- # @board.notification_queue_length.must_equal (0)
257
-
258
- # pointers = []
259
- # pointers add_points @board.paddr(['0','1','2'])
260
- # pointers add_points @board.paddr(['0','1','3'])
261
- # pointers add_points @board.paddr(['0','1','4'])
262
- # pointers add_points @board.paddr(['0','1','5'])
263
-
264
- # pointers.each{|p|padd_points10}
265
-
266
- # @board.notification_queue_length.must_equal (4)
267
- # x = @board.pop_from_notification_queue
268
- # x.length.must_equal(3)
269
- # @board.notification_queue_length.must_equal (3)
270
- # x = @board.pop_from_notification_queue
271
- # x = @board.pop_from_notification_queue
272
- # @board.notification_queue_length.must_equal (1)
273
- # x.length.must_equal(3)
274
- # end
275
251
  end
@@ -0,0 +1,61 @@
1
+ require 'test_helper'
2
+
3
+ describe Chawk do
4
+ before do
5
+ Chawk.clear_all_data!
6
+ @agent = Chawk::Models::Agent.first || Chawk::Models::Agent.create(:name=>"Test User")
7
+ end
8
+
9
+ it "obeys the order" do
10
+ node1 = Chawk.node(@agent,'a:b')
11
+ Chawk::Models::Range.new(start_ts:1140.0,stop_ts:1085.0,beats:1,parent_node:node1).save.must_equal false
12
+ Chawk::Models::Range.new(start_ts:1140.0,stop_ts:1140.0,beats:1,parent_node:node1).save.must_equal false
13
+ Chawk::Models::Range.new(start_ts:1000.0,stop_ts:1140.0,beats:1,parent_node:node1).save.must_equal true
14
+ end
15
+
16
+ it "calculates range" do
17
+ node1 = Chawk.node(@agent,'a:b')
18
+ node1.clear_points!
19
+
20
+ range = Chawk::Models::Range.create(start_ts:1085.0,stop_ts:1140.0,beats:1,parent_node:node1)
21
+ ag = Chawk::Models::NodeAggregator.new(range.data_node)
22
+ ag.sum.must_equal(0)
23
+ ag.mean.round(2).must_equal(0.0)
24
+
25
+
26
+ node1._insert_point(92,1085.2340175364745)
27
+ node1._insert_point(94,1100.0643872093362)
28
+ node1._insert_point(92,1102.3558603493182)
29
+ node1._insert_point(94,1119.2568446818536)
30
+ node1._insert_point(91,1125.283357089852)
31
+ node1._insert_point(91,1131.9783343810343)
32
+ node1._insert_point(88,1132.299788479544)
33
+
34
+ range = Chawk::Models::Range.create(start_ts:1085.0,stop_ts:1140.0,beats:1,parent_node:node1)
35
+
36
+ range.data_node.points.length.must_equal(220)
37
+ range.data_node.points[25].value.must_equal(92)
38
+ range.data_node.points[140].value.must_equal(94)
39
+ ag = Chawk::Models::NodeAggregator.new(range.data_node)
40
+ ag.sum.must_equal(20066)
41
+ ag.mean.round(2).must_equal(91.21)
42
+
43
+ node1.add_points [{'v'=>1500, 't'=>1135.0}] #invalidate range and rebuild
44
+
45
+ range.reload
46
+ range.data_node.points[200].value.must_equal(1500)
47
+ ag = Chawk::Models::NodeAggregator.new(range.data_node)
48
+ ag.sum.must_equal(48306)
49
+ ag.max.must_equal(1500)
50
+ ag.min.must_equal(0)
51
+
52
+ range = Chawk::Models::Range.create(start_ts:1088.0,stop_ts:8100.0,beats:14400,parent_node:node1)
53
+ range.data_node.points.length.must_equal(2)
54
+
55
+ ag = Chawk::Models::NodeAggregator.new(range.data_node)
56
+ ag.sum.must_equal(1592)
57
+ ag.mean.must_equal(796)
58
+ ag.stdev.round(2).must_equal(995.61)
59
+
60
+ end
61
+ end