Skip to content

Instantly share code, notes, and snippets.

@CodySwannGT
Last active January 21, 2017 00:01
Show Gist options
  • Save CodySwannGT/835f8689577af2286cf0b64499512bf9 to your computer and use it in GitHub Desktop.
Save CodySwannGT/835f8689577af2286cf0b64499512bf9 to your computer and use it in GitHub Desktop.
Without S3 vs With S3
#!/bin/bash
FILES=/home/qualisauto/*_checks.csv
check_file=/home/qualisauto/checks_combined.csv
touch $check_file
#### If there are any files at $FILES, then combine them into one file named by $check_file
if ls $FILES 1> /dev/null 2>&1; then
echo "WE got files"
cat $FILES > $check_file
fi
### if $check_file isn't empty
if [ -s $check_file ]
then
export DATE=`date "+%Y-%m-%dT%H:%M:%S"`
sort $check_file > /home/qualisauto/new_checks #### sort all the lines within $check_file and pipe them out to a file called 'new_checks'
name=$(basename "$check_file") ### get whatever the actual file name (without the path)
aws s3 mv $check_file s3://com-gunnertech-qualis-production/home/qualisauto/saved/$name-$DATE ### push it to S3
#### I'm not sure what this dance is all about, but it's a straight copy/paste from the old script
cp /home/qualisauto/new_checks /home/qualisauto/unsorted_checks
rm /home/qualisauto/old_checks
mv /home/qualisauto/new_checks /home/qualisauto/old_checks
#### /end song and dance
### if unsorted_checks isn't empty
if [ -s /home/qualisauto/unsorted_checks ]
then
#### I have no idea what those flags do, but sort the file and pipe it to sorted_checks
sort -t, -rk7 /home/qualisauto/unsorted_checks > /home/qualisauto/sorted_checks
rm /home/qualisauto/unsorted_checks
### move the previously named "sorted_checks" to and archived location
aws s3 mv s3://com-gunnertech-qualis-production/home/qualisauto/sorted_checks s3://com-gunnertech-qualis-production/home/qualisauto/saved/sorted_checks_$DATE
### move the new local "sorted_checks" up to s3
aws s3 mv /home/qualisauto/sorted_checks s3://com-gunnertech-qualis-production/home/qualisauto/sorted_checks
fi
fi
### Clean up our mess
if [ -e $check_file ]
then
echo "Removing combined check file"
rm $check_file
fi
### Clean up our mess
if ls $FILES 1> /dev/null 2>&1; then
echo "WE deleting files"
rm $FILES
fi
### If there is an s3 object with key == CHECK_FILE_PATH, copy the contents of that object to response_target (file_name)
### At that point, file_name becomes identical to CHECK_FILE_PATH from "without-s3"
### The only other difference is we delete the temp file when we're done with it
def self.load_mas_data
Rails.logger.info "#{Time.now} Load Checks"
voided_checks = Hash.new
file_name = "/tmp/checks"
resp = S3.get_object(
bucket: ENV['S3_BUCKET'],
key: CHECK_FILE_PATH,
response_target: file_name
) rescue nil
if resp
CSV.foreach(file_name) do |row|
if row[6] == "R" && voided_checks[row[0]].blank?
voided_checks[row[0] + row[1] + row[3]] = (row[4].to_f * 100).to_i
check = Check.find_by_check_number(row[0].gsub(/^0*/, ""))
check.destroy unless check.blank?
end
unless row[1].blank? || !row[6].in?(%w(A E))
puts("\n\n*** Importing Check ##{row[0].gsub(/^0*/, "")} ***\n\n")
Rails.logger.info("\n\n*** Importing Check ##{row[0].gsub(/^0*/, "")} ***\n\n")
if voided_checks[row[0] + row[1] + row[3]].nil?
self.paid_item(row)
else
voided_checks[row[0] + row[1] + row[3]] = nil if voided_checks[row[0] + row[1] + row[3]] == ((row[4].to_f * 100).to_i) * -1
end
end
end
File.delete(file_name)
end
end
### If there is a file named CHECK_FILE_PATH, loop over that file
def self.load_mas_data
Rails.logger.info "#{Time.now} Load Checks"
voided_checks = Hash.new
if File.exists?(CHECK_FILE_PATH)
CSV.foreach(CHECK_FILE_PATH) do |row|
if row[6] == "R" && voided_checks[row[0]].blank?
voided_checks[row[0] + row[1] + row[3]] = (row[4].to_f * 100).to_i
check = Check.find_by_check_number(row[0].gsub(/^0*/, ""))
check.destroy unless check.blank?
end
unless row[1].blank? || !row[6].in?(%w(A E))
Rails.logger.info("\n\n*** Importing Check ##{row[0].gsub(/^0*/, "")} ***\n\n")
if voided_checks[row[0] + row[1] + row[3]].nil?
self.paid_item(row)
else
voided_checks[row[0] + row[1] + row[3]] = nil if voided_checks[row[0] + row[1] + row[3]] == ((row[4].to_f * 100).to_i) * -1
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment