From 0efdb7b0aeb430dbc0d853c8aa79a7609476d8f2 Mon Sep 17 00:00:00 2001 From: Caio Almeida <117518+caiosba@users.noreply.github.com> Date: Tue, 17 Dec 2024 13:02:35 -0300 Subject: [PATCH] Fixing conflict --- app/models/explainer.rb | 11 ++++++++--- lib/check_data_points.rb | 2 +- ...1216215050_set_language_for_explainers.rake | 18 ++++++++++++++++++ lib/team_statistics.rb | 8 ++++---- test/lib/team_statistics_test.rb | 5 +++-- test/models/explainer_test.rb | 9 +++++++++ 6 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 lib/tasks/migrate/20241216215050_set_language_for_explainers.rake diff --git a/app/models/explainer.rb b/app/models/explainer.rb index aa25ea9e4..77ee25af3 100644 --- a/app/models/explainer.rb +++ b/app/models/explainer.rb @@ -13,10 +13,10 @@ class Explainer < ApplicationRecord has_many :explainer_items, dependent: :destroy has_many :project_medias, through: :explainer_items - before_validation :set_team + before_validation :set_team, :set_language validates_format_of :url, with: URI.regexp, allow_blank: true, allow_nil: true validates_presence_of :team, :title, :description - validate :language_in_allowed_values, unless: proc { |e| e.language.blank? } + validate :language_in_allowed_values after_save :update_paragraphs_in_alegre after_update :detach_explainer_if_trashed @@ -130,8 +130,13 @@ def set_team self.team ||= Team.current end + def set_language + default_language = self.team&.get_language || 'und' + self.language ||= default_language + end + def language_in_allowed_values - allowed_languages = self.team.get_languages || ['en'] + allowed_languages = self.team&.get_languages || ['en'] allowed_languages << 'und' errors.add(:language, I18n.t(:"errors.messages.invalid_article_language_value")) unless allowed_languages.include?(self.language) end diff --git a/lib/check_data_points.rb b/lib/check_data_points.rb index 83c70d7df..5f4025bff 100644 --- a/lib/check_data_points.rb +++ b/lib/check_data_points.rb @@ -6,7 +6,7 @@ class << self # Number of tipline messages def tipline_messages(team_id, start_date, end_date, granularity = nil, platform = nil, language = nil) start_date, end_date = parse_start_end_dates(start_date, end_date) - query = TiplineMessage.where(team_id: team_id, created_at: start_date..end_date) + query = TiplineMessage.where(team_id: team_id, created_at: start_date..end_date, state: ['sent', 'received']) query_based_on_granularity(query, platform, language, granularity) end diff --git a/lib/tasks/migrate/20241216215050_set_language_for_explainers.rake b/lib/tasks/migrate/20241216215050_set_language_for_explainers.rake new file mode 100644 index 000000000..f137113c1 --- /dev/null +++ b/lib/tasks/migrate/20241216215050_set_language_for_explainers.rake @@ -0,0 +1,18 @@ +namespace :check do + namespace :migrate do + task set_language_for_explainers: :environment do + started = Time.now.to_i + query = Explainer.where(language: nil) + n = query.count + i = 0 + query.find_each do |explainer| + i += 1 + language = explainer.team&.get_language || 'und' + explainer.update_column(:language, language) + puts "[#{Time.now}] [#{i}/#{n}] Setting language for explainer ##{explainer.id} as #{language}" + end + minutes = ((Time.now.to_i - started) / 60).to_i + puts "[#{Time.now}] Done in #{minutes} minutes. Number of explainers without language: #{query.count}" + end + end +end diff --git a/lib/team_statistics.rb b/lib/team_statistics.rb index 28b32010b..4c58dedfe 100644 --- a/lib/team_statistics.rb +++ b/lib/team_statistics.rb @@ -83,10 +83,10 @@ def top_articles_tags FROM ( SELECT unnest(fcs.tags) AS tag FROM fact_checks fcs INNER JOIN claim_descriptions cds ON fcs.claim_description_id = cds.id - WHERE cds.team_id = :team_id AND fcs.created_at BETWEEN :start_date AND :end_date AND fcs.language IN (:language) + WHERE cds.team_id = :team_id AND fcs.updated_at BETWEEN :start_date AND :end_date AND fcs.language IN (:language) UNION ALL SELECT unnest(explainers.tags) AS tag FROM explainers - WHERE explainers.team_id = :team_id AND explainers.created_at BETWEEN :start_date AND :end_date AND explainers.language IN (:language) + WHERE explainers.team_id = :team_id AND explainers.updated_at BETWEEN :start_date AND :end_date AND explainers.language IN (:language) ) AS all_tags GROUP BY tag ORDER BY tag_count DESC @@ -198,7 +198,7 @@ def top_requested_media_clusters # FIXME: The "demand" is across languages and platforms def top_media_tags tags = {} - clusters = CheckDataPoints.top_clusters(@team.id, @start_date, @end_date, 5, 'last_seen', @language || @all_languages, 'language', @platform) + clusters = CheckDataPoints.top_media_tags(@team.id, @start_date, @end_date, 20, 'last_seen', @language || @all_languages, 'language', @platform) clusters.each do |pm_id, demand| item = ProjectMedia.find(pm_id) item.tags_as_sentence.split(',').map(&:strip).each do |tag| @@ -218,7 +218,7 @@ def number_of_articles_sent end def number_of_matched_results_by_article_type - query = TiplineRequest.where(team_id: @team.id, smooch_request_type: ['relevant_search_result_requests', 'irrelevant_search_result_requests', 'timeout_search_requests'], created_at: @start_date..@end_date) + query = TiplineRequest.where(team_id: @team.id, created_at: @start_date..@end_date) query = query.where(platform: @platform) unless @platform.blank? query = query.where(language: @language) unless @language.blank? { 'FactCheck' => query.joins(project_media: { claim_description: :fact_check }).count, 'Explainer' => query.joins(project_media: :explainers).count } diff --git a/test/lib/team_statistics_test.rb b/test/lib/team_statistics_test.rb index 7cd7a46f6..5b1d9249c 100644 --- a/test/lib/team_statistics_test.rb +++ b/test/lib/team_statistics_test.rb @@ -2,6 +2,7 @@ class TeamStatisticsTest < ActiveSupport::TestCase def setup + Explainer.delete_all @team = create_team @team.set_languages = ['en', 'pt'] @team.save! @@ -52,7 +53,7 @@ def teardown create_fact_check(tags: ['foo', 'bar'], language: 'en', rating: 'false', claim_description: create_claim_description(project_media: create_project_media(team: @team))) create_fact_check(tags: ['foo', 'bar'], claim_description: create_claim_description(project_media: create_project_media(team: team))) exp = create_explainer team: @team, language: 'en', tags: ['foo'] - create_explainer team: @team, tags: ['foo', 'bar'] + create_explainer team: @team, tags: ['foo', 'bar'], language: 'pt' create_explainer language: 'en', team: team, tags: ['foo', 'bar'] end @@ -60,7 +61,7 @@ def teardown create_fact_check(tags: ['bar'], report_status: 'published', rating: 'verified', language: 'en', claim_description: create_claim_description(project_media: create_project_media(team: @team))) create_fact_check(tags: ['foo', 'bar'], claim_description: create_claim_description(project_media: create_project_media(team: team))) create_explainer team: @team, language: 'en', tags: ['foo'] - create_explainer team: @team, tags: ['foo', 'bar'] + create_explainer team: @team, tags: ['foo', 'bar'], language: 'pt' create_explainer language: 'en', team: team, tags: ['foo', 'bar'] exp.updated_at = Time.now exp.save! diff --git a/test/models/explainer_test.rb b/test/models/explainer_test.rb index 7d191e1e7..9a3385e85 100644 --- a/test/models/explainer_test.rb +++ b/test/models/explainer_test.rb @@ -5,6 +5,10 @@ def setup Explainer.delete_all end + def teardown + User.current = Team.current = nil + end + test "should create explainer" do assert_difference 'Explainer.count' do create_explainer @@ -153,4 +157,9 @@ def setup end end end + + test "should set default language when language is not set" do + ex = create_explainer language: nil + assert_equal 'en', ex.reload.language + end end