nightfury 0.1.1 → 0.2.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.
data/lib/nightfury/identity.rb
CHANGED
@@ -10,7 +10,8 @@ module Nightfury
|
|
10
10
|
|
11
11
|
class << self
|
12
12
|
|
13
|
-
attr_reader :metrics
|
13
|
+
attr_reader :metrics, :tags
|
14
|
+
attr_accessor :store_as
|
14
15
|
|
15
16
|
def name
|
16
17
|
self.to_s.demodulize.underscore
|
@@ -26,17 +27,50 @@ module Nightfury
|
|
26
27
|
end
|
27
28
|
ENDOFMETHOD
|
28
29
|
end
|
29
|
-
|
30
|
+
|
31
|
+
def tag(name, options={})
|
32
|
+
@tags ||= {}
|
33
|
+
@tags[name] = options[:store_as] ? options[:store_as] : name
|
34
|
+
end
|
30
35
|
end
|
31
36
|
|
32
|
-
attr_accessor :id
|
37
|
+
attr_accessor :id, :tags
|
33
38
|
|
34
|
-
def initialize(id)
|
39
|
+
def initialize(id, options={})
|
35
40
|
@id = id
|
41
|
+
@tags = options[:tags]
|
36
42
|
end
|
37
43
|
|
38
44
|
def key_prefix
|
39
|
-
|
45
|
+
store_name = self.class.store_as ? self.class.store_as : self.class.name
|
46
|
+
tag_ids = generate_tag_ids
|
47
|
+
tag_ids = tag_ids.nil? ? '' : ":#{tag_ids}"
|
48
|
+
"#{store_name}.#{id}#{tag_ids}"
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def generate_tag_ids
|
54
|
+
return nil unless tags
|
55
|
+
tag_values = {}
|
56
|
+
tags.each do |key, value|
|
57
|
+
store_name = self.class.tags[key]
|
58
|
+
next if store_name.nil?
|
59
|
+
tag_values[store_name] = value
|
60
|
+
end
|
61
|
+
|
62
|
+
tag_ids = nil
|
63
|
+
tag_values_sorted = tag_values.keys.sort
|
64
|
+
tag_values_sorted.each do |store_name|
|
65
|
+
tag = "#{store_name}.#{tag_values[store_name]}"
|
66
|
+
if tag_ids.nil?
|
67
|
+
tag_ids = tag
|
68
|
+
else
|
69
|
+
tag_ids = "#{tag_ids}:#{tag}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
tag_ids
|
40
74
|
end
|
41
75
|
end
|
42
76
|
end
|
data/lib/nightfury/metric.rb
CHANGED
@@ -1,17 +1,19 @@
|
|
1
1
|
module Nightfury
|
2
2
|
module Metric
|
3
3
|
class Base
|
4
|
-
attr_reader :name, :redis, :redis_key_prefix
|
4
|
+
attr_reader :name, :redis, :redis_key_prefix, :store_as
|
5
5
|
|
6
6
|
def initialize(name, options={})
|
7
7
|
@name = name
|
8
8
|
@redis = Nightfury.redis
|
9
9
|
@redis_key_prefix = options[:redis_key_prefix]
|
10
|
+
@store_as = options[:store_as]
|
10
11
|
end
|
11
12
|
|
12
13
|
def redis_key
|
13
14
|
prefix = redis_key_prefix.blank? ? '' : "#{redis_key_prefix}:"
|
14
|
-
|
15
|
+
store_name = store_as ? store_as : name
|
16
|
+
"#{prefix}#{store_name}"
|
15
17
|
end
|
16
18
|
|
17
19
|
def delete
|
data/lib/nightfury/version.rb
CHANGED
@@ -21,6 +21,18 @@ describe Nightfury::Identity::Base do
|
|
21
21
|
Dummy.instance_methods.should include(:another_count)
|
22
22
|
end
|
23
23
|
end
|
24
|
+
|
25
|
+
describe "tags" do
|
26
|
+
it "should add to tags" do
|
27
|
+
Dummy.tag(:label_id)
|
28
|
+
Dummy.tags[:label_id].should == :label_id
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should add name and store_as to tags" do
|
32
|
+
Dummy.tag(:label_id, store_as: :l)
|
33
|
+
Dummy.tags[:label_id].should == :l
|
34
|
+
end
|
35
|
+
end
|
24
36
|
end
|
25
37
|
|
26
38
|
describe "InstanceMethods" do
|
@@ -31,7 +43,22 @@ describe Nightfury::Identity::Base do
|
|
31
43
|
|
32
44
|
it "should generates a key prefix" do
|
33
45
|
d = Dummy.new(1)
|
34
|
-
d.key_prefix.should == 'dummy
|
46
|
+
d.key_prefix.should == 'dummy.1'
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should use store_as to generate key prefix if provided" do
|
50
|
+
d = Dummy.new(1)
|
51
|
+
flexmock(Dummy).should_receive(:store_as => :d)
|
52
|
+
d.key_prefix.should == 'd.1'
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should include tags in the key prefix" do
|
56
|
+
DummyTwo = Class.new(Dummy)
|
57
|
+
DummyTwo.store_as = :d
|
58
|
+
DummyTwo.tag(:label_id, store_as: :l)
|
59
|
+
DummyTwo.tag(:agent_id, store_as: :a)
|
60
|
+
d = DummyTwo.new(1, tags: {label_id: 2, agent_id: 3})
|
61
|
+
d.key_prefix.should == 'd.1:a.3:l.2'
|
35
62
|
end
|
36
63
|
|
37
64
|
describe "Dynamically generated metric" do
|
@@ -41,7 +68,7 @@ describe Nightfury::Identity::Base do
|
|
41
68
|
metric_object = d.third_count
|
42
69
|
metric_object.should be_kind_of(Nightfury::Metric::Value)
|
43
70
|
metric_object.name.should == :third_count
|
44
|
-
metric_object.redis_key_prefix.should == 'dummy
|
71
|
+
metric_object.redis_key_prefix.should == 'dummy.1'
|
45
72
|
end
|
46
73
|
end
|
47
74
|
end
|
@@ -1,25 +1,25 @@
|
|
1
1
|
describe Nightfury::Metric::Value do
|
2
2
|
it "should get a value by delegating to redis" do
|
3
3
|
value_metric = Nightfury::Metric::Value.new(:tickets_count)
|
4
|
-
flexmock(value_metric.redis).should_receive(:get).with('
|
4
|
+
flexmock(value_metric.redis).should_receive(:get).with('tickets_count').once
|
5
5
|
value_metric.get
|
6
6
|
end
|
7
7
|
|
8
8
|
it "should set a value by delegating to redis" do
|
9
9
|
value_metric = Nightfury::Metric::Value.new(:tickets_count)
|
10
|
-
flexmock(value_metric.redis).should_receive(:set).with('
|
10
|
+
flexmock(value_metric.redis).should_receive(:set).with('tickets_count', 1).once
|
11
11
|
value_metric.set(1)
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should increment a value by delegating to redis" do
|
15
15
|
value_metric = Nightfury::Metric::Value.new(:tickets_count)
|
16
|
-
flexmock(value_metric.redis).should_receive(:incrby).with('
|
16
|
+
flexmock(value_metric.redis).should_receive(:incrby).with('tickets_count', 1).once
|
17
17
|
value_metric.incr
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should decrement a value by delegating to redis" do
|
21
21
|
value_metric = Nightfury::Metric::Value.new(:tickets_count)
|
22
|
-
flexmock(value_metric.redis).should_receive(:decrby).with('
|
22
|
+
flexmock(value_metric.redis).should_receive(:decrby).with('tickets_count', 2).once
|
23
23
|
value_metric.decr(2)
|
24
24
|
end
|
25
25
|
end
|
@@ -8,12 +8,17 @@ describe Nightfury::Metric::Base do
|
|
8
8
|
|
9
9
|
it "should have a redis key" do
|
10
10
|
metric = Nightfury::Metric::Base.new(:tickets_count)
|
11
|
-
metric.redis_key.should == "
|
11
|
+
metric.redis_key.should == "tickets_count"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should use store_as in the redis key instead of name if provided" do
|
15
|
+
metric = Nightfury::Metric::Base.new(:tickets_count, store_as: :tc)
|
16
|
+
metric.redis_key.should == "tc"
|
12
17
|
end
|
13
18
|
|
14
19
|
it "should accept a redis key prefix" do
|
15
20
|
metric = Nightfury::Metric::Base.new(:tickets_count, redis_key_prefix: 'prefix')
|
16
|
-
metric.redis_key.should == "prefix:
|
21
|
+
metric.redis_key.should == "prefix:tickets_count"
|
17
22
|
end
|
18
23
|
|
19
24
|
it "should have nightfury's redis connection" do
|
@@ -23,7 +28,7 @@ describe Nightfury::Metric::Base do
|
|
23
28
|
|
24
29
|
it "should be able to remove itself" do
|
25
30
|
metric = Nightfury::Metric::Base.new(:tickets_count)
|
26
|
-
flexmock(metric.redis).should_receive(:del).with('
|
31
|
+
flexmock(metric.redis).should_receive(:del).with('tickets_count').once
|
27
32
|
metric.delete
|
28
33
|
end
|
29
34
|
end
|