diff --git a/lib/check_search.rb b/lib/check_search.rb index 89bc0e067..135a9c2e9 100644 --- a/lib/check_search.rb +++ b/lib/check_search.rb @@ -342,10 +342,11 @@ def self.get_exported_data(query, team_id) # Prepare the export data = [] header = nil + fields = [] if feed_sharing_only_fact_checks header = ['Fact-check title', 'Fact-check summary', 'Fact-check URL', 'Tags', 'Workspace', 'Updated at', 'Rating'] else - header = ['Claim', 'Item page URL', 'Status', 'Created by', 'Submitted at', 'Published at', 'Number of media', 'Tags'] + header = ['Claim', 'Item page URL', 'Status', 'Created by', 'Submitted at', 'Social Media Posted at', 'Report Published at', 'Number of media', 'Tags'] fields = team.team_tasks.sort fields.each { |tt| header << tt.label } end @@ -356,39 +357,15 @@ def self.get_exported_data(query, team_id) while !search_after.empty? 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.compact.map(&:to_i) + pm_report = {} + Dynamic.where(annotation_type: 'report_design', annotated_type: 'ProjectMedia', annotated_id: ids) + .find_each do |raw| + pm_report[raw.annotated_id] = raw.data['last_published'].to_i if raw.data['state'] == 'published' + end # 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 = nil - if feed_sharing_only_fact_checks - row = [ - pm.fact_check_title, - pm.fact_check_summary, - pm.fact_check_url, - pm.tags_as_sentence, - pm.team_name, - pm.updated_at_timestamp, - pm.status - ] - else - 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 - end + row = self.get_exported_data_row(feed_sharing_only_fact_checks, pm, pm_report[pm.id], fields) data << row end @@ -398,6 +375,42 @@ def self.get_exported_data(query, team_id) data end + def self.get_exported_data_row(feed_sharing_only_fact_checks, pm, report_published_at, fields) + row = nil + if feed_sharing_only_fact_checks + row = [ + pm.fact_check_title, + pm.fact_check_summary, + pm.fact_check_url, + pm.tags_as_sentence, + pm.team_name, + pm.updated_at_timestamp, + pm.status + ] + else + report_published_at_value = report_published_at ? Time.at(report_published_at).strftime("%Y-%m-%d %H:%M:%S") : nil + 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"), + report_published_at_value, + 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 + end + row + end + private def adjust_es_window_size