Class: ElaticSensitivity

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ ElaticSensitivity

Returns a new instance of ElaticSensitivity.



7
8
9
10
11
12
13
14
# 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

Instance Attribute Details

#cObject

Returns the value of attribute c.



5
6
7
# File 'lib/rails_elastic_sensitivity.rb', line 5

def c
  @c
end

#cache_mfx_tableObject

Returns the value of attribute cache_mfx_table.



5
6
7
# File 'lib/rails_elastic_sensitivity.rb', line 5

def cache_mfx_table
  @cache_mfx_table
end

#elastic_sensitivityObject

Returns the value of attribute elastic_sensitivity.



5
6
7
# File 'lib/rails_elastic_sensitivity.rb', line 5

def elastic_sensitivity
  @elastic_sensitivity
end

#epiObject

Returns the value of attribute epi.



5
6
7
# File 'lib/rails_elastic_sensitivity.rb', line 5

def epi
  @epi
end

#kObject

Returns the value of attribute k.



5
6
7
# File 'lib/rails_elastic_sensitivity.rb', line 5

def k
  @k
end

#k_squareObject

Returns the value of attribute k_square.



5
6
7
# File 'lib/rails_elastic_sensitivity.rb', line 5

def k_square
  @k_square
end

#main_tObject

Returns the value of attribute main_t.



5
6
7
# File 'lib/rails_elastic_sensitivity.rb', line 5

def main_t
  @main_t
end

Instance Method Details

#compute_constant(precompute_params) ⇒ Object



71
72
73
74
75
76
# 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

#detailsObject

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



27
28
29
30
31
32
33
# 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) ⇒ Object

like User.joins(:gamecharacters)



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
66
67
68
69
# 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) ⇒ Object



16
17
18
19
20
21
22
23
24
# 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