Skip to content

Commit

Permalink
Fixing conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba committed Dec 17, 2024
1 parent d0a2298 commit 0efdb7b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 10 deletions.
11 changes: 8 additions & 3 deletions app/models/explainer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/check_data_points.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions lib/tasks/migrate/20241216215050_set_language_for_explainers.rake
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions lib/team_statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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|
Expand All @@ -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 }
Expand Down
5 changes: 3 additions & 2 deletions test/lib/team_statistics_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class TeamStatisticsTest < ActiveSupport::TestCase
def setup
Explainer.delete_all
@team = create_team
@team.set_languages = ['en', 'pt']
@team.save!
Expand Down Expand Up @@ -52,15 +53,15 @@ 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

travel_to Time.parse('2024-01-02') do
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!
Expand Down
9 changes: 9 additions & 0 deletions test/models/explainer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 0efdb7b

Please sign in to comment.