Skip to content

Commit

Permalink
Fix: upload results skips samples when CSV has whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ysbaddaden committed Jul 20, 2023
1 parent 3f4995b commit 42b2cde
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
13 changes: 6 additions & 7 deletions app/controllers/samples_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,15 @@ def bulk_destroy
end

def bulk_process_csv
uuid_regex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i
uuid_regex = /\b[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}\b/i

params[:csv_files].each do |csv_file|
CSV.open(csv_file.path) do |csv_stream|
csv_stream.each do |row|
sample_id, measured_signal = row[0], row[1]
next unless sample_id&.match(uuid_regex) # non uuids are ignored
sample = Sample.find_by_uuid(sample_id)
unless sample.nil?
sample.measured_signal ||= Float(measured_signal) if measured_signal.present?
csv_stream.each do |(sample_id, measured_signal)|
next unless sample_id&.match(uuid_regex) && measured_signal.present?

if sample = Sample.find_by_uuid(sample_id.strip)
sample.measured_signal ||= Float(measured_signal.strip)
sample.save!
end
end
Expand Down
36 changes: 19 additions & 17 deletions app/views/samples/_upload_results_js.haml
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,32 @@
// Parse the input file
var text = load_event.target.result
var lines = text.split(/\r\n|\r|\n/); // tolerate both Windows and Unix linebreaks
var samples_param = [] // params for GET call
var samples = [] // samples uuids
var duplicates = false
lines.forEach(function(line) {
var row = line.split(',');
if (line != '' && isUUID(row[0])){
if (samples.indexOf(row[0]) == -1){
samples_param.push( 'uuids[]=' + row[0] );
samples.push( row[0] );
}
else {
duplicates = true;
}
}

lines.forEach(function(line) {
var row = line
.split(",")
.map((part) => part.trim());
if (!isUUID(row[0])) return;

if (samples.indexOf(row[0]) == -1){
samples.push(row[0]);
} else {
duplicates = true;
}
});

// Retrieve found uuids
var samples_params = samples
.map((uuid) => "uuids[]=" + uuid)
.join("&");
var context = getUrlParameter('context');
var url = '/samples/existing_uuids?context='+context+"&"+samples_param.join("&")
var url = '/samples/existing_uuids?context='+context+"&"+samples_params;
var found = await fetch(url)
.then((response) => response.json())
.then((r) => {
return r.message;
})
.then((r) => r.message);

// Compute not found ones
var not_found = samples.filter(function(x) {
return found.indexOf(x) < 0;
Expand Down

0 comments on commit 42b2cde

Please sign in to comment.