Module: MmEsSearch::Models::AbstractRangeFacetModel

Extended by:
ActiveSupport::Concern
Includes:
Api::Facet, Api::Query
Defined in:
lib/mm_es_search/models/abstract_range_facet_model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#best_time_unitObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 130

def best_time_unit
  if rows.present?
    all_times = rows.map { |r| [r.from, r.to] }.flatten.compact
    min = all_times.min
    max = all_times.max
  else
    min = stats.min
    max = stats.max
  end
  diff = Time.diff(min, max)
  if    diff[:year].abs   > 0 then :year
  elsif diff[:month].abs  > 0 then :month
  elsif diff[:day].abs    > 0 then :day
  elsif diff[:hour].abs   > 0 then :hour
  elsif diff[:minute].abs > 0 then :min
  elsif diff[:second].abs > 0 then :sec
                              else :year
  end
end

#build_field_stats(result) ⇒ Object



71
72
73
74
75
76
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 71

def build_field_stats(result)
  self.stats = StatisticalFacetResult.new(result.except('_type'))
  zero_rows
  current_state
  field_stats_set
end

#build_rows(values) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 335

def build_rows(values)
  self.rows = checked_rows #NOTE: we preserve selected rows
  if (num_values = values.length) == 2
    rows << RangeFacetRow.new(:from => values.first, :to => values.last)
  else
    rows << RangeFacetRow.new(:to => values.first)                          # -> 1 (if starting with 1,2,3,4)
    (num_values - 1).times do
      rows << RangeFacetRow.new(:from => values.shift, :to => values.first) # 1 -> 2,  2 -> 3,  3 -> 4
    end
    rows << RangeFacetRow.new(:from => values.first) if num_values > 1      # 4 ->
  end
end

#calculate_range_values(min, max, mean, sd) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 259

def calculate_range_values(min, max, mean, sd)
  
  #TODO come up with a system that works with decimal values
  # AND when the diff between lower and upper is very small or zero
  
  final_casting = if min.is_a?(Float) or max.is_a?(Float)
    :to_f
  else
    :to_i
  end
  
  # orig_range = max - min
  # if orig_range < 50 and not orig_range.zero?
    # scale = (50/orig_range.floor)
    # scale = round_to_power_of_ten(scale, :up, [(scale.to_s.length - 1), 1].max)
    # min, max, mean, sd = [min, max, mean, sd].map { |v| v*scale }
  # else
    # scale = nil
  # end
  
  orig_lower = [min.floor, (mean - sd).floor].max
  orig_upper = [max.ceil, (mean + sd).ceil].min
  orig_range = orig_upper - orig_lower
  
  if orig_range < 100 and not orig_range.zero?
    #binding.pry
    scale = (100/orig_range.floor)
    scale = round_to_power_of_ten(scale, :up, [(scale.to_s.length - 1), 1].max)
    lower, upper, range = [orig_lower, orig_upper, orig_range].map { |v| v*scale }
  else
    lower, upper, range = orig_lower, orig_upper, orig_range
    scale = nil
  end
  
  power = if range.zero?
    lower.to_s.length - 1
  else  
    range.to_s.length - 1
  end
  
  lower = round_to_power_of_ten(lower, :down, power)
  upper = round_to_power_of_ten(upper, :up, power)
  orig_inc = ((upper - lower) / (TARGET_NUM_ROLES-2)).floor
  
  #binding.pry
  
  inc = round_to_power_of_ten(orig_inc, :up, power)
  
  if inc == 0
    values = [lower - 5, lower + 5] #TODO remove this once pruning support is added
    #binding.pry
  else
    values = (TARGET_NUM_ROLES-1).times.map { |i| lower + inc*i }
    if (gte_upper = values.select { |n| n >= upper }).length > 1
      values = values[0..-gte_upper.length]
    end
  end
  
  values.map!(&final_casting)
  puts "SCALE SCALE SCALE is #{scale}"
  if scale and not scale.zero?
    values.map! { |v| v/scale }
  end
  
  #binding.pry if orig_range == 0
  puts "values are: #{values}"
  
  values
  
end

#deserialize_value(key, es_value) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 150

def deserialize_value(key, es_value)
  
  case key
  when "total", "sum_of_squares", "variance"
    return nil #i.e. discard these
  end
  
  if @transform_lookup and stored_val = @transform_lookup[es_value]
    return stored_val
  end
  
  if is_time?
    
    case key
    when "from", "to", "min", "max", "mean"
                  
      if timezone_matters?
        case es_value
        when Numeric
          Time.zone.at(es_value/1000)
        when String
          Time.zone.parse es_value
        end
      else
        t = case es_value
        when Numeric
          Time.at(es_value/1000)
        when String
          Time.parse es_value
        end
        Time.zone.local(*t.to_a[0..5].reverse)
      end
      
    when "std_deviation"
      es_value/1000
      
    when "count"
      es_value
      
    end
    

  else
    es_value
  end
  
