-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from MITLibraries/tco35-csv-data-loader
Adds CSV loader for SearchEvents
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'csv' | ||
|
||
# Loaders can bulk load data | ||
namespace :search_events do | ||
# csv loader can bulk load SearchEvents and Terms. | ||
# | ||
# @note For use in development environments only. Duplicate search events will be created if the same CSV is loaded | ||
# multiple times. | ||
# | ||
# @note the csv should be formated as `term phrase`, `timestamp`. A dataclip is available that can export in this | ||
# format. | ||
# @example | ||
# bin/rails search_events:csv_loader['local_path_to_file.csv', 'some-source-to-use-for-all-loaded-records'] | ||
# | ||
# @param path [String] local file path to a CSV file to load | ||
# @param source [String] source name to load the data under | ||
desc 'Load search_events from csv' | ||
task :csv_loader, %i[path source] => :environment do |_task, args| | ||
raise ArgumentError.new, 'Path is required' unless args.path.present? | ||
raise ArgumentError.new, 'Source is required' unless args.source.present? | ||
|
||
Rails.logger.info("Loading data from #{args.path}") | ||
|
||
CSV.foreach(args.path) do |row| | ||
term = Term.create_or_find_by!(phrase: row.first) | ||
term.search_events.create!(source: args.source, created_at: row.last) | ||
end | ||
end | ||
end |