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

Be able to export a full list of media clusters. #2024

Merged
merged 8 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
56 changes: 32 additions & 24 deletions lib/check_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def medias_get_search_result(query)

def self.get_exported_data(query, team_id)
team = Team.find(team_id)
search = CheckSearch.new(query, nil, team_id)
Team.current = team

# Prepare the export
data = []
Expand All @@ -344,31 +344,39 @@ def self.get_exported_data(query, team_id)
fields.each { |tt| header << tt.label }
data << header

# No pagination for the export
search.set_option('esoffset', 0)
search.set_option('eslimit', CheckConfig.get(:export_csv_maximum_number_of_results, 10000, :integer))

# Iterate through each result and generate an output row for the CSV
search.medias.find_each do |pm|
row = [
pm.claim_description&.description,
pm.full_url,
pm.status_i18n,
pm.author_name.to_s.gsub(/ \[.*\]$/, ''),
pm.created_at.strftime("%Y-%m-%d %H:%M:%S"),
pm.published_at&.strftime("%Y-%m-%d %H:%M:%S"),
pm.linked_items_count,
pm.tags_as_sentence
]
annotations = pm.get_annotations('task').map(&:load)
fields.each do |field|
annotation = annotations.find { |a| a.team_task_id == field.id }
answer = (annotation ? (begin annotation.first_response_obj.file_data[:file_urls].join("\n") rescue annotation.first_response.to_s end) : '')
answer = begin JSON.parse(answer).collect{ |x| x['url'] }.join(', ') rescue answer end
row << answer
# Paginate
search = CheckSearch.new(query, nil, team_id)
search_after = [0]
while true
result = $repository.search(_source: 'annotated_id', query: search.medias_query, sort: [{ annotated_id: { order: :asc } }], size: 10000, search_after: search_after).results
ids = result.collect{ |i| i['annotated_id'] }.uniq.map(&:to_i)
break if ids.empty?

# Iterate through each result and generate an output row for the CSV
ProjectMedia.where(id: ids, team_id: search.team_condition(team_id)).find_each do |pm|
row = [
pm.claim_description&.description,
pm.full_url,
pm.status_i18n,
pm.author_name.to_s.gsub(/ \[.*\]$/, ''),
pm.created_at.strftime("%Y-%m-%d %H:%M:%S"),
pm.published_at&.strftime("%Y-%m-%d %H:%M:%S"),
pm.linked_items_count,
pm.tags_as_sentence
]
annotations = pm.get_annotations('task').map(&:load)
fields.each do |field|
annotation = annotations.find { |a| a.team_task_id == field.id }
answer = (annotation ? (begin annotation.first_response_obj.file_data[:file_urls].join("\n") rescue annotation.first_response.to_s end) : '')
answer = begin JSON.parse(answer).collect{ |x| x['url'] }.join(', ') rescue answer end
row << answer
end
data << row
end
data << row

search_after = [ids.max]
end

data
end

Expand Down
13 changes: 9 additions & 4 deletions test/lib/list_export_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,23 @@ def teardown
end
end

test "should export media CSV" do
test "should export media (including child media) CSV" do
setup_elasticsearch
t = create_team
create_team_task team_id: t.id, fieldset: 'tasks'
2.times { create_project_media team: t }
parent = create_project_media team: t, disable_es_callbacks: false
child = create_project_media team: t, disable_es_callbacks: false
create_relationship source_id: parent.id, target_id: child.id, relationship_type: Relationship.confirmed_type

export = ListExport.new(:media, '{}', t.id)
sleep 2 # Wait for indexing

export = ListExport.new(:media, { show_similar: true }.to_json, t.id)
csv_url = export.generate_csv_and_send_email(create_user)
response = Net::HTTP.get_response(URI(csv_url))
assert_equal 200, response.code.to_i
csv_content = CSV.parse(response.body, headers: true)
assert_equal 2, csv_content.size
assert_equal 2, export.number_of_rows
assert_equal 2, csv_content.size
end

test "should export feed CSV" do
Expand Down
Loading