Class: Aeternitas::Metrics::Ratio

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(from, to, resolution, values) ⇒ Ratio

Create a new ratio time series

Parameters:

  • from (DateTime)

    start of the time series

  • to (DateTime)

    end of the time series

  • resolution (Symbol)

    time series resolution

  • values (Array)

    time series data



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

#fromDateTime (readonly)

Returns start of the time series.

Returns:

  • (DateTime)

    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

#resolutionSymbol (readonly)

Returns resolution of the time series.

Returns:

  • (Symbol)

    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

#toDateTime (readonly)

Returns end of the time series.

Returns:

  • (DateTime)

    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

#valuesArray (readonly)

Returns time series data.

Returns:

  • (Array)

    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

#avgFloat

Computes the average ration within the time series.

Returns:

  • (Float)

    the average ratio



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

#maxFloat

Computes the maximum ration within the time series.

Returns:

  • (Float)

    the maximum ratio



50
51
52
# File 'lib/aeternitas/metrics/ratio.rb', line 50

def max
  @values.max_by { |v| v['ratio'] }['ratio']
end

#minFloat

Computes the minimum ration within the time series.

Returns:

  • (Float)

    the minimum ratio



44
45
46
# File 'lib/aeternitas/metrics/ratio.rb', line 44

def min
  @values.min_by { |v| v['ratio'] }['ratio']
end

#to_aObject



38
39
40
# File 'lib/aeternitas/metrics/ratio.rb', line 38

def to_a
  @values.to_a
end

#to_sObject



62
63
64
# File 'lib/aeternitas/metrics/ratio.rb', line 62

def to_s
  values.to_s
end