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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 166

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



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

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

#build_rows(values) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 380

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



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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 295

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



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 186

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

#distance_centerObject



117
118
119
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 117

def distance_center
  "gcpvj32dx" #London, UK
end

#distance_unitObject



371
372
373
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 371

def distance_unit
  @distance_unit ||= "km"
end

#es_facet_classObject

end



101
102
103
104
105
106
107
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 101

def es_facet_class
  if is_distance?
    GeoDistanceFacet
  else
    RangeFacet
  end
end

#es_filter_classObject



109
110
111
112
113
114
115
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 109

def es_filter_class
  if is_distance?
    GeoDistanceRangeFilter
  else
    RangeFilter
  end
end

#get_time_stats(time_unit) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 260

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



250
251
252
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 250

def handle_field_stats_result(result)
  build_field_stats transform_field_stats(result)
end

#initialize_distance_rowsObject



375
376
377
378
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 375

def initialize_distance_rows
  values = [0, 1, 5, 10, 50, 100]
  build_rows(values)
end

#initialize_numeric_rowsObject



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

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

#initialize_rowsObject

end



155
156
157
158
159
160
161
162
163
164
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 155

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

#initialize_time_rowsObject



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 273

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_distance?Boolean

Returns:

  • (Boolean)


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

def is_distance?
  false
end

#is_time?Boolean

Returns:

  • (Boolean)


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

def is_time?
  false
end

#reinitializeObject



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

def reinitialize
  prune_unchecked_rows
  field_stats_set if is_distance?
end

#required_row_fieldsObject



412
413
414
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 412

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

#result_nameObject



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

def result_name
  'ranges'
end

#round_to_power_of_ten(n, direction, power) ⇒ Object



393
394
395
396
397
398
399
400
401
402
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 393

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



76
77
78
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 76

def row_class
  RangeFacetRow
end

#select_display_modeObject



404
405
406
407
408
409
410
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 404

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



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 234

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)


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

def timezone_matters?
  true
end

#to_facetObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 121

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

#to_stats_facetObject



87
88
89
90
91
92
93
94
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 87

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



254
255
256
257
258
# File 'lib/mm_es_search/models/abstract_range_facet_model.rb', line 254

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