Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Generate Restaurant Popular Zone

missing = []

missing_tags = []


def filter_tags_by_distance(restaurant, distance_in_kms)
  tags_with_location = RestaurantTag.where.not(lat: nil, lng: nil).pluck(:id, :lat, :lng).to_h { |id, lat, lng| [id, [lat, lng]] }


  return tags_with_location.keys if tags_with_location.empty?

  tags_distance = tags_with_location.transform_values do |location|
    find_distance_between2(restaurant.to_coordinates, location)
  end

  delete_restaurant_tag_restaurant(restaurant, tags_distance)

  tags_distance.select { |_tag_id, distance| distance <= distance_in_kms }.keys
end


def find_distance_between2(coordinate1, coordinate2)
  Geocoder::Calculations.distance_between(coordinate1, coordinate2)
end

def delete_restaurant_tag_restaurant(restaurant, tags_distance)
  ActiveRecord::Base.transaction do
    tag_ids = tags_distance.select { |_k, v| v }.keys
    restaurant.with_lock do
      restaurant.restaurant_tags_restaurants.where(restaurant_tag_id: tag_ids).delete_all
    end
  end
end

def find_nearest_popular_zone_id(restaurant)
  popular_zones = restaurant.restaurant_tags.where_category('PopularZone')
  return if popular_zones.blank?

  distances = popular_zones.map do |tag|
    {
      tag_id: tag.id,
      distance: find_distance_between(tag.lat, tag.lng, restaurant.lat, restaurant.lng),
    }

  end


  distances.min_by { |hsh| hsh[:distance] }[:tag_id]
end


def find_distance_between(tag_lat, tag_lng, restaurant_lat, restaurant_lng)
  return 1000 if tag_lat.blank? || tag_lng.blank? || restaurant_lat.blank? || restaurant_lng.blank?

  Geocoder::Calculations.distance_between([tag_lat, tag_lng], [restaurant_lat, restaurant_lng])
end

distance_in_kms = AdminSetting.popular_zone_distance_limit_max_x_km.to_i

Restaurant.find_each do |restaurant|
  ActiveRecord::Base.transaction do
    today = Time.now_in_tz(restaurant.time_zone).to_date
    not_expired = restaurant.expiry_date.present? && restaurant.expiry_date >= today

    filtered_tags = filter_tags_by_distance(restaurant, distance_in_kms)
    if filtered_tags.empty?
      missing_tags.push restaurant

      next
    end

    filtered_tags.map do |restaurant_tag_id|
      rt = RestaurantTag.find restaurant_tag_id
      rt.restaurants << restaurant
    rescue 
      next
    end


    primary_location = restaurant.primary_tag_by_category('PopularZone')

    nearest_popular_zone_id = find_nearest_popular_zone_id(restaurant)
    if nearest_popular_zone_id == primary_location&.restaurant_tag_id || nearest_popular_zone_id.blank?
      missing.push restaurant
      next
    end

    begin
    new_popular_zone_tag = restaurant.primary_tags.build(restaurant_tag_id: nearest_popular_zone_id)
    new_popular_zone_tag.save
    rescue 
    end

    CleanCompactRestaurantCacheWorker.perform_async(restaurant.id)
  end
end