Class: DataStore::AverageCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/data_store/average_calculator.rb

Constant Summary collapse

TIMESTAMP_CORRECTION_FACTOR =
0.0001

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ AverageCalculator

Returns a new instance of AverageCalculator.



9
10
11
12
13
14
# File 'lib/data_store/average_calculator.rb', line 9

def initialize(table)
  @table       = table
  @identifier  = table.identifier
  @table_index = table.table_index
  @base        = Base.find(identifier: identifier)
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



7
8
9
# File 'lib/data_store/average_calculator.rb', line 7

def base
  @base
end

#identifierObject (readonly)

Returns the value of attribute identifier.



7
8
9
# File 'lib/data_store/average_calculator.rb', line 7

def identifier
  @identifier
end

#tableObject (readonly)

Returns the value of attribute table.



7
8
9
# File 'lib/data_store/average_calculator.rb', line 7

def table
  @table
end

#table_indexObject (readonly)

Returns the value of attribute table_index.



7
8
9
# File 'lib/data_store/average_calculator.rb', line 7

def table_index
  @table_index
end

Instance Method Details

#performObject

Calculate average value if needed Average value is store through an add call by a Table object So the average calculator is called again recursively



19
20
21
22
23
24
# File 'lib/data_store/average_calculator.rb', line 19

def perform
  if calculation_needed?
    average = previous_average_record ? calculate! : dataset.avg(:value)
    table.add(average, table_index: table_index + 1, created: last[:created], type: :gauge)
  end
end