-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds CSV loader for SearchEvents #54
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't matter that this will create duplicate search events if, for example, the same CSV is loaded twice, right? I assume not given that this is just for dev environments, but I want to make sure I'm not missing anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JPrevost Tagging on ☝️, as it occurred to me that GitHub may not send notifications on comments until a review is complete.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are correct that this would load the same csv multiple times if run multiple times and doesn't try to detect that it has already been loaded. I suspect this is fine, but maybe at minimum we should note?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just pushed a note to that effect. Will approve now so you can merge this as soon as you like, but feel free to modify the note as needed. (I'm not sure what multiple notes for the same method look like in YARD, and for some reason my local docserver isn't showing me the CSV loader task.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the tasks don't show up in Yard sadly. I suspect we can nudge them in there with some config. Multiple notes are fine though from my poking around on other methods.