Skip to content

Instantly share code, notes, and snippets.

@deathweaselx86
Forked from mltsy/reprocess_hours.rake
Last active May 15, 2016 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deathweaselx86/61545a74fe6dc99189a32f968af79852 to your computer and use it in GitHub Desktop.
Save deathweaselx86/61545a74fe6dc99189a32f968af79852 to your computer and use it in GitHub Desktop.
Takes a filename of an event log (plain text, not gz), finds all the verification events in that file and reprocesses the hours.
desc "open a given log file, and extract/save hours from all Business Verification records logged"
task :reprocess_hours, [:filename] => :environment do |t, args|
lines_read = lines_processed = lines_skipped = lines_failed = 0
filename = args[:filename]
puts "Reading lines from #{filename}..."
in_log = File.open(filename)
in_log.each do |line|
lines_read += 1
json = JSON.parse(line)
if json['scope'] == ['BusinessVerifier']
business = {}
business['uuid'] = json['business']['uuid']
business['hours'] = json['hours']
business['social_media_config'] = json['social_media_config']
if process(business)
lines_processed+=1
else
lines_failed +=1
end
else
lines_skipped += 1
end
puts "Read: #{lines_read} (skipped: #{lines_skipped}, processed: #{lines_processed}, failed: #{lines_failed})" if lines_read % 100 == 0
end
end
def process(biz)
uuid = biz['uuid']
business = Business.with_deleted.where(uuid: uuid).first
return false if business.nil?
hours = AdvertiserHoursOfOperation.where(advertiser_source_id: business.id).first_or_create
social = SocialMediaConfig.where(business_id: business.id).first_or_create
# fix hours
biz['hours'].each do |key, value|
begin
hours.send("#{key}=", DateTime.parse(value))
rescue
hours.send("#{key}=", value)
end
end
hours.save!
# fix social media
if biz['social_media_config'].has_key? 'yelp_id' && biz['social_media_config']['yelp_id'].present?
yelp_id = biz['social_media_config']['yelp_id']
social.update_attributes(yelp_id: yelp_id )
end
puts "Updated #{uuid}"
return true
rescue StandardError => e
puts "ERROR processing #{uuid} - #{e.class}: #{e.message}"
return false
end
@driverdan
Copy link

The if statement on line 40 does the same thing in both cases. Is this intentional? Should the statement just be removed?

@driverdan
Copy link

Line #48 checks for key yelp but then uses yelp_id. Does it need to check for yelp?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment