app/models/random_hierarchy_image.rb
# TODO - rewrite this, it's WAY too complex. Most of these features are unused
# and unnecessary. Also, it should just keep looking for random images until
# it finds enough... not relying on the "limit * 3" thing to get more than it
# needs. Of course, that algorithm will need to watch for endless loops.
rewrite this, it's WAY too complex. Most of these features are unused
and unnecessary. Also, it should just keep looking for random images until
it finds enough... not relying on the "limit * 3" thing to get more than it
needs. Of course, that algorithm will need to watch for endless loops.
rewrite this it ' s WAY too complex . Most of these features are unused and unnecessary . Also it should just keep looking for random images until it finds enough ... not relying on the " limit * 3 " thing to get more than it needs . Of course that algorithm will need to watch for endless loops .
def random_set(limit = 10, hierarchy = nil, options = {})
      options[:size] ||= '130_130'
      options[:language] ||= Language.english
      if hierarchy
        starting_id = rand(hierarchy_count(hierarchy) - limit).floor
        starting_id = 0 if starting_id > (hierarchy_count(hierarchy) - limit)
        starting_id = starting_id + min_id()
      else
        starting_id = rand(max_id() - limit).floor
        starting_id = 0 if starting_id > (max_id() - limit)
      end
      hierarchy_condition = hierarchy ? "AND hierarchy_id=#{hierarchy.id}" : ""
      random_image_result = if $HOMEPAGE_MARCH_RICHNESS_THRESHOLD
        RandomHierarchyImage.joins(:taxon_concept_metrics, :taxon_concept).
          where(["random_hierarchy_images.id > ? AND richness_score > ? AND published = 1 #{hierarchy_condition} AND supercedure_id = 0",
                starting_id, $HOMEPAGE_MARCH_RICHNESS_THRESHOLD]).limit(limit * 3)
                            else
        RandomHierarchyImage.joins(:taxon_concept).
          where(["random_hierarchy_images.id > ? AND published=1 #{hierarchy_condition} AND supercedure_id = 0", starting_id]).limit(limit * 3)
                            end
      used_concepts = {}
      random_images = []
      random_image_result.each do |ri|
        next if !used_concepts[ri.taxon_concept_id].nil?
        random_images << ri
        used_concepts[ri.taxon_concept_id] = true
        break if random_images.length >= limit
      end
      RandomHierarchyImage.preload_associations(random_images,
        [ { taxon_concept: [
            { preferred_entry: { hierarchy_entry: [ :hierarchy, { name: [ :canonical_form, :ranked_canonical_form ] } ] } },
            { taxon_concept_exemplar_image: :data_object },
            { preferred_common_names: :name } ] } ])
      random_images = random_set(limit, nil, size: options[:size]) if random_images.blank? && hierarchy
      Rails.logger.warn "Found no Random Taxa in the database (#{starting_id}, #{limit})" if random_images.blank?
      return random_images
    end
def random_set ( limit = 10 , hierarchy = nil , options = { } ) options [ :size ] ||= '130_130' options [ :language ] ||= Language . english if hierarchy starting_id = rand ( hierarchy_count ( hierarchy ) - limit ) . floor starting_id = 0 if starting_id > ( hierarchy_count ( hierarchy ) - limit ) starting_id = starting_id + min_id ( ) else starting_id = rand ( max_id ( ) - limit ) . floor starting_id = 0 if starting_id > ( max_id ( ) - limit ) end hierarchy_condition = hierarchy ? "AND hierarchy_id=#{hierarchy.id}" : "" random_image_result = if $HOMEPAGE_MARCH_RICHNESS_THRESHOLD RandomHierarchyImage . joins ( :taxon_concept_metrics , :taxon_concept ) . where ( [ "random_hierarchy_images.id > ? AND richness_score > ? AND published = 1 #{hierarchy_condition} AND supercedure_id = 0" , starting_id , $HOMEPAGE_MARCH_RICHNESS_THRESHOLD ] ) . limit ( limit * 3 ) else RandomHierarchyImage . joins ( :taxon_concept ) . where ( [ "random_hierarchy_images.id > ? AND published=1 #{hierarchy_condition} AND supercedure_id = 0" , starting_id ] ) . limit ( limit * 3 ) end used_concepts = { } random_images = [ ] random_image_result . each do | ri | next if ! used_concepts [ ri . taxon_concept_id ] . nil? random_images << ri used_concepts [ ri . taxon_concept_id ] = true break if random_images . length >= limit end RandomHierarchyImage . preload_associations ( random_images , [ { taxon_concept : [ { preferred_entry : { hierarchy_entry : [ :hierarchy , { name : [ :canonical_form , :ranked_canonical_form ] } ] } } , { taxon_concept_exemplar_image : :data_object } , { preferred_common_names : :name } ] } ] ) random_images = random_set ( limit , nil , size : options [ :size ] ) if random_images . blank? && hierarchy Rails . logger . warn "Found no Random Taxa in the database (#{starting_id}, #{limit})" if random_images . blank? return random_images end
TODO - rewrite this, it's WAY too complex.
TODO - rewrite this it ' s WAY too complex .
# This next line only applies when there are very few RandomTaxa. # This next line only applies when there are very few RandomTaxa. # this query now grabs all the metadata we'll need including: # sci_name, common_name, taxon_concept_id, object_cache_url # it also looks for twice the limit as we still have some concepts with more than one preferred common name
limit
<not_specific>
hierarchy
<not_specific>
options
<not_specific>