You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.3 KiB
54 lines
1.3 KiB
4 months ago
|
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
|