-
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
Open
kdp-cloud
wants to merge
30
commits into
seek4science:main
Choose a base branch
from
ELIXIR-Belgium:362-add-and-edit-attributes-in-sample-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+167
−98
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
cfd7789
Add sample metadata updater module
kdp-cloud 133718f
Add Sample Metadata upater job
kdp-cloud 9c5aec7
Add after_action for updating the sample json metadata
kdp-cloud 90ac8b4
Skip if sample metadata keys correspond with sample attribute titles
kdp-cloud 037521b
Remove add new attribute constraint
kdp-cloud caeab80
Don't start job if there are samples
kdp-cloud 8ce597b
typo
kdp-cloud d78ea79
Make unit editable
kdp-cloud fd18c8b
Fix sample types controller test
kdp-cloud 36e355a
test for adding new attribute
kdp-cloud f894152
linting
kdp-cloud fd3a532
Add UpdateSampleMetadataTest
kdp-cloud a631ae3
Add test
kdp-cloud 662fbdf
Fix the Sample metadata updater
kdp-cloud f4a59ac
Check user
kdp-cloud 8b20eaf
Change `allow_name_change?`
kdp-cloud 4a079c1
Add explaination
kdp-cloud 2547489
Fix test
kdp-cloud 928ffc4
Change existing title constraints test
kdp-cloud d44fafc
Add test for changing name of attribute.
kdp-cloud 3cb7b13
Allow adding optional attribute to existing sample type
kdp-cloud fc0a0b0
Adjusted error message if attribute.new_record?
kdp-cloud 675a39b
Restructure constraints
kdp-cloud 190cd76
Put job in with_current_user block
kdp-cloud 60763a1
Split test in optional and mandatory test
kdp-cloud d107a7b
check if samples instead of returning false
kdp-cloud 07da6a1
test should fail if samples
kdp-cloud 2dadb38
Remove `allow_name_change?` completely
kdp-cloud ed0d6bd
Revert deletion of `allow_new_attrbute?`
kdp-cloud 520beb0
Update sample metadata in batches
kdp-cloud File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class UpdateSampleMetadataJob < ApplicationJob | ||
queue_with_priority 1 | ||
queue_as QueueNames::SAMPLES | ||
|
||
def perform(sample_type, user, attribute_changes = []) | ||
sample_type.samples.in_batches(of: 1000) do |samples| | ||
Seek::Samples::SampleMetadataUpdater.new(samples, user, attribute_changes).update_metadata | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(samples, user, attribute_changes) | ||
@samples = samples | ||
@user = user | ||
@attribute_change_maps = attribute_changes | ||
end | ||
|
||
def update_metadata | ||
return if @attribute_change_maps.blank? || @samples.blank? || @user.nil? | ||
|
||
User.with_current_user(@user) do | ||
@samples.each do |sample| | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# 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.perform_now(@sample_type, @person.user, attribute_change_maps) | ||
@sample_type.reload | ||
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?