end

#get_time_stats(time_unit) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 224

def get_time_stats(time_unit)
  min, max, mean = [:min, :max, :mean].map do |stat|
    stats.send(stat).send time_unit
  end
  long_time_unit = case time_unit
  when :sec then :second
  when :min then :minute
  else time_unit
  end
  sd = stats.std_deviation/(1.send long_time_unit) #e.g. 1.year or 1.month
  return min, max, mean, sd
end

#handle_field_stats_result(result) ⇒ Object



214
215
216
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 214

def handle_field_stats_result(result)
  build_field_stats transform_field_stats(result)
end

#initialize_numeric_rowsObject



330
331
332
333
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 330

def initialize_numeric_rows
  values = calculate_range_values(stats.min, stats.max, stats.mean, stats.std_deviation)
  build_rows(values)
end

#initialize_rowsObject

end



121
122
123
124
125
126
127
128
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 121

def initialize_rows
  @transform_lookup = {}
  if is_time?
    initialize_time_rows
  else
    initialize_numeric_rows
  end
end

#initialize_time_rowsObject



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 237

def initialize_time_rows
  time_unit = best_time_unit
  min, max, mean, sd = get_time_stats(time_unit)
  range_vals = calculate_range_values(min, max, mean, sd)
  unit_index = TIME_UNITS.index(time_unit)
  row_times = range_vals.map do |val|
    time_params = DEFAULT_TIME_PARAMS.dup
    if unit_index > 0 #e.g. if all same year, copy year across from stats.mean
      mean_array = stats.mean.to_a[0..5].reverse
      time_params[0..unit_index-1] = mean_array[0..unit_index-1]
    end
    time_params[unit_index] = val
    Time.zone.local(*time_params)
  end
  es_times = row_times.map do |row_time|
    serialize_value(row_time).tap do |es_time|
      @transform_lookup[es_time] = row_time
    end
  end
  build_rows(es_times)
end

#is_time?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 59

def is_time?
  false
end

#required_row_fieldsObject



367
368
369
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 367

def required_row_fields
  ['from', 'to']
end

#result_nameObject



55
56
57
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 55

def result_name
  'ranges'
end

#round_to_power_of_ten(n, direction, power) ⇒ Object



348
349
350
351
352
353
354
355
356
357
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 348

def round_to_power_of_ten(n, direction, power)
  p = 10**power
  return n if (n % p).zero?
  case direction
  when :up
    n + (p - n % p)
  when :down
    n - n % p
  end
end

#row_classObject



67
68
69
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 67

def row_class
  RangeFacetRow
end

#select_display_modeObject



359
360
361
362
363
364
365
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 359

def select_display_mode
  #logic for setting mode based on stats
  #TODO build this logic
  if true
    self.display_mode = "range"
  end
end

#serialize_value(value, for_filter = false) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 198

def serialize_value(value, for_filter = false)
  es_value = if is_time?
    t = if timezone_matters?
      value.utc
    else
      Time.utc(*value.to_a[0..5].reverse)
    end
    for_filter ? t.iso8601 : t.to_f*1000
  else
    value
  end
  @transform_lookup ||= {}
  @transform_lookup[es_value] = value
  es_value
end

#timezone_matters?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 63

def timezone_matters?
  true
end

#to_facetObject

end



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 92

def to_facet
  case display_mode || select_display_mode
  when "range"
    initialize_rows
    RangeFacet.new(
      default_params.merge(
        :label        => prefix_label('display_result'),
        :ranges       => rows.map(&:to_range_item),
        :facet_filter => facet_filter
      )
    )
  when "histogram"
    raise NotImplementedError
  else
    raise "display mode '#{display_mode}' is not recognised"
  end
end

#to_stats_facetObject



78
79
80
81
82
83
84
85
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 78

def to_stats_facet
  StatisticalFacet.new(
    default_params.merge(
      :label        => prefix_label('field_stats_result'),
      :facet_filter => facet_filter
    )
  )
end

#transform_field_stats(result) ⇒ Object



218
219
220
221
222
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 218

def transform_field_stats(result)
  result.each_with_object({}) do |(key, value), output|
    output[key] = deserialize_value(key, value)
  end
end