Class: OlapReport::Cube::Adapters::AbstractAdapter
- Inherits:
-
Object
- Object
- OlapReport::Cube::Adapters::AbstractAdapter
- Defined in:
- lib/olap_report/cube/adapters/abstract_adapter.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #create_aggregated_table(table) ⇒ Object
-
#initialize(model) ⇒ AbstractAdapter
constructor
A new instance of AbstractAdapter.
-
#measure_update_sql(measure, measures = []) ⇒ Object
Build update statements for measures’ values sum = sum + new sum count = count + new count max = MAX(max, new max) min = MIN(min, new min) avg if no count or sum measures in current aggregation, recreate aggregated table from scratch if count measure exists avg = (avg * count + new avg * new count) / (count + new count) if sum measure exists avg = (sum + new sum) / (sum / avg + new sum / new avg).
- #method_missing(meth, *args, &block) ⇒ Object
- #update_aggregated_table(query, update_values_sql) ⇒ Object
Constructor Details
#initialize(model) ⇒ AbstractAdapter
Returns a new instance of AbstractAdapter.
5 6 7 8 |
# File 'lib/olap_report/cube/adapters/abstract_adapter.rb', line 5 def initialize(model) raise ArgumentError unless model @connection = model.connection end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/olap_report/cube/adapters/abstract_adapter.rb', line 10 def method_missing(meth, *args, &block) if @connection.respond_to?(meth) @connection.public_send(meth, *args, &block) else super end end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
3 4 5 |
# File 'lib/olap_report/cube/adapters/abstract_adapter.rb', line 3 def connection @connection end |
Instance Method Details
#create_aggregated_table(table) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/olap_report/cube/adapters/abstract_adapter.rb', line 19 def create_aggregated_table(table) connection.create_table table.table_name, id: false, force: true do |t| table.columns.each do |column_obj| = {} [:default, :limit, :scale, :precision, :null, :primary, :coder].each do |opt| [opt] = column_obj.public_send(opt) end t.public_send column_obj.type, column_obj.name, end end index_columns = table.levels.map(&:name).sort index_name = (%w|idx| + index_columns).join('_') connection.add_index table.table_name, index_columns, unique: true, name: index_name end |
#measure_update_sql(measure, measures = []) ⇒ Object
Build update statements for measures’ values
sum = sum + new sum
count = count + new count
max = MAX(max, new max)
min = MIN(min, new min)
avg
if no count or sum measures in current aggregation, recreate aggregated table from scratch
if count measure exists
avg = (avg * count + new avg * new count) / (count + new count)
if sum measure exists
avg = (sum + new sum) / (sum / avg + new sum / new avg)
49 50 |
# File 'lib/olap_report/cube/adapters/abstract_adapter.rb', line 49 def measure_update_sql(measure, measures=[]) end |
#update_aggregated_table(query, update_values_sql) ⇒ Object
52 53 54 |
# File 'lib/olap_report/cube/adapters/abstract_adapter.rb', line 52 def update_aggregated_table(query, update_values_sql) connection.execute query end |