nightfury 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,21 @@
1
1
  module Nightfury
2
2
  module Metric
3
3
  class AvgTimeSeries < TimeSeries
4
+ def get_padded_range(start_time, end_time)
5
+ data_points = get_range(start_time, end_time)
6
+ each_timestamp(start_time, end_time) do |current_step_time, last_step_time|
7
+ current_step_time = current_step_time.to_s
8
+ last_step_time = last_step_time.to_s
9
+ next if data_points[current_step_time]
10
+ data_points[current_step_time] = '0.0'
11
+ end
12
+ data_points
13
+ end
4
14
 
5
15
  protected
6
16
 
7
17
  def before_set(value, time)
8
- step_time = get_step_time(time).to_i
18
+ step_time = get_step_time(time)
9
19
  data_point, meta_value = get_exact(step_time, true)
10
20
  current_count = current_total = 0.0
11
21
  unless data_point.nil?
@@ -1,6 +1,17 @@
1
1
  module Nightfury
2
2
  module Metric
3
3
  class CountTimeSeries < TimeSeries
4
+ def get_padded_range(start_time, end_time)
5
+ data_points = get_range(start_time, end_time)
6
+ each_timestamp(start_time, end_time) do |current_step_time, last_step_time|
7
+ current_step_time = current_step_time.to_s
8
+ last_step_time = last_step_time.to_s
9
+ next if data_points[current_step_time]
10
+ data_points[current_step_time] = data_points[last_step_time]
11
+ end
12
+ data_points
13
+ end
14
+
4
15
  def incr(step=1, timestamp = Time.now)
5
16
  last_data_point = get
6
17
  if last_data_point
@@ -5,6 +5,16 @@ module Nightfury
5
5
  def self.floor_time(time, seconds=60)
6
6
  Time.at((time.to_f / seconds).floor * seconds)
7
7
  end
8
+
9
+ def self.seconds_in_step(step_name, time)
10
+ {
11
+ minute: 60,
12
+ hour: 1.hour,
13
+ day: 1.day,
14
+ week: 1.week,
15
+ month: Time.days_in_month(time.month, time.year)
16
+ }[step_name]
17
+ end
8
18
 
9
19
  def initialize(name, options={})
10
20
  super(name, options)
@@ -51,9 +61,9 @@ module Nightfury
51
61
 
52
62
  def get_range(start_time, end_time)
53
63
  return nil unless redis.exists(redis_key)
54
- start_time = get_step_time(start_time).to_i
55
- end_time = get_step_time(end_time).to_i
56
- data_points = redis.zrangebyscore(redis_key, start_time, end_time, withscores: true)
64
+ start_time = get_step_time(start_time)
65
+ end_time = get_step_time(end_time)
66
+ data_points = redis.zrangebyscore(redis_key, start_time.to_i, end_time.to_i, withscores: true)
57
67
  decode_many_data_points(data_points)
58
68
  end
59
69
 
@@ -80,14 +90,8 @@ module Nightfury
80
90
  {}
81
91
  end
82
92
 
83
- protected
84
-
85
- def before_set(value, time)
86
- [value, time]
87
- end
88
-
89
- def decode_data_point(data_point)
90
- [data_point[1], data_point[0], {}]
93
+ def seconds_in_step(time)
94
+ self.class.seconds_in_step(step, time)
91
95
  end
92
96
 
93
97
  def floor_time(time, seconds=60)
@@ -95,15 +99,30 @@ module Nightfury
95
99
  end
96
100
 
97
101
  def get_step_time(time)
98
- case step
99
- when :minute then floor_time(time, 60)
100
- when :hour then floor_time(time, 1.hour)
101
- when :day then floor_time(time, 1.day)
102
- when :week then floor_time(time, 1.week)
103
- when :month then floor_time(time, Time.days_in_month(time.month, time.year))
102
+ floor_time(time, seconds_in_step(time))
103
+ end
104
+
105
+ def each_timestamp(start_time, end_time, &block)
106
+ start_time = get_step_time(start_time).to_i
107
+ end_time = get_step_time(end_time).to_i
108
+ current_step_time, last_step_time = start_time, nil
109
+ start_time.step(end_time, seconds_in_step(Time.at(current_step_time))) do
110
+ yield(current_step_time, last_step_time)
111
+ last_step_time = current_step_time
112
+ current_step_time += seconds_in_step(Time.at(current_step_time))
104
113
  end
