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

Filter and sort sample queries #2113

Draft
wants to merge 4 commits into
base: seek-1.16
Choose a base branch
from
Draft
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
19 changes: 19 additions & 0 deletions app/controllers/samples_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,25 @@ def query
template_id: params[:output_template_id] }, output_template_attribute)
end

####################################################################################################################
# Filtering by date range
####################################################################################################################
if params[:filter_begin_date_created].present?
@result = @result.select { |s| s.created_at >= params[:filter_begin_date_created].to_date }
end

if params[:filter_end_date_created].present?
@result = @result.select { |s| s.created_at <= params[:filter_end_date_created].to_date }
end

if params[:filter_begin_date_updated].present?
@result = @result.select { |s| s.updated_at >= params[:filter_begin_date_updated].to_date }
end

if params[:filter_end_date_updated].present?
@result = @result.select { |s| s.updated_at <= params[:filter_end_date_updated].to_date }
end

@result = @result.select { |s| (project_ids & s.project_ids).any? } if project_ids.present?
@total_samples = @result.length
@result = @result.any? ? @result.authorized_for('view') : []
Expand Down
10 changes: 9 additions & 1 deletion app/views/samples/_table_view.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@
<th <%= 'data-title-column="true" ' if (attribute.is_title && link) -%>data-accessor-name="<%= attribute.accessor_name -%>" data-column-type="<%= attribute.sample_attribute_type.base_type-%>" data-search-title="<%= attribute.title %>">
<%= sample_attribute_display_title(attribute) %>
</th>
<% end %>
<% end %>
<th>
Date created
</th>
<th>
Date updated
</th>
</tr>
<% end %>
<% end %>
Expand Down Expand Up @@ -57,6 +63,8 @@
<td><%= display_attribute(sample, attribute, link: link) %></td>
<% end %>
<% end %>
<td><%= sample.created_at %></td>
<td><%= sample.updated_at %></td>
<% end %>
<% end %>
</tbody>
Expand Down
28 changes: 27 additions & 1 deletion app/views/samples/query_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,29 @@
<input id="output_attribute_value" class="form-control"/>
</div>
</div>
<% end %>
<div class="row">
<div class="col-md-4">
<h4>
Select date range
</h4>
</div>
</div>
<div class="row">
<div class="col-md-4">
<label>Begin date created</label>
<input id="filter_begin_date_created" type="date" class="form-control"></input >
<label>End date created</label>
<input id="filter_end_date_created" type="date" class="form-control"></input >
</div>
<div class="col-md-4">
<label>Begin date updated</label>
<input id="filter_begin_date_updated" type="date" class="form-control"></input >
<label>End date updated</label>
<input id="filter_begin_date_updated" type="date" class="form-control"></input >
</div>
</div>

<% end %>

<div class="row">
<div class="col-md-4" style="margin-bottom:20px">
Expand Down Expand Up @@ -187,6 +209,10 @@
output_template_id: $j("#output_template").val(),
output_attribute_id: $j("#output_template_attribute").val(),
output_attribute_value: $j("#output_attribute_value").val(),
filter_begin_date_created: $j("#filter_begin_date_created").val(),
filter_end_date_created: $j("#filter_end_date_created").val(),
filter_begin_date_updated: $j("#filter_begin_date_updated").val(),
filter_end_date_updated: $j("#filter_end_date_updated").val()
}
$j.ajax({
url: "<%=query_samples_path%>",
Expand Down
111 changes: 111 additions & 0 deletions test/functional/samples_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,117 @@ def rdf_test_object
end
end

test 'should filter samples according to the dates created and updated' do
with_config_value(:isa_json_compliance_enabled, true) do
person = FactoryBot.create(:person)
project = FactoryBot.create(:project)

login_as(person)

template1 = FactoryBot.create(:isa_source_template)

sample_type = FactoryBot.create(:simple_sample_type, contributor: person, project_ids: [project.id],
title: 'Source sample type', template_id: template1.id)
sample_type.create_sample_attributes_from_isa_template(template1)

# Create Samples at three different dates
travel_to(Time.zone.local(2024, 9, 1, 12, 0, 0))
source1 = FactoryBot.create :sample, title: 'Source 1', sample_type: sample_type, project_ids: [project.id], contributor: person,
data: { 'Source Name': 'Source 1', 'Source Characteristic 1': 'Source Characteristic A', 'Source Characteristic 2': "Cox's Orange Pippin" }

travel_to(Time.zone.local(2024, 9, 8, 12, 0, 0))
source2 = FactoryBot.create :sample, title: 'Source 2', sample_type: sample_type, project_ids: [project.id], contributor: person,
data: { 'Source Name': 'Source 2', 'Source Characteristic 1': 'Source Characteristic B', 'Source Characteristic 2': "Cox's Orange Pippin" }

travel_to(Time.zone.local(2024, 9, 15, 12, 0, 0))
source3 = FactoryBot.create :sample, title: 'Source 3', sample_type: sample_type, project_ids: [project.id], contributor: person,
data: { 'Source Name': 'Source 3', 'Source Characteristic 1': 'Source Characteristic C', 'Source Characteristic 2': "Cox's Orange Pippin" }

travel_back

# Query for samples created on 2024-09-01 or later
post :query, xhr: true, params: {
project_ids: [project.id],
template_id: template1.id,
template_attribute_id: template1.template_attributes.first.id,
template_attribute_value: 'source',
filter_begin_date_created: '2024-09-01'
}

assert_response :success

result = assigns(:result)
assert_equal 3, result.length

# Query for samples created on 2024-09-02 or later
post :query, xhr: true, params: {
project_ids: [project.id],
template_id: template1.id,
template_attribute_id: template1.template_attributes.first.id,
template_attribute_value: 'source',
filter_begin_date_created: '2024-09-02'
}

assert_response :success

result = assigns(:result)
assert_equal 2, result.length

# Query for samples created between 2024-09-05 and 2024-09-10
post :query, xhr: true, params: {
project_ids: [project.id],
template_id: template1.id,
template_attribute_id: template1.template_attributes.first.id,
template_attribute_value: 'source',
filter_begin_date_created: '2024-09-05',
filter_end_date_created: '2024-09-10'
}

assert_response :success

result = assigns(:result)
assert_equal 1, result.length
assert_equal 'Source 2', result.first['title']

# Update the first sample
travel_to(Time.zone.local(2024, 9, 20, 12, 0, 0))
source1.update!(external_identifier: 's1')
source2.update!(external_identifier: 's2')
travel_to(Time.zone.local(2024, 9, 25, 12, 0, 0))
source3.update!(external_identifier: 's3')


travel_back
# Query for samples updated between 2024-09-20 or 2024-09-21
post :query, xhr: true, params: {
project_ids: [project.id],
template_id: template1.id,
template_attribute_id: template1.template_attributes.first.id,
template_attribute_value: 'source',
filter_begin_date_updated: '2024-09-20',
filter_end_date_updated: '2024-09-21'
}

assert_response :success

result = assigns(:result)
assert_equal 2, result.length

# Query for samples updated before 2024-09-26
post :query, xhr: true, params: {
project_ids: [project.id],
template_id: template1.id,
template_attribute_id: template1.template_attributes.first.id,
template_attribute_value: 'source',
filter_end_date_updated: '2024-09-26'
}

result = assigns(:result)
assert_equal 3, result.length

end
end

test 'form hides private linked multi samples' do
person = FactoryBot.create(:person)
login_as(person)
Expand Down
Loading