Skip to content

Commit

Permalink
Add temporary url_loader for search events
Browse files Browse the repository at this point in the history
This is an affordance to set up the demonstration for Wednesday
  • Loading branch information
matt-bernhardt committed Aug 26, 2024
1 parent 08d6de7 commit 3c6f358
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/tasks/search_event_loader.rake
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,26 @@ namespace :search_events do
term.search_events.create!(source: args.source, created_at: row.last)
end
end

desc 'Load search_events from url'
task :url_loader, %i[addr source] => :environment do |_task, args|
raise ArgumentError.new, 'URL is required' if args.addr.blank?
raise ArgumentError.new, 'Source is required' if args.source.blank?

Rails.logger.info("Term count before import: #{Term.count}")
Rails.logger.info("SearchEvent count before import: #{SearchEvent.count}")

url = URI.parse(args.addr)
Rails.logger.info("Loading data from #{url}")

file = url.open.read
data = CSV.parse(file)
data.each do |row|
term = Term.create_or_find_by!(phrase: row.first)
term.search_events.create!(source: args.source, created_at: row.last)
end

Rails.logger.info("Term count after import: #{Term.count}")
Rails.logger.info("SearchEvent count after import: #{SearchEvent.count}")
end
end

0 comments on commit 3c6f358

Please sign in to comment.