chawk 0.1.10
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.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +148 -0
- data/Rakefile +9 -0
- data/chawk.gemspec +35 -0
- data/lib/addr.rb +89 -0
- data/lib/chawk.rb +46 -0
- data/lib/chawk/version.rb +3 -0
- data/lib/models.rb +87 -0
- data/lib/paddr.rb +45 -0
- data/lib/quantizer.rb +36 -0
- data/lib/store.rb +71 -0
- data/lib/vaddr.rb +21 -0
- data/test/lib/addr_test.rb +49 -0
- data/test/lib/paddr_test.rb +212 -0
- data/test/lib/quantizer_test.rb +63 -0
- data/test/lib/vaddr_test.rb +160 -0
- data/test/test_helper.rb +33 -0
- metadata +237 -0
data/lib/paddr.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'store'
|
2
|
+
|
3
|
+
module Chawk
|
4
|
+
class Paddr
|
5
|
+
|
6
|
+
include Chawk::Store
|
7
|
+
|
8
|
+
def model
|
9
|
+
Chawk::Models::Point
|
10
|
+
end
|
11
|
+
|
12
|
+
def coll
|
13
|
+
@node.points
|
14
|
+
end
|
15
|
+
|
16
|
+
def stored_type
|
17
|
+
Integer
|
18
|
+
end
|
19
|
+
|
20
|
+
def +(other = 1)
|
21
|
+
raise ArgumentError unless other.is_a?(Numeric) && other.integer?
|
22
|
+
int = (self.last.value.to_i + other.to_i)
|
23
|
+
self << int
|
24
|
+
end
|
25
|
+
|
26
|
+
def -(other = 1)
|
27
|
+
raise ArgumentError unless other.is_a?(Numeric) && other.integer?
|
28
|
+
self + (-other)
|
29
|
+
end
|
30
|
+
|
31
|
+
def length
|
32
|
+
coll.length
|
33
|
+
end
|
34
|
+
|
35
|
+
def max
|
36
|
+
coll.max(:value)
|
37
|
+
end
|
38
|
+
|
39
|
+
def min
|
40
|
+
coll.min(:value)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/lib/quantizer.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Chawk
|
2
|
+
module Quantizer
|
3
|
+
def starting_step(x,step_width)
|
4
|
+
if (x % step_width) == 0
|
5
|
+
step = x
|
6
|
+
else
|
7
|
+
step = x - (x % step_width) + step_width
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def ending_step(x,step_width)
|
12
|
+
if (x % step_width) == 0
|
13
|
+
step = x
|
14
|
+
else
|
15
|
+
step = x - (x % step_width)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def quantize(ary,step_width,steps=nil)
|
20
|
+
# TODO: Needs lots more testing, then faster implementation
|
21
|
+
# with caching (probaby at data add point)
|
22
|
+
#puts "#{ary.length}"
|
23
|
+
step = starting_step(ary[0][1],step_width)
|
24
|
+
end_step = ending_step(ary[-1][1], step_width)
|
25
|
+
out = [ary[0]]
|
26
|
+
while (step > end_step) do
|
27
|
+
step -= step_width
|
28
|
+
next_step = step - step_width
|
29
|
+
data = ary[0]
|
30
|
+
data = ary.reverse.detect{|a|a[1] > next_step} || data#(ary[-1])
|
31
|
+
out << [data[0],step]
|
32
|
+
end
|
33
|
+
out
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/store.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
module Chawk
|
2
|
+
module Store
|
3
|
+
|
4
|
+
def initialize(addr)
|
5
|
+
@addr = addr
|
6
|
+
@node = addr.node
|
7
|
+
end
|
8
|
+
|
9
|
+
def _insert(val,ts,options={})
|
10
|
+
#DataMapper.logger.debug "PREINSERT #{val} -- #{ts.to_f}"
|
11
|
+
|
12
|
+
values = {value:val,observed_at:ts.to_f,agent:@addr.agent}
|
13
|
+
values[:meta] = options[:meta] if options[:meta]
|
14
|
+
|
15
|
+
coll.create(values)
|
16
|
+
end
|
17
|
+
|
18
|
+
def clear_history!
|
19
|
+
model.all(node_id:@node.id).destroy
|
20
|
+
end
|
21
|
+
|
22
|
+
def length
|
23
|
+
coll.length
|
24
|
+
end
|
25
|
+
|
26
|
+
def <<(args,options={})
|
27
|
+
dt = Time.now
|
28
|
+
if args.is_a?(Array)
|
29
|
+
args.each do |arg|
|
30
|
+
if arg.is_a?(stored_type)
|
31
|
+
self._insert(arg,dt,options)
|
32
|
+
else
|
33
|
+
raise ArgumentError
|
34
|
+
end
|
35
|
+
end
|
36
|
+
else
|
37
|
+
if args.is_a?(stored_type)
|
38
|
+
_insert(args,dt,options)
|
39
|
+
else
|
40
|
+
raise ArgumentError
|
41
|
+
end
|
42
|
+
end
|
43
|
+
self.last
|
44
|
+
end
|
45
|
+
|
46
|
+
def last(count=1)
|
47
|
+
if count == 1
|
48
|
+
if coll.length > 0
|
49
|
+
coll.last
|
50
|
+
#PPoint.new(self, coll.last)
|
51
|
+
else
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
else
|
55
|
+
vals = coll.all(limit:count,order:[:observed_at.asc, :id.asc])
|
56
|
+
#last_items = vals #.each.collect{|val|PPoint.new(self, val)}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def range(dt_from, dt_to,options={})
|
61
|
+
vals = coll.all(:observed_at.gte => dt_from, :observed_at.lte =>dt_to, limit:1000,order:[:observed_at.asc, :id.asc])
|
62
|
+
return vals
|
63
|
+
end
|
64
|
+
|
65
|
+
def since(dt_from)
|
66
|
+
self.range(dt_from,Time.now)
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
data/lib/vaddr.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
describe Chawk::Addr do
|
5
|
+
before do
|
6
|
+
Chawk.clear_all_data!
|
7
|
+
@agent = Chawk::Models::Agent.first || Chawk::Models::Agent.create(:name=>"Test User")
|
8
|
+
@addr = Chawk.addr(@agent,'a/b')
|
9
|
+
end
|
10
|
+
|
11
|
+
it "has path" do
|
12
|
+
@addr.must_respond_to(:path)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "has address" do
|
16
|
+
@addr.must_respond_to(:address)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "address is a/b" do
|
20
|
+
@addr.address.must_equal("a/b")
|
21
|
+
Chawk.addr(@agent,'a/b').address.must_equal("a/b")
|
22
|
+
Chawk.addr(@agent,'0/x/z').address.must_equal("0/x/z")
|
23
|
+
end
|
24
|
+
|
25
|
+
it "rejects invalid paths" do
|
26
|
+
lambda {Chawk.addr(@agent,['A'])}.must_raise(ArgumentError)
|
27
|
+
lambda {Chawk.addr(@agent,0)}.must_raise(ArgumentError)
|
28
|
+
lambda {Chawk.addr(@agent,:a)}.must_raise(ArgumentError)
|
29
|
+
lambda {Chawk.addr(@agent,Object.new)}.must_raise(ArgumentError)
|
30
|
+
lambda {Chawk.addr(@agent,String.new)}.must_raise(ArgumentError)
|
31
|
+
lambda {Chawk.addr(@agent,"")}.must_raise(ArgumentError)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "stops unauthorized access" do
|
35
|
+
agent2 = Chawk::Models::Agent.first(name:"Steve Austin") || Chawk::Models::Agent.create(name:"Steve Austin")
|
36
|
+
lambda{@addr = Chawk.addr(agent2,'a/b')}.must_raise SecurityError
|
37
|
+
@addr.public_read=true
|
38
|
+
Chawk.addr(agent2,'a/b').address.must_equal "a/b"
|
39
|
+
@addr.public_read=false
|
40
|
+
lambda{@addr = Chawk.addr(agent2,'a/b')}.must_raise SecurityError
|
41
|
+
@addr.set_permissions(agent2,true,false,false)
|
42
|
+
Chawk.addr(agent2,'a/b').address.must_equal "a/b"
|
43
|
+
@addr.set_permissions(agent2,false,false,false)
|
44
|
+
lambda{@addr = Chawk.addr(agent2,'a/b')}.must_raise SecurityError
|
45
|
+
@addr.set_permissions(agent2,false,false,true)
|
46
|
+
Chawk.addr(agent2,'a/b').address.must_equal "a/b"
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,212 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe Chawk::Paddr do
|
4
|
+
before do
|
5
|
+
#@board = Chawk::Board.new()
|
6
|
+
@agent = Chawk::Models::Agent.first || Chawk::Models::Agent.create(:name=>"Test User")
|
7
|
+
@addr = Chawk.addr(@agent,'a/b')
|
8
|
+
@addr.points.clear_history!
|
9
|
+
end
|
10
|
+
|
11
|
+
it "has length" do
|
12
|
+
@addr.points.must_respond_to(:length)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "calculates length" do
|
16
|
+
@addr.points.clear_history!
|
17
|
+
@addr.points.length.must_equal(0)
|
18
|
+
@addr.points << 2
|
19
|
+
@addr.points.length.must_equal(1)
|
20
|
+
@addr.points << 2
|
21
|
+
@addr.points.length.must_equal(2)
|
22
|
+
@addr.points << 2
|
23
|
+
@addr.points.length.must_equal(3)
|
24
|
+
@addr.points << [1,2,3,4]
|
25
|
+
@addr.points.length.must_equal(7)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "has clear_history!" do
|
29
|
+
@addr.points.must_respond_to(:clear_history!)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "clears history" do
|
33
|
+
@addr.points.clear_history!
|
34
|
+
@addr.points.length.must_equal(0)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "accepts _insert" do
|
38
|
+
@addr.points._insert(100,Time.now)
|
39
|
+
@addr.points.length.must_equal(1)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "accepts <<" do
|
43
|
+
@addr.points.must_respond_to(:"<<")
|
44
|
+
end
|
45
|
+
|
46
|
+
it "accepts integers" do
|
47
|
+
@addr.points << 10
|
48
|
+
@addr.points.length.must_equal(1)
|
49
|
+
@addr.points << 0
|
50
|
+
@addr.points.length.must_equal(2)
|
51
|
+
@addr.points << 190
|
52
|
+
@addr.points << 10002
|
53
|
+
@addr.points << [10,0,190,100]
|
54
|
+
end
|
55
|
+
|
56
|
+
it "does +" do
|
57
|
+
@addr.points.must_respond_to(:"+")
|
58
|
+
@addr.points << 10
|
59
|
+
@addr.points + 100
|
60
|
+
@addr.points.last.value.must_equal(110)
|
61
|
+
@addr.points + -10
|
62
|
+
@addr.points.last.value.must_equal(100)
|
63
|
+
@addr.points.+
|
64
|
+
@addr.points.last.value.must_equal(101)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should only + integers" do
|
68
|
+
lambda {@addr.points + 'A'}.must_raise(ArgumentError)
|
69
|
+
lambda {@addr.points + nil}.must_raise(ArgumentError)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "does -" do
|
73
|
+
@addr.points.must_respond_to(:"-")
|
74
|
+
@addr.points << 10
|
75
|
+
@addr.points - 100
|
76
|
+
@addr.points.last.value.must_equal(-90)
|
77
|
+
@addr.points - -10
|
78
|
+
@addr.points.last.value.must_equal(-80)
|
79
|
+
@addr.points.-
|
80
|
+
@addr.points.last.value.must_equal(-81)
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should only - integers" do
|
84
|
+
lambda {@addr.points - 'A'}.must_raise(ArgumentError)
|
85
|
+
lambda {@addr.points - nil}.must_raise(ArgumentError)
|
86
|
+
end
|
87
|
+
|
88
|
+
it "only accepts integers" do
|
89
|
+
lambda {@addr.points << 10.0}.must_raise(ArgumentError)
|
90
|
+
lambda {@addr.points << nil}.must_raise(ArgumentError)
|
91
|
+
lambda {@addr.points << [10.0,:x]}.must_raise(ArgumentError)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "has last()" do
|
95
|
+
@addr.points.must_respond_to(:last)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "remembers last value" do
|
99
|
+
@addr.points << 10
|
100
|
+
@addr.points.last.value.must_equal(10)
|
101
|
+
@addr.points << 1000
|
102
|
+
@addr.points.last.value.must_equal(1000)
|
103
|
+
@addr.points << 99
|
104
|
+
@addr.points.last.value.must_equal(99)
|
105
|
+
@addr.points << [10,0,190,100]
|
106
|
+
@addr.points.last.value.must_equal(100)
|
107
|
+
end
|
108
|
+
|
109
|
+
it "returns ordinal last" do
|
110
|
+
@addr.points << [10,9,8,7,6,5,4,3,2,1,0]
|
111
|
+
@addr.points.last(5).length.must_equal(5)
|
112
|
+
end
|
113
|
+
|
114
|
+
it "has max()" do
|
115
|
+
@addr.points.must_respond_to(:max)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "does max()" do
|
119
|
+
@addr.points.clear_history!
|
120
|
+
@addr.points << [1,2,3,4,5]
|
121
|
+
@addr.points.max.must_equal(5)
|
122
|
+
@addr.points << 100
|
123
|
+
@addr.points.max.must_equal(100)
|
124
|
+
@addr.points << 100
|
125
|
+
@addr.points.max.must_equal(100)
|
126
|
+
@addr.points << 99
|
127
|
+
@addr.points.max.must_equal(100)
|
128
|
+
@addr.points << 0
|
129
|
+
@addr.points.max.must_equal(100)
|
130
|
+
end
|
131
|
+
|
132
|
+
it "does min()" do
|
133
|
+
@addr.points << [11,12,13,14,15]
|
134
|
+
@addr.points.min.must_equal(11)
|
135
|
+
@addr.points << 100
|
136
|
+
@addr.points.min.must_equal(11)
|
137
|
+
@addr.points << 10
|
138
|
+
@addr.points.min.must_equal(10)
|
139
|
+
@addr.points << 99
|
140
|
+
@addr.points.min.must_equal(10)
|
141
|
+
@addr.points << 0
|
142
|
+
@addr.points.min.must_equal(0)
|
143
|
+
end
|
144
|
+
|
145
|
+
it :does_range do
|
146
|
+
@addr.points.must_respond_to(:range)
|
147
|
+
|
148
|
+
ts = Time.now
|
149
|
+
|
150
|
+
@addr.points._insert(0,ts-1000)
|
151
|
+
@addr.points._insert(1,ts-1000)
|
152
|
+
@addr.points._insert(2,ts-1000)
|
153
|
+
@addr.points._insert(3,ts-1000)
|
154
|
+
@addr.points._insert(4,ts-1000)
|
155
|
+
@addr.points._insert(5,ts-800)
|
156
|
+
@addr.points._insert(6,ts-800)
|
157
|
+
@addr.points._insert(7,ts-800)
|
158
|
+
@addr.points._insert(8,ts-200)
|
159
|
+
@addr.points._insert(9,ts-10)
|
160
|
+
@addr.points._insert(10,ts-5)
|
161
|
+
@addr.points.range(ts-1000,ts).length.must_equal 11
|
162
|
+
@addr.points.range(ts-800,ts).length.must_equal 6
|
163
|
+
@addr.points.range(ts-200,ts).length.must_equal 3
|
164
|
+
@addr.points.range(ts-10,ts).length.must_equal 2
|
165
|
+
@addr.points.range(ts-5,ts).length.must_equal 1
|
166
|
+
@addr.points.range(ts-200,ts-11).length.must_equal 1
|
167
|
+
@addr.points.range(ts-1000,ts-1000).length.must_equal 5
|
168
|
+
|
169
|
+
@addr.points._insert(0,ts-100)
|
170
|
+
@addr.points.range(ts-200,ts).length.must_equal 4
|
171
|
+
end
|
172
|
+
|
173
|
+
it "does since" do
|
174
|
+
ts = Time.now
|
175
|
+
|
176
|
+
@addr.points._insert(0,ts-1000)
|
177
|
+
@addr.points._insert(7,ts-800)
|
178
|
+
@addr.points._insert(8,ts-200)
|
179
|
+
@addr.points._insert(10,ts-5)
|
180
|
+
@addr.points.must_respond_to(:since)
|
181
|
+
@addr.points.since(ts-1000).length.must_equal(4)
|
182
|
+
@addr.points.since(ts-300).length.must_equal(2)
|
183
|
+
end
|
184
|
+
|
185
|
+
# it :acts_like_an_integer do
|
186
|
+
# @addr.points << 36878
|
187
|
+
# last = @addr.points.last
|
188
|
+
# last.to_i.must_equal 36878
|
189
|
+
# end
|
190
|
+
|
191
|
+
# it "does mq" do
|
192
|
+
# @board.flush_notification_queue
|
193
|
+
# @board.notification_queue_length.must_equal (0)
|
194
|
+
|
195
|
+
# pointers = []
|
196
|
+
# pointers << @board.paddr(['0','1','2'])
|
197
|
+
# pointers << @board.paddr(['0','1','3'])
|
198
|
+
# pointers << @board.paddr(['0','1','4'])
|
199
|
+
# pointers << @board.paddr(['0','1','5'])
|
200
|
+
|
201
|
+
# pointers.each{|p|p<<10}
|
202
|
+
|
203
|
+
# @board.notification_queue_length.must_equal (4)
|
204
|
+
# x = @board.pop_from_notification_queue
|
205
|
+
# x.length.must_equal(3)
|
206
|
+
# @board.notification_queue_length.must_equal (3)
|
207
|
+
# x = @board.pop_from_notification_queue
|
208
|
+
# x = @board.pop_from_notification_queue
|
209
|
+
# @board.notification_queue_length.must_equal (1)
|
210
|
+
# x.length.must_equal(3)
|
211
|
+
# end
|
212
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
#require 'minitest'
|
2
|
+
#require 'minitest/autorun'
|
3
|
+
#require 'chawk'
|
4
|
+
require 'test_helper'
|
5
|
+
|
6
|
+
|
7
|
+
ts1 = [['A',100],['B',99],['C',95],['D',50],['E',45],['F',30],['G',29],['H',28]]
|
8
|
+
ts2 = [['A',100],['B',85],['C',80],['D',71],['E',70],['F',30],['G',29],['H',28]]
|
9
|
+
|
10
|
+
class Qharness
|
11
|
+
include Chawk::Quantizer
|
12
|
+
attr_accessor :datum
|
13
|
+
def initialize()
|
14
|
+
@datum = datum
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe Chawk::Quantizer do
|
19
|
+
before do
|
20
|
+
@qh = Qharness.new()
|
21
|
+
end
|
22
|
+
|
23
|
+
it "does quantize" do
|
24
|
+
@qh.must_respond_to "quantize"
|
25
|
+
@qh.quantize(ts1,10).must_equal (
|
26
|
+
[["A", 100], ["C", 90], ["C", 80], ["C", 70], ["C", 60], ["E", 50], ["E", 40], ["H", 30], ["H", 20]]
|
27
|
+
)
|
28
|
+
@qh.quantize(ts2,10).must_equal(
|
29
|
+
[["A", 100], ["B", 90], ["D", 80], ["E", 70], ["E", 60], ["E", 50], ["E", 40], ["H", 30], ["H", 20]]
|
30
|
+
)
|
31
|
+
@qh.quantize(ts2,5).must_equal(
|
32
|
+
[["A", 100], ["A", 95], ["A", 90], ["B", 85], ["C", 80], ["D", 75], ["E", 70], ["E", 65], ["E", 60], ["E", 55], ["E", 50], ["E", 45], ["E", 40], ["E", 35], ["H", 30], ["H", 25]] )
|
33
|
+
end
|
34
|
+
|
35
|
+
it "picks first step" do
|
36
|
+
@qh.starting_step(101,10).must_equal(110)
|
37
|
+
@qh.starting_step(111,10).must_equal(120)
|
38
|
+
@qh.starting_step(100,10).must_equal(100)
|
39
|
+
@qh.starting_step(99,10).must_equal(100)
|
40
|
+
@qh.starting_step(92,10).must_equal(100)
|
41
|
+
@qh.starting_step(91,10).must_equal(100)
|
42
|
+
@qh.starting_step(90,10).must_equal(90)
|
43
|
+
@qh.starting_step(1,10).must_equal(10)
|
44
|
+
@qh.starting_step(3,10).must_equal(10)
|
45
|
+
@qh.starting_step(7,10).must_equal(10)
|
46
|
+
@qh.starting_step(10,10).must_equal(10)
|
47
|
+
@qh.starting_step(101,5).must_equal(105)
|
48
|
+
@qh.starting_step(102,5).must_equal(105)
|
49
|
+
@qh.starting_step(105,5).must_equal(105)
|
50
|
+
@qh.starting_step(107,5).must_equal(110)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "picks last step" do
|
54
|
+
@qh.ending_step(101,10).must_equal(100)
|
55
|
+
@qh.ending_step(111,10).must_equal(110)
|
56
|
+
@qh.ending_step(100,10).must_equal(100)
|
57
|
+
@qh.ending_step(101,5).must_equal(100)
|
58
|
+
@qh.ending_step(106,5).must_equal(105)
|
59
|
+
@qh.ending_step(100,5).must_equal(100)
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
end
|