-
Notifications
You must be signed in to change notification settings - Fork 52
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
362 add and edit attributes in sample types #2032
base: main
Are you sure you want to change the base?
Changes from 27 commits
cfd7789
133718f
9c5aec7
90ac8b4
037521b
caeab80
8ce597b
d78ea79
fd18c8b
36e355a
f894152
fd3a532
a631ae3
662fbdf
f4a59ac
8b20eaf
4a079c1
2547489
928ffc4
d44fafc
3cb7b13
fc0a0b0
675a39b
190cd76
60763a1
d107a7b
07da6a1
2dadb38
ed0d6bd
520beb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
class UpdateSampleMetadataJob < ApplicationJob | ||
queue_with_priority 1 | ||
queue_as QueueNames::SAMPLES | ||
|
||
def perform(sample_type, attribute_changes = [], user) | ||
Seek::Samples::SampleMetadataUpdater.new(sample_type, attribute_changes, user).update_metadata | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. General question, but what happens if someone views a sample before the job has finished running? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to test this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On your local instance you could update a sample type without any workers running. Alternatively, add a |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module Seek | ||
module Samples | ||
|
||
class SampleMetadataUpdateException < StandardError; end | ||
|
||
# Class to handle the updating of sample metadata after updating Sample type | ||
class SampleMetadataUpdater | ||
def initialize(sample_type, attribute_changes, user) | ||
@sample_type = sample_type | ||
@attribute_change_maps = attribute_changes | ||
@user = user | ||
end | ||
|
||
def update_metadata | ||
return if @attribute_change_maps.blank? || @sample_type.samples.blank? || @sample_type.nil? || @user.nil? | ||
|
||
User.with_current_user(@user) do | ||
@sample_type.samples.each do |sample| | ||
kdp-cloud marked this conversation as resolved.
Show resolved
Hide resolved
|
||
metadata = JSON.parse(sample.json_metadata) | ||
# Update the metadata keys with the new attribute titles | ||
@attribute_change_maps.each do |change| | ||
metadata[change[:new_title]] = metadata.delete(change[:old_title]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If someone creates a new sample before the job finishes, this line will wipe out the changed attributes. |
||
end | ||
sample.update_column(:json_metadata, metadata.to_json) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class UpdateSampleMetadataJobTest < ActiveSupport::TestCase | ||
def setup | ||
@person = FactoryBot.create(:person) | ||
@project = @person.projects.first | ||
@sample_type = FactoryBot.create(:simple_sample_type, project_ids: [@project.id], contributor: @person, policy: FactoryBot.create(:public_policy)) | ||
(1..10).each do |_i| | ||
FactoryBot.create(:sample, sample_type: @sample_type, contributor: @person) | ||
end | ||
end | ||
|
||
def teardown | ||
# Do nothing | ||
end | ||
|
||
test 'perform' do | ||
User.with_current_user(@person.user) do | ||
UpdateSampleMetadataJob.new.perform(@sample_type, [], @person.user) | ||
end | ||
end | ||
|
||
test 'Check sample metadata after updating the attribute title' do | ||
User.with_current_user(@person.user) do | ||
@sample_type.sample_attributes.first.update!(title: 'new title') | ||
attribute_change_maps = [{id: @sample_type.sample_attributes.first.id, old_title: 'the_title', new_title: 'new title' }] | ||
assert_equal @sample_type.sample_attributes.first.title, 'new title' | ||
refute_equal @sample_type.sample_attributes.first.title, 'the_title' | ||
UpdateSampleMetadataJob.new.perform(@sample_type, attribute_change_maps, @person.user) | ||
@sample_type.samples.each do |sample| | ||
json_metadata = JSON.parse sample.json_metadata | ||
assert json_metadata.keys.include?('new title') | ||
refute json_metadata.keys.include?('the_title') | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would happen if 2 updates were made to a sample type in a short time?
I think you may need a mechanism for "locking" the sample type to further changes until the update job has completed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't know that was possible. Has it already been done somewhere else in the source code?