nightfury 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ module Nightfury
2
+ module Metric
3
+ class CountTimeSeries < TimeSeries
4
+ def incr(step=1, timestamp = Time.now)
5
+ last_data_point = get
6
+ if last_data_point
7
+ time, value = last_data_point.flatten
8
+ value = value.to_i + step
9
+ set(value, timestamp)
10
+ else
11
+ set(step, timestamp)
12
+ end
13
+ end
14
+
15
+ def decr(step=1, timestamp = Time.now)
16
+ last_data_point = get
17
+ if last_data_point
18
+ time, value = last_data_point.flatten
19
+ value = value.to_i - step
20
+ set(value, timestamp)
21
+ else
22
+ set(-step, timestamp)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module Nightfury
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+
3
+ describe Nightfury::Metric::CountTimeSeries do
4
+ describe "Incr" do
5
+ context "Has data points" do
6
+ it "should be able to increment value by 1 at the current timestamp by default" do
7
+ count_series = Nightfury::Metric::CountTimeSeries.new(1)
8
+ time_now = Time.now
9
+ # Add a data point
10
+ count_series.set(1, time_now - 10)
11
+
12
+ Timecop.freeze(time_now) do
13
+ count_series.incr
14
+ end
15
+ Timecop.return
16
+
17
+ count_series.get.should == { time_now.to_i.to_s => "2"}
18
+ end
19
+
20
+ it "should be able to increment value by a given step" do
21
+ count_series = Nightfury::Metric::CountTimeSeries.new(1)
22
+ time_now = Time.now
23
+ # Add a data point
24
+ count_series.set(1, time_now - 10)
25
+
26
+ Timecop.freeze(time_now) do
27
+ count_series.incr(2)
28
+ end
29
+ Timecop.return
30
+
31
+ count_series.get.should == { time_now.to_i.to_s => "3"}
32
+ end
33
+
34
+
35
+ it "should be able to increment value at a given timestamp" do
36
+ count_series = Nightfury::Metric::CountTimeSeries.new(1)
37
+ time_now = Time.now
38
+ time_later = time_now + 10
39
+ # Add a data point
40
+ count_series.set(1, time_now - 10)
41
+ count_series.incr(1,time_later)
42
+ count_series.get.should == { time_later.to_i.to_s => "2"}
43
+ end
44
+ end
45
+
46
+ context "Has no data points" do
47
+ it "should add a data point with the given step" do
48
+ count_series = Nightfury::Metric::CountTimeSeries.new(1)
49
+ time_now = Time.now
50
+ count_series.incr(2, time_now)
51
+ count_series.get.should == { time_now.to_i.to_s => "2"}
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "Decr" do
57
+ context "Has data points" do
58
+ it "should be able to decrement value by 1 at the current timestamp by default" do
59
+ count_series = Nightfury::Metric::CountTimeSeries.new(1)
60
+ time_now = Time.now
61
+ # Add a data point
62
+ count_series.set(1, time_now - 10)
63
+
64
+ Timecop.freeze(time_now) do
65
+ count_series.decr
66
+ end
67
+ Timecop.return
68
+
69
+ count_series.get.should == { time_now.to_i.to_s => "0"}
70
+ end
71
+
72
+ it "should be able to decrement value by a given step" do
73
+ count_series = Nightfury::Metric::CountTimeSeries.new(1)
74
+ time_now = Time.now
75
+ # Add a data point
76
+ count_series.set(2, time_now - 10)
77
+
78
+ Timecop.freeze(time_now) do
79
+ count_series.decr(2)
80
+ end
81
+ Timecop.return
82
+
83
+ count_series.get.should == { time_now.to_i.to_s => "0"}
84
+ end
85
+
86
+
87
+ it "should be able to decrement value at a given timestamp" do
88
+ count_series = Nightfury::Metric::CountTimeSeries.new(1)
89
+ time_now = Time.now
90
+ time_later = time_now + 10
91
+ # Add a data point
92
+ count_series.set(1, time_now - 10)
93
+ count_series.decr(1,time_later)
94
+ count_series.get.should == { time_later.to_i.to_s => "0"}
95
+ end
96
+ end
97
+
98
+ context "Has no data points" do
99
+ it "should add a data point with the given step" do
100
+ count_series = Nightfury::Metric::CountTimeSeries.new(1)
101
+ time_now = Time.now
102
+ count_series.decr(2, time_now)
103
+ count_series.get.should == { time_now.to_i.to_s => "-2"}
104
+ end
105
+ end
106
+ end
107
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nightfury
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -30,12 +30,14 @@ files:
30
30
  - lib/nightfury/identity.rb
31
31
  - lib/nightfury/metric.rb
32
32
  - lib/nightfury/metric/avg_time_series.rb
33
+ - lib/nightfury/metric/count_time_series.rb
33
34
  - lib/nightfury/metric/time_series.rb
34
35
  - lib/nightfury/metric/value.rb
35
36
  - lib/nightfury/version.rb
36
37
  - nightfury.gemspec
37
38
  - spec/nightfury/identity_spec.rb
38
39
  - spec/nightfury/metric/avg_time_series_spec.rb
40
+ - spec/nightfury/metric/count_time_series_spec.rb
39
41
  - spec/nightfury/metric/time_series_spec.rb
40
42
  - spec/nightfury/metric/value_spec.rb
41
43
  - spec/nightfury/metric_spec.rb
@@ -69,6 +71,7 @@ summary: Nightfury is a reporting/analytics backend written on Redis
69
71
  test_files:
70
72
  - spec/nightfury/identity_spec.rb
71
73
  - spec/nightfury/metric/avg_time_series_spec.rb
74
+ - spec/nightfury/metric/count_time_series_spec.rb
72
75
  - spec/nightfury/metric/time_series_spec.rb
73
76
  - spec/nightfury/metric/value_spec.rb
74
77
  - spec/nightfury/metric_spec.rb