Skip to content

Commit

Permalink
[WIP] Ticket CV2-5067: Implementing export for articles and feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba committed Aug 23, 2024
1 parent 1726979 commit fe2b85a
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/graph/mutations/export_mutations.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ExportMutations
class ExportList < Mutations::BaseMutation
argument :query, GraphQL::Types::String, required: true
argument :type, GraphQL::Types::String, required: true # 'media', 'feed' or 'article'
argument :query, GraphQL::Types::String, required: true # JSON
argument :type, GraphQL::Types::String, required: true # 'media', 'feed', 'fact-check' or 'explainer'

field :success, GraphQL::Types::Boolean, null: true

Expand Down
8 changes: 8 additions & 0 deletions app/models/explainer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ def update_paragraphs_in_alegre
self.class.delay_for(5.seconds).update_paragraphs_in_alegre(self.id, previous_paragraphs_count, Time.now.to_f)
end

def self.get_exported_data(query, team)
data = [['ID', 'Title', 'Description', 'URL', 'Language']]
team.filtered_explainers(query).find_each do |exp|
data << [exp.id, exp.title, exp.description, exp.url, exp.language]
end
data
end

def self.update_paragraphs_in_alegre(id, previous_paragraphs_count, timestamp)
explainer = Explainer.find(id)

Expand Down
8 changes: 8 additions & 0 deletions app/models/fact_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def update_item_status
end
end

def self.get_exported_data(query, team)
data = [['ID', 'Title', 'Summary', 'URL', 'Language', 'Report Status', 'Imported?']]
team.filtered_fact_checks(query).find_each do |fc|
data << [fc.id, fc.title, fc.summary, fc.url, fc.language, fc.report_status, fc.imported.to_s]
end
data
end

private

def set_language
Expand Down
8 changes: 8 additions & 0 deletions app/models/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ def saved_search_was
SavedSearch.find_by_id(self.saved_search_id_before_last_save)
end

def get_exported_data(filters)
data = [['Title', 'Number of media', 'Number of requests', 'Number of fact-checks']]
self.filtered_clusters(filters).find_each do |cluster|
data << [cluster.title, cluster.media_count, cluster.requests_count, cluster.fact_checks_count]
end
data
end

# This takes some time to run because it involves external HTTP requests and writes to the database:
# 1) If the query contains a media URL, it will be downloaded... if it contains some other URL, it will be sent to Pender
# 2) Requests will be made to Alegre in order to index the request media and to look for similar requests
Expand Down
17 changes: 16 additions & 1 deletion lib/list_export.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
class ListExport
TYPES = [:article, :feed, :media]
TYPES = [:media, :feed, :fact_check, :explainer]

def initialize(type, query, team_id)
@type = type
@query = query
@parsed_query = JSON.parse(@query)
@team_id = team_id
@team = Team.find(team_id)
@feed = Feed.where(id: @parsed_query['feed_id'], team_id: @team_id).last if type == :feed
raise "Invalid export type '#{type}'. Should be one of: #{TYPES}" unless TYPES.include?(type)
end

def number_of_rows
case @type
when :media
CheckSearch.new(@query, nil, @team_id).number_of_results
when :feed
@feed.clusters_count(@parsed_query)
when :fact_check
@team.filtered_fact_checks(@parsed_query).count
when :explainer
@team.filtered_explainers(@parsed_query).count
end
end

Expand Down Expand Up @@ -47,6 +56,12 @@ def export_data
case @type
when :media
CheckSearch.get_exported_data(@query, @team_id)
when :feed
@feed.get_exported_data(@parsed_query)
when :fact_check
FactCheck.get_exported_data(@parsed_query, @team)
when :explainer
Explainer.get_exported_data(@parsed_query, @team)
end
end
end
66 changes: 60 additions & 6 deletions test/lib/list_export_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,80 @@ def setup
def teardown
end

test "should export media CSV and expire it" do
test "should expire the export" do
t = create_team
create_team_task team_id: t.id, fieldset: 'tasks'
pm1 = create_project_media team: t
pm2 = create_project_media team: t
pm = create_project_media team: t

stub_configs({ 'export_csv_expire' => 2 }) do

# Generate a CSV with the two exported items
export = ListExport.new(:media, '{}', 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

# Make sure it expires after 2 seconds
sleep 3 # Just to be safe
response = Net::HTTP.get_response(URI(csv_url))
assert_equal 403, response.code.to_i
end
end

test "should export media CSV" do
t = create_team
create_team_task team_id: t.id, fieldset: 'tasks'
2.times { create_project_media team: t }

export = ListExport.new(:media, '{}', 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
end

test "should export feed CSV" do
t = create_team
f = create_feed team: t
2.times { f.clusters << create_cluster }

export = ListExport.new(:feed, { feed_id: f.id }.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
end

test "should export fact-checks CSV" do
t = create_team
2.times do
pm = create_project_media team: t
cd = create_claim_description project_media: pm
create_fact_check claim_description: cd
end

export = ListExport.new(:fact_check, '{}', 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
end

test "should export explainers CSV" do
t = create_team
2.times { create_explainer team: t }

export = ListExport.new(:explainer, '{}', 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
end
end

0 comments on commit fe2b85a

Please sign in to comment.