Class: Nightfury::Metric::TimeSeries
- Inherits:
-
Base
- Object
- Base
- Nightfury::Metric::TimeSeries
show all
- Defined in:
- lib/nightfury/metric/time_series.rb
Constant Summary
Constants inherited
from Base
Base::ALLOWED_STEPS
Instance Attribute Summary
Attributes inherited from Base
#name, #redis, #redis_key_prefix, #step, #store_as
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#delete, #redis_key
Constructor Details
#initialize(name, options = {}) ⇒ TimeSeries
Returns a new instance of TimeSeries.
19
20
21
22
|
# File 'lib/nightfury/metric/time_series.rb', line 19
def initialize(name, options={})
super(name, options)
init_time_series unless redis.exists(redis_key)
end
|
Class Method Details
.floor_time(time, seconds = 60) ⇒ Object
5
6
7
|
# File 'lib/nightfury/metric/time_series.rb', line 5
def self.floor_time(time, seconds=60)
Time.at((time.to_f / seconds).floor * seconds)
end
|
.seconds_in_step(step_name, time) ⇒ Object
9
10
11
12
13
14
15
16
17
|
# File 'lib/nightfury/metric/time_series.rb', line 9
def self.seconds_in_step(step_name, time)
{
minute: 60,
hour: 1.hour,
day: 1.day,
week: 1.week,
month: Time.days_in_month(time.month, time.year).days
}[step_name]
end
|
Instance Method Details
89
90
91
|
# File 'lib/nightfury/metric/time_series.rb', line 89
def default_meta
{}
end
|
#each_timestamp(start_time, end_time, &block) ⇒ Object
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/nightfury/metric/time_series.rb', line 105
def each_timestamp(start_time, end_time, &block)
start_time = get_step_time(start_time).to_i
end_time = get_step_time(end_time).to_i
current_step_time, last_step_time = start_time, nil
start_time.step(end_time, seconds_in_step(Time.at(current_step_time))) do
yield(current_step_time, last_step_time)
last_step_time = current_step_time
current_step_time += seconds_in_step(Time.at(current_step_time))
end
end
|
#floor_time(time, seconds = 60) ⇒ Object
97
98
99
|
# File 'lib/nightfury/metric/time_series.rb', line 97
def floor_time(time, seconds=60)
self.class.floor_time(time, seconds)
end
|
#get(timestamp = nil, get_meta = false) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/nightfury/metric/time_series.rb', line 33
def get(timestamp=nil, get_meta=false)
return nil unless redis.exists(redis_key)
data_point = ''
if timestamp
timestamp = get_step_time(timestamp).to_i
data_point = redis.zrangebyscore(redis_key, 0, timestamp, withscores: true)
data_point = data_point.last
else
data_point = redis.zrevrange(redis_key, 0, 0, withscores: true)
data_point = data_point.last
end
return get_meta ? [nil, {}] : nil if data_point.nil?
return get_meta ? [nil, {}] : nil if data_point[1] == 0.0
time, data, meta_value = decode_data_point(data_point)
get_meta ? [{time => data}, meta_value] : {time => data}
end
|
#get_all ⇒ Object
70
71
72
73
74
|
# File 'lib/nightfury/metric/time_series.rb', line 70
def get_all
return nil unless redis.exists(redis_key)
data_points = redis.zrange(redis_key,1,-1, withscores: true)
decode_many_data_points(data_points)
end
|
#get_exact(timestamp, get_meta = false) ⇒ Object
52
53
54
55
56
57
58
59
60
|
# File 'lib/nightfury/metric/time_series.rb', line 52
def get_exact(timestamp, get_meta=false)
return nil unless redis.exists(redis_key)
timestamp = get_step_time(timestamp).to_i
data_point = redis.zrangebyscore(redis_key, timestamp, timestamp, withscores: true)
data_point = data_point.last
return get_meta ? [nil, {}] : nil if data_point.nil?
time, data, meta_value = decode_data_point(data_point)
result = get_meta ? [{time => data}, meta_value] : {time => data}
end
|
#get_range(start_time, end_time) ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/nightfury/metric/time_series.rb', line 62
def get_range(start_time, end_time)
return nil unless redis.exists(redis_key)
start_time = get_step_time(start_time)
end_time = get_step_time(end_time)
data_points = redis.zrangebyscore(redis_key, start_time.to_i, end_time.to_i, withscores: true)
decode_many_data_points(data_points)
end
|
#get_step_time(time) ⇒ Object
101
102
103
|
# File 'lib/nightfury/metric/time_series.rb', line 101
def get_step_time(time)
floor_time(time, seconds_in_step(time))
end
|
76
77
78
79
80
81
82
|
# File 'lib/nightfury/metric/time_series.rb', line 76
def meta
unless @meta
json = redis.zrange(redis_key, 0, 0).first
@meta = JSON.parse(json)
end
@meta
end
|
84
85
86
87
|
# File 'lib/nightfury/metric/time_series.rb', line 84
def meta=(value)
@meta = value
save_meta
end
|
#seconds_in_step(time) ⇒ Object
93
94
95
|
# File 'lib/nightfury/metric/time_series.rb', line 93
def seconds_in_step(time)
self.class.seconds_in_step(step, time)
end
|
#set(value, time = Time.now, options = {}) ⇒ Object
24
25
26
27
28
29
30
31
|
# File 'lib/nightfury/metric/time_series.rb', line 24
def set(value, time=Time.now, options={})
value, time = before_set(value, time) unless options[:skip_before_set]
init_time_series unless redis.exists(redis_key)
add_value_to_timeline(value, time)
end
|