Class: RailsObservatory::TimeSeries
- Inherits:
-
Object
- Object
- RailsObservatory::TimeSeries
show all
- Extended by:
- Insertion
- Defined in:
- lib/rails_observatory/redis/time_series.rb,
lib/rails_observatory/redis/time_series/insertion.rb,
lib/rails_observatory/redis/time_series/query_builder.rb
Defined Under Namespace
Modules: Insertion
Classes: QueryBuilder
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Insertion
distribution, increment
Constructor Details
#initialize(name:, labels: {}, data:, time_range:, agg_duration:) ⇒ TimeSeries
Returns a new instance of TimeSeries.
30
31
32
33
34
35
36
|
# File 'lib/rails_observatory/redis/time_series.rb', line 30
def initialize(name:, labels: {}, data:, time_range:, agg_duration:)
@name = name
@time_range = time_range
@agg_duration = agg_duration
@labels = labels.deep_symbolize_keys
@data = data
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
9
10
11
|
# File 'lib/rails_observatory/redis/time_series.rb', line 9
def data
@data
end
|
#labels ⇒ Object
Returns the value of attribute labels.
9
10
11
|
# File 'lib/rails_observatory/redis/time_series.rb', line 9
def labels
@labels
end
|
#name ⇒ Object
Returns the value of attribute name.
9
10
11
|
# File 'lib/rails_observatory/redis/time_series.rb', line 9
def name
@name
end
|
Class Method Details
.redis ⇒ Object
11
12
13
|
# File 'lib/rails_observatory/redis/time_series.rb', line 11
def self.redis
Rails.configuration.rails_observatory.redis
end
|
.to_ms(duration) ⇒ Object
87
88
89
|
# File 'lib/rails_observatory/redis/time_series.rb', line 87
def self.to_ms(duration)
duration.to_i * 1_000
end
|
.where(**conditions) ⇒ Object
26
27
28
|
# File 'lib/rails_observatory/redis/time_series.rb', line 26
def self.where(**conditions)
QueryBuilder.new(self).where(**conditions)
end
|
.with_slice(time_range) ⇒ Object
19
20
21
22
23
24
|
# File 'lib/rails_observatory/redis/time_series.rb', line 19
def self.with_slice(time_range)
ActiveSupport::IsolatedExecutionState[:observatory_slice] = time_range
yield
ensure
ActiveSupport::IsolatedExecutionState[:observatory_slice] = nil
end
|
Instance Method Details
#empty? ⇒ Boolean
75
76
77
|
# File 'lib/rails_observatory/redis/time_series.rb', line 75
def empty?
data.empty?
end
|
#end_time ⇒ Object
50
51
52
|
# File 'lib/rails_observatory/redis/time_series.rb', line 50
def end_time
@time_range.end.nil? ? Time.now : @time_range.end
end
|
#end_time_ms ⇒ Object
46
47
48
|
# File 'lib/rails_observatory/redis/time_series.rb', line 46
def end_time_ms
end_time.to_i.in_milliseconds
end
|
#filled_data ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/rails_observatory/redis/time_series.rb', line 54
def filled_data
if @time_range.end.nil?
Enumerator
.produce(to_ms(start_time)) { |t| t + @agg_duration }
.take_while { |t| t < to_ms(end_time) }
.map do |t|
match = data.find { |ts, _| ts == t }
if match
timestamp, val = match
[timestamp, val.to_f]
else
[t, 0]
end
end
else
data.map do |ts, value|
[ts, value.to_i || 0]
end
end
end
|
#redis ⇒ Object
15
16
17
|
# File 'lib/rails_observatory/redis/time_series.rb', line 15
def redis
self.class.redis
end
|
#start_time ⇒ Object
38
39
40
|
# File 'lib/rails_observatory/redis/time_series.rb', line 38
def start_time
@time_range.begin.nil? ? Time.utc(2023, 1, 1, 0, 0, 0) : @time_range.begin
end
|
#start_time_ms ⇒ Object
42
43
44
|
# File 'lib/rails_observatory/redis/time_series.rb', line 42
def start_time_ms
start_time.to_i.in_milliseconds
end
|
#to_ms(duration) ⇒ Object
83
84
85
|
# File 'lib/rails_observatory/redis/time_series.rb', line 83
def to_ms(duration)
self.class.to_ms(duration)
end
|
#value ⇒ Object
79
80
81
|
# File 'lib/rails_observatory/redis/time_series.rb', line 79
def value
data.dig(0, 1).to_i
end
|