Class: Shiba::IndexStats::Column

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column, index, rows_per, cardinality) ⇒ Column

Returns a new instance of Column.



73
74
75
76
77
78
# File 'lib/shiba/index_stats.rb', line 73

def initialize(column, index, rows_per, cardinality)
  @column = column
  @index = index
  @rows_per = rows_per
  @cardinality = cardinality
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



80
81
82
# File 'lib/shiba/index_stats.rb', line 80

def column
  @column
end

#rows_perObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/shiba/index_stats.rb', line 90

def rows_per
  return @rows_per if @rows_per && @rows_per.is_a?(Integer)
  return nil if table_count.nil?

  if @rows_per.nil?
    if table_count == 0
      @rows_per = 1
    else
      @rows_per = (table_count / @cardinality).round
    end
  elsif @rows_per.is_a?(String)
    @rows_per = ((@rows_per.to_f / 100.0) * table_count.to_f).round
  end
  @rows_per
end

Instance Method Details

#encode_with(coder) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/shiba/index_stats.rb', line 109

def encode_with(coder)
  coder.map = {'column' => @column}

  count = table_count
  count = 1 if count == 0
  ratio_per_item = self.rows_per / count.to_f

  if count <= 10
    ratio_threshold = 1_000_0000 # always show a number
  elsif count <= 1000
    ratio_threshold = 0.1
  elsif count <= 1_000_000
    ratio_threshold = 0.01
  else
    ratio_threshold = 0.001
  end

  if ratio_per_item > ratio_threshold
    coder.map['rows_per'] = (ratio_per_item * 100).round.to_s + "%"
  else
    coder.map['rows_per'] = rows_per
  end
  coder.tag = nil
end

#raw_cardinalityObject



86
87
88
# File 'lib/shiba/index_stats.rb', line 86

def raw_cardinality
  @cardinality
end

#table_countObject



82
83
84
# File 'lib/shiba/index_stats.rb', line 82

def table_count
  @index.table.count
end