105
114
  end
106
115
 
116
+ protected
117
+
118
+ def before_set(value, time)
119
+ [value, time]
120
+ end
121
+
122
+ def decode_data_point(data_point)
123
+ [data_point[1], data_point[0], {}]
124
+ end
125
+
107
126
  private
108
127
 
109
128
  def add_value_to_timeline(value, time)
@@ -1,3 +1,3 @@
1
1
  module Nightfury
2
- VERSION = "0.7.1"
2
+ VERSION = "0.7.2"
3
3
  end
@@ -1,6 +1,19 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Nightfury::Metric::AvgTimeSeries do
4
+ describe "#get_padded_range" do
5
+ it "should fill the missing data points" do
6
+ count_series = Nightfury::Metric::AvgTimeSeries.new(:count)
7
+ start_time = Time.new(2013,1,1,0,0,0)
8
+ end_time = start_time + 120
9
+ missing_time = start_time + 60
10
+
11
+ count_series.set(1, start_time)
12
+ count_series.set(2, end_time)
13
+ count_series.get_padded_range(start_time, end_time)[missing_time.to_i.to_s].should == "0.0"
14
+ end
15
+ end
16
+
4
17
  it "should calculate avg in buckets defined by step" do
5
18
  avg_metric = Nightfury::Metric::AvgTimeSeries.new(:avg)
6
19
  first_bucket_time = Time.now
@@ -1,6 +1,19 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Nightfury::Metric::CountTimeSeries do
4
+ describe "#get_padded_range" do
5
+ it "should fill the missing data points" do
6
+ count_series = Nightfury::Metric::CountTimeSeries.new(:count)
7
+ start_time = Time.new(2013,1,1,0,0,0)
8
+ end_time = start_time + 120
9
+ missing_time = start_time + 60
10
+
11
+ count_series.set(1, start_time)
12
+ count_series.set(2, end_time)
13
+ count_series.get_padded_range(start_time, end_time)[missing_time.to_i.to_s].should == "1"
14
+ end
15
+ end
16
+
4
17
  describe "Incr" do
5
18
  context "Has data points" do
6
19
  it "should be able to increment value by 1 at the current timestamp by default" do
@@ -18,6 +18,22 @@ describe Nightfury::Metric::TimeSeries do
18
18
  end
19
19
  end
20
20
 
21
+ describe "#each_timestamp" do
22
+ it "should iterate through each timestamp between a start and a end time" do
23
+ ts_metric = Nightfury::Metric::TimeSeries.new(:avg_time)
24
+
25
+ start_time = Time.new(2013,1,1,0,0,0)
26
+ mid_time = start_time + 60
27
+ end_time = mid_time + 60
28
+
29
+ expected = [[start_time.to_i, nil], [mid_time.to_i, start_time.to_i], [end_time.to_i, mid_time.to_i]]
30
+
31
+ expect { |b|
32
+ ts_metric.each_timestamp(start_time, end_time, &b)
33
+ }.to yield_successive_args(*expected)
34
+ end
35
+ end
36
+
21
37
  describe "Getter" do
22
38
  describe "#get" do
23
39
  it "should retrun nil if metric key on redis is empty" do
@@ -179,7 +195,7 @@ describe Nightfury::Metric::TimeSeries do
179
195
  it "should call before_set, before adding the value to the timeline" do
180
196
  ts_metric = Nightfury::Metric::TimeSeries.new(:avg_time)
181
197
  time = Time.now
182
- flexmock(ts_metric).should_receive(:before_set).with(1, time).once
198
+ flexmock(ts_metric).should_receive(:before_set).with(1, time).and_return([1,time]).once
183
199
  ts_metric.set(1, time)
184
200
  end
185
201
 
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.7.1
4
+ version: 0.7.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: