2 changed files with 62 additions and 0 deletions
@ -0,0 +1,53 @@
|
||||
require "csv" |
||||
|
||||
module Imports |
||||
class VariableDefinitionsService |
||||
attr_reader :path, :count |
||||
|
||||
def initialize(path:) |
||||
@path = path |
||||
@count = 0 |
||||
end |
||||
|
||||
def call |
||||
files = Dir.glob(File.join(@path, "*.csv")) |
||||
files.each do |file| |
||||
process_file(file) |
||||
end |
||||
end |
||||
|
||||
private |
||||
|
||||
def process_file(file) |
||||
file_name = File.basename(file, ".csv") |
||||
parsed_file_name = file_name.split("_") |
||||
log_type = parsed_file_name[0] |
||||
year = "20#{parsed_file_name[2]}".to_i |
||||
|
||||
records_added = 0 |
||||
|
||||
CSV.foreach(file) do |row| |
||||
next if row.empty? |
||||
|
||||
variable = row[0].downcase |
||||
definition = row[1..].join(",") |
||||
next if variable.nil? || definition.nil? |
||||
|
||||
existing_record = CsvVariableDefinition.find_by(variable: variable.strip, definition: definition.strip, log_type:) |
||||
|
||||
if existing_record.nil? |
||||
CsvVariableDefinition.create!( |
||||
variable: variable.strip, |
||||
definition: definition.strip, |
||||
log_type:, |
||||
year:, |
||||
) |
||||
records_added += 1 |
||||
end |
||||
end |
||||
|
||||
Rails.logger.debug "Added #{records_added} variable/definition records for file: #{file_name}. Duplicates excluded." |
||||
@count += records_added |
||||
end |
||||
end |
||||
end |
||||
@ -0,0 +1,9 @@
|
||||
namespace :data_import do |
||||
desc "Add CsvVariableDefinition records for each file in the specified directory" |
||||
task :add_variable_definitions, [:path] => :environment do |_task, args| |
||||
path = Rails.root.join(args[:path]) |
||||
service = Imports::VariableDefinitionsService.new(path:) |
||||
service.call |
||||
Rails.logger.info "CSV Variable Definitions: #{service.count} total records added" |
||||
end |
||||
end |
||||
Loading…
Reference in new issue