Class: Aeternitas::Metrics::Ratio
- Inherits:
-
Object
- Object
- Aeternitas::Metrics::Ratio
- Includes:
- Enumerable
- Defined in:
- lib/aeternitas/metrics/ratio.rb
Overview
Stores time series data for ratios The time series values have the following format:
{
timestamp: DateTime("2000-01-01 00:00:00 UTC"),
ratio: 0.01
}
Instance Attribute Summary collapse
-
#from ⇒ DateTime
readonly
Start of the time series.
-
#resolution ⇒ Symbol
readonly
Resolution of the time series.
-
#to ⇒ DateTime
readonly
End of the time series.
-
#values ⇒ Array
readonly
Time series data.
Instance Method Summary collapse
-
#avg ⇒ Float
Computes the average ration within the time series.
- #each(&block) ⇒ Object
-
#initialize(from, to, resolution, values) ⇒ Ratio
constructor
Create a new ratio time series.
-
#max ⇒ Float
Computes the maximum ration within the time series.
-
#min ⇒ Float
Computes the minimum ration within the time series.
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(from, to, resolution, values) ⇒ Ratio
Create a new ratio time series
27 28 29 30 31 32 |
# File 'lib/aeternitas/metrics/ratio.rb', line 27 def initialize(from, to, resolution, values) @from = from @to = to @resolution = resolution @values = values end |
Instance Attribute Details
#from ⇒ DateTime (readonly)
Returns start of the time series.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/aeternitas/metrics/ratio.rb', line 17 class Ratio include Enumerable attr_reader :from, :to, :resolution, :values # Create a new ratio time series # @param [DateTime] from start of the time series # @param [DateTime] to end of the time series # @param [Symbol] resolution time series resolution # @param [Array] values time series data def initialize(from, to, resolution, values) @from = from @to = to @resolution = resolution @values = values end def each(&block) @values.each(&block) end def to_a @values.to_a end # Computes the minimum ration within the time series. # @return [Float] the minimum ratio def min @values.min_by { |v| v['ratio'] }['ratio'] end # Computes the maximum ration within the time series. # @return [Float] the maximum ratio def max @values.max_by { |v| v['ratio'] }['ratio'] end # Computes the average ration within the time series. # @return [Float] the average ratio def avg return 0 if count.zero? p @values @values.inject(0) { |sum, v| sum + v[:ratio] } / @values.count end def to_s values.to_s end end |
#resolution ⇒ Symbol (readonly)
Returns resolution of the time series.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/aeternitas/metrics/ratio.rb', line 17 class Ratio include Enumerable attr_reader :from, :to, :resolution, :values # Create a new ratio time series # @param [DateTime] from start of the time series # @param [DateTime] to end of the time series # @param [Symbol] resolution time series resolution # @param [Array] values time series data def initialize(from, to, resolution, values) @from = from @to = to @resolution = resolution @values = values end def each(&block) @values.each(&block) end def to_a @values.to_a end # Computes the minimum ration within the time series. # @return [Float] the minimum ratio def min @values.min_by { |v| v['ratio'] }['ratio'] end # Computes the maximum ration within the time series. # @return [Float] the maximum ratio def max @values.max_by { |v| v['ratio'] }['ratio'] end # Computes the average ration within the time series. # @return [Float] the average ratio def avg return 0 if count.zero? p @values @values.inject(0) { |sum, v| sum + v[:ratio] } / @values.count end def to_s values.to_s end end |
#to ⇒ DateTime (readonly)
Returns end of the time series.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/aeternitas/metrics/ratio.rb', line 17 class Ratio include Enumerable attr_reader :from, :to, :resolution, :values # Create a new ratio time series # @param [DateTime] from start of the time series # @param [DateTime] to end of the time series # @param [Symbol] resolution time series resolution # @param [Array] values time series data def initialize(from, to, resolution, values) @from = from @to = to @resolution = resolution @values = values end def each(&block) @values.each(&block) end def to_a @values.to_a end # Computes the minimum ration within the time series. # @return [Float] the minimum ratio def min @values.min_by { |v| v['ratio'] }['ratio'] end # Computes the maximum ration within the time series. # @return [Float] the maximum ratio def max @values.max_by { |v| v['ratio'] }['ratio'] end # Computes the average ration within the time series. # @return [Float] the average ratio def avg return 0 if count.zero? p @values @values.inject(0) { |sum, v| sum + v[:ratio] } / @values.count end def to_s values.to_s end end |
#values ⇒ Array (readonly)
Returns time series data.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/aeternitas/metrics/ratio.rb', line 17 class Ratio include Enumerable attr_reader :from, :to, :resolution, :values # Create a new ratio time series # @param [DateTime] from start of the time series # @param [DateTime] to end of the time series # @param [Symbol] resolution time series resolution # @param [Array] values time series data def initialize(from, to, resolution, values) @from = from @to = to @resolution = resolution @values = values end def each(&block) @values.each(&block) end def to_a @values.to_a end # Computes the minimum ration within the time series. # @return [Float] the minimum ratio def min @values.min_by { |v| v['ratio'] }['ratio'] end # Computes the maximum ration within the time series. # @return [Float] the maximum ratio def max @values.max_by { |v| v['ratio'] }['ratio'] end # Computes the average ration within the time series. # @return [Float] the average ratio def avg return 0 if count.zero? p @values @values.inject(0) { |sum, v| sum + v[:ratio] } / @values.count end def to_s values.to_s end end |
Instance Method Details
#avg ⇒ Float
Computes the average ration within the time series.
56 57 58 59 60 |
# File 'lib/aeternitas/metrics/ratio.rb', line 56 def avg return 0 if count.zero? p @values @values.inject(0) { |sum, v| sum + v[:ratio] } / @values.count end |
#each(&block) ⇒ Object
34 35 36 |
# File 'lib/aeternitas/metrics/ratio.rb', line 34 def each(&block) @values.each(&block) end |
#max ⇒ Float
Computes the maximum ration within the time series.
50 51 52 |
# File 'lib/aeternitas/metrics/ratio.rb', line 50 def max @values.max_by { |v| v['ratio'] }['ratio'] end |
#min ⇒ Float
Computes the minimum ration within the time series.
44 45 46 |
# File 'lib/aeternitas/metrics/ratio.rb', line 44 def min @values.min_by { |v| v['ratio'] }['ratio'] end |
#to_a ⇒ Object
38 39 40 |
# File 'lib/aeternitas/metrics/ratio.rb', line 38 def to_a @values.to_a end |
#to_s ⇒ Object
62 63 64 |
# File 'lib/aeternitas/metrics/ratio.rb', line 62 def to_s values.to_s end |