Created
May 16, 2017 16:25
-
-
Save mmichael0413/347a26ddfad6553449100a2a948e7a17 to your computer and use it in GitHub Desktop.
ThirdChannel - lookup how many Checkins for the last month have at least one image with geolocation data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :calculate do | |
task checkin_images_geo_data: :environment do | |
headers_success = ['Checkin ID'] | |
headers_error = ['Checkin ID', 'Error'] | |
checkins_with_geo_images = [] | |
other_checkins = [] | |
checkins = Checkin.includes(:submissions, { submissions: :images }) | |
.where('created_at >= ?', (1.month + 1.day).ago.to_datetime) | |
puts "Looking for geolocation data for #{checkins.count} visits with submission images" | |
checkins.each do |checkin| | |
begin | |
images = checkin.submissions.map(&:images).flatten.uniq.compact | |
if images && images.any?{ |i| i.exif && i.exif['latitude'].present? } | |
checkins_with_geo_images << [checkin.id] | |
else | |
other_checkins << [checkin.id, 'No EXIF data found'] | |
end | |
rescue => error | |
other_checkins << [checkin.id, error] | |
end | |
end | |
if checkins_with_geo_images.any? | |
temp_csv_path = "tmp/checkins_with_geo_images_#{DateTime.now.to_i}.csv" | |
puts "Generating CSV with Checkin ids for SubmissionImages with geo data at #{temp_csv_path}" | |
CSV.open(temp_csv_path, 'wb') do |csv| | |
csv << headers_success | |
checkins_with_geo_images.each do |checkin| | |
csv << checkin | |
end | |
end | |
end | |
if other_checkins.any? | |
temp_csv_path = "tmp/checkins_without_geo_images_#{DateTime.now.to_i}.csv" | |
puts "Generating CSV with Checkin ids for SubmissionImages without geo data at #{temp_csv_path}" | |
CSV.open(temp_csv_path, 'wb') do |csv| | |
csv << headers_error | |
other_checkins.each do |checkin| | |
csv << checkin | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment