class ElaticSensitivity

Attributes

c[RW]
cache_mfx_table[RW]
elastic_sensitivity[RW]
epi[RW]
k[RW]
k_square[RW]
main_t[RW]

Public Class Methods

new(table) click to toggle source
# File lib/rails_elastic_sensitivity.rb, line 7
def initialize(table)
  @cache_mfx_table = {}
  @main_t = table.to_s.classify.constantize
  Rails.cache.write('now_es', self)
  @k_square = 0
  @k = 1
  @c = 1
end

Public Instance Methods

compute_constant(precompute_params) click to toggle source
# File lib/rails_elastic_sensitivity.rb, line 71
def compute_constant(precompute_params)
  precompute_params.map do |ps|
    key = "#{ps[1]}_#{ps[0]}"
    @cache_mfx_table[key]
  end.reduce(0, :+) + 1
end
details() click to toggle source

Print model table name, sql description, noise added, elastic sensitivity, mfx value

# File lib/rails_elastic_sensitivity.rb, line 27
def details
  p '===== details ====='
  p "Main TABLE: #{@main_t}"
  p "MAX_FREQUENCY_METRIX"
  p @cache_mfx_table
  '===== details ====='
end
joins(*args) click to toggle source

like User.joins(:gamecharacters)

# File lib/rails_elastic_sensitivity.rb, line 36
def joins(*args)
  self_table_name = @main_t.to_s.downcase
  joins_t = args[0]

  #  multiple joins
  if args[0].is_a?(Hash)
    @k_square = 2
    first_t = joins_t.keys[0]
    second_t = joins_t[first_t]
    precompute_params = [
      ['id', "#{self_table_name}s"],
      ["#{self_table_name}_id", first_t.to_s],
    ]
    precompute_params.each{ |ps| precompute_mfx(*ps) }
    temp_c = compute_constant(precompute_params)
    temp_k = 2
    mfx_on_second_t = precompute_mfx("#{first_t.to_s.singularize}_id", second_t.to_s)
    @k = temp_k + (temp_k * mfx_on_second_t) + (temp_c) + 1
    @c = (temp_c * mfx_on_second_t) + mfx_on_second_t + temp_c
  else
    # single joins
    @k_square = 0
    precompute_params = [
      ['id', "#{self_table_name}s"],
      ["#{self_table_name}_id", joins_t.to_s],
    ]
    precompute_params.each{ |ps| precompute_mfx(*ps) }
    @c = compute_constant(precompute_params)
    @k = 2
  end

  Rails.cache.write('now_es', self)
  @main_t.joins(joins_t)
end
precompute_mfx(attribute, table) click to toggle source
# File lib/rails_elastic_sensitivity.rb, line 16
def precompute_mfx(attribute, table)
  if @cache_mfx_table["#{table}_#{attribute}"]
    return @cache_mfx_table["#{table}_#{attribute}"]
  end

  sql = "SELECT COUNT(#{attribute}) as count FROM #{table} GROUP BY #{attribute} ORDER BY count DESC LIMIT 1;"
  records_array = ActiveRecord::Base.connection.execute(sql)
  return @cache_mfx_table["#{table}_#{attribute}"] = records_array.first[0] # result
end