Skip to content
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

Refactor ZipGenotypingFiles #527

Merged
merged 6 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ Style/FrozenStringLiteralComment:

Metrics/LineLength:
Max: 100

Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'
7 changes: 5 additions & 2 deletions app/controllers/phenotypes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,11 @@ def feed
end

def get_genotypes
Sidekiq::Client.enqueue(Zipgenotypingfiles, params[:id],
params[:variation], current_user.email)
ZipGenotypingFiles.perform_async(
params[:id],
params[:variation],
current_user.email
)
@phenotype = Phenotype.find(params[:id])
@variation = params[:variation]
end
Expand Down
81 changes: 81 additions & 0 deletions app/workers/zip_genotyping_files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

class ZipGenotypingFiles
include Sidekiq::Worker
sidekiq_options queue: :zipgenotyping, retry: 5, unique: true

def perform(phenotype_id, variation, target_address)
@phenotype = Phenotype.find(phenotype_id)
@variation = variation
@target_address = target_address
@time = Time.current.to_s.tr(':', '_')

if genotypes.empty?
send_no_results
else
zip_genotypes
send_results
end
end

def zip_genotypes
return if File.exist?(zip_file_path)

Zip::File.open(zip_file_path, Zip::File::CREATE) do |zipfile|
genotypes.each do |genotype|
zipfile.add(
zipped_file_name(genotype),
Rails.root.join('public', 'data', genotype.fs_filename)
)
end
end
File.chmod(0o777, zip_file_path)
end

def send_results
UserMailer.genotyping_results(
target_address,
zip_file_path.to_s,
phenotype.characteristic,
variation
).deliver_later
end

def zipped_file_name(genotype)
"user#{genotype.user_id}_file#{genotype.id}_yearofbirth" \
"#{genotype.user.yearofbirth}_sex#{genotype.user.sex}.#{genotype.filetype}.txt"
end

def send_no_results
UserMailer.no_genotyping_results(
target_address,
phenotype.characteristic,
variation
).deliver_later
end

private

attr_reader :phenotype, :variation, :target_address, :time

def genotypes
@genotypes ||= user_phenotypes.includes(:user).flat_map do |user_phenotype|
user_phenotype.user.genotypes
end
end

def user_phenotypes
UserPhenotype
.where(phenotype_id: phenotype.id)
.search(variation)
end

def zip_file_path
@zip_file_path ||= Rails.root.join(
'public',
'data',
'zip',
"#{phenotype.id}.#{time.to_s.tr(' ', '_')}.zip"
)
end
end
37 changes: 0 additions & 37 deletions app/workers/zipgenotypingfiles.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/factories/achievements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

FactoryBot.define do
factory :achievement do
award 'Foooooooo'
award { 'Foooooooo' }
end
end
4 changes: 2 additions & 2 deletions spec/factories/fitbit_activities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

FactoryBot.define do
factory :fitbit_activity do
steps 100
floors 1
steps { 100 }
floors { 1 }
end
end
4 changes: 2 additions & 2 deletions spec/factories/fitbit_bodies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

FactoryBot.define do
factory :fitbit_body do
weight '100'
bmi '200'
weight { '100' }
bmi { '200' }
end
end
8 changes: 4 additions & 4 deletions spec/factories/fitbit_sleeps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

FactoryBot.define do
factory :fitbit_sleep do
minutes_asleep 480
minutes_awake 10
number_awakenings 1
minutes_to_sleep 10
minutes_asleep { 480 }
minutes_awake { 10 }
number_awakenings { 1 }
minutes_to_sleep { 10 }
end
end
2 changes: 1 addition & 1 deletion spec/factories/genome_gov_papers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

FactoryBot.define do
factory :genome_gov_paper do
title 'A Genome.gov Paper'
title { 'A Genome.gov Paper' }
end
end
2 changes: 1 addition & 1 deletion spec/factories/genotypes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

FactoryBot.define do
factory :genotype do
genotype_file_name 'foo.txt'
genotype_file_name { 'foo.txt' }
user
end
end
10 changes: 5 additions & 5 deletions spec/factories/mendeley_papers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

FactoryBot.define do
factory :mendeley_paper do
title 'Musterstudie'
title { 'Musterstudie' }
uuid { UUIDTools::UUID.random_create.to_s }
first_author 'Max Mustermann'
mendeley_url 'http://example.com'
doi '10.1000/182'
pub_year 2013
first_author { 'Max Mustermann' }
mendeley_url { 'http://example.com' }
doi { '10.1000/182' }
pub_year { 2013 }
end
end
6 changes: 3 additions & 3 deletions spec/factories/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
FactoryBot.define do
factory :message do
user
sent false
subject 'HELLO WORLD'
body 'THIS IS AN AWESOME MESSAGE'
sent { false }
subject { 'HELLO WORLD' }
body { 'THIS IS AN AWESOME MESSAGE' }
end
end
2 changes: 1 addition & 1 deletion spec/factories/phenotypes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

FactoryBot.define do
factory :phenotype do
characteristic 'Penis length'
characteristic { 'Penis length' }

factory :phenotype_with_users do
after :create do |t|
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/picture_phenotypes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

FactoryBot.define do
factory :picture_phenotype do
characteristic 'Eye color'
characteristic { 'Eye color' }
end
end
2 changes: 1 addition & 1 deletion spec/factories/plos_papers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

FactoryBot.define do
factory :plos_paper do
title 'A PLOS Paper'
title { 'A PLOS Paper' }
end
end
8 changes: 4 additions & 4 deletions spec/factories/snp_comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

FactoryBot.define do
factory :snp_comment do
comment_text 'This is a great SNP!'
subject 'Great!'
user_id 1
snp_id 1
comment_text { 'This is a great SNP!' }
subject { 'Great!' }
user_id { 1 }
snp_id { 1 }
end
end
2 changes: 1 addition & 1 deletion spec/factories/snpedia_papers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

FactoryBot.define do
factory :snpedia_paper do
url 'http://www.snpedia.com/index.php/Rs1234(A;C)'
url { 'http://www.snpedia.com/index.php/Rs1234(A;C)' }
end
end
6 changes: 3 additions & 3 deletions spec/factories/snps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
sequence(:name) { |i| "rs#{i}" }
sequence(:position) { |i| i }
sequence(:chromosome) { |i| i }
genotype_frequency('AA' => 1)
allele_frequency('A' => 0, 'T' => 0, 'G' => 0, 'C' => 0)
ranking 0
genotype_frequency { { 'AA' => 1 } }
allele_frequency { { 'A' => 0, 'T' => 0, 'G' => 0, 'C' => 0 } }
ranking { 0 }
end
end
2 changes: 1 addition & 1 deletion spec/factories/user_phenotypes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
factory :user_phenotype do
association :user
association :phenotype
variation 'pink'
variation { 'pink' }
end
end
2 changes: 1 addition & 1 deletion spec/factories/user_picture_phenotypes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

FactoryBot.define do
factory :user_picture_phenotype do
variation 'pink'
variation { 'pink' }
end
end
2 changes: 1 addition & 1 deletion spec/factories/user_snps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

FactoryBot.define do
factory :user_snp do
local_genotype 'AG'
local_genotype { 'AG' }
genotype
user
snp
Expand Down
10 changes: 5 additions & 5 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

FactoryBot.define do
factory :user do
name 'Dogbert'
name { 'Dogbert' }
sequence(:email) { |i| "fubert#{i}@example.org" }
password 'strengjeheim'
password_confirmation 'strengjeheim'
sex 'yes please'
yearofbirth '1970'
password { 'strengjeheim' }
password_confirmation { 'strengjeheim' }
sex { 'yes please' }
yearofbirth { '1970' }
end
end
31 changes: 0 additions & 31 deletions spec/integration/zipgenotypingfiles_spec.rb

This file was deleted.

Loading