From 860f8a7a2258f0a7d4adca675da3cf9c87eb4e18 Mon Sep 17 00:00:00 2001 From: Caio <117518+caiosba@users.noreply.github.com> Date: Mon, 21 Oct 2024 22:51:11 -0300 Subject: [PATCH] Ticket CV2-5389: Implementing changes to date ranges as requested by Alex --- lib/team_statistics.rb | 16 +++++++++------ .../controllers/graphql_controller_11_test.rb | 2 +- test/lib/team_statistics_test.rb | 20 +++++++++---------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/team_statistics.rb b/lib/team_statistics.rb index 42da772be..d42605d9c 100644 --- a/lib/team_statistics.rb +++ b/lib/team_statistics.rb @@ -1,5 +1,6 @@ class TeamStatistics - PERIODS = ['last_day', 'last_week', 'last_month', 'last_year'] + PERIODS = ['past_week', 'past_2_weeks', 'past_month', 'past_3_months', 'past_6_months', 'year_to_date'] + PLATFORMS = Bot::Smooch::SUPPORTED_INTEGRATION_NAMES def initialize(team, period, language, platform = nil) @@ -198,12 +199,15 @@ def number_of_matched_results def time_range ago = { - last_day: 1.day, - last_week: 1.week, - last_month: 1.month, - last_year: 1.year + past_week: 1.week, + past_2_weeks: 2.weeks, + past_month: 1.month, + past_3_months: 3.months, + past_6_months: 6.months }[@period.to_sym] - Time.now.ago(ago).to_datetime..Time.now.to_datetime + from = Time.now.ago(ago) unless ago.nil? + from = Time.now.beginning_of_year if @period.to_s == 'year_to_date' + from.to_datetime..Time.now.to_datetime end def fact_checks_base_query(timestamp_field = :created_at, group_by_day = false) diff --git a/test/controllers/graphql_controller_11_test.rb b/test/controllers/graphql_controller_11_test.rb index 280fa460c..7da137925 100644 --- a/test/controllers/graphql_controller_11_test.rb +++ b/test/controllers/graphql_controller_11_test.rb @@ -210,7 +210,7 @@ def teardown query = <<~GRAPHQL query { team(slug: "#{team.slug}") { - statistics(period: "last_week") { + statistics(period: "past_week") { number_of_articles_created_by_date number_of_articles_updated_by_date number_of_explainers_created diff --git a/test/lib/team_statistics_test.rb b/test/lib/team_statistics_test.rb index be2a2297f..4dbcd6c3d 100644 --- a/test/lib/team_statistics_test.rb +++ b/test/lib/team_statistics_test.rb @@ -12,36 +12,36 @@ def teardown test "should provide a valid period" do assert_raises ArgumentError do - TeamStatistics.new(@team, 'last_century', 'en', 'whatsapp') + TeamStatistics.new(@team, 'past_century', 'en', 'whatsapp') end assert_nothing_raised do - TeamStatistics.new(@team, 'last_month', 'en', 'whatsapp') + TeamStatistics.new(@team, 'past_month', 'en', 'whatsapp') end end test "should provide a valid workspace" do assert_raises ArgumentError do - TeamStatistics.new(Class.new, 'last_month', 'en', 'whatsapp') + TeamStatistics.new(Class.new, 'past_month', 'en', 'whatsapp') end assert_nothing_raised do - TeamStatistics.new(@team, 'last_month', 'en', 'whatsapp') + TeamStatistics.new(@team, 'past_month', 'en', 'whatsapp') end end test "should provide a valid platform" do assert_raises ArgumentError do - TeamStatistics.new(@team, 'last_month', 'en', 'icq') + TeamStatistics.new(@team, 'past_month', 'en', 'icq') end assert_nothing_raised do - TeamStatistics.new(@team, 'last_month', 'en', 'whatsapp') + TeamStatistics.new(@team, 'past_month', 'en', 'whatsapp') end end test "should have a GraphQL ID" do - assert_kind_of String, TeamStatistics.new(@team, 'last_month', 'en', 'whatsapp').id + assert_kind_of String, TeamStatistics.new(@team, 'past_month', 'en', 'whatsapp').id end test "should return articles statistics" do @@ -67,7 +67,7 @@ def teardown end travel_to Time.parse('2024-01-08') do - object = TeamStatistics.new(@team, 'last_week', 'en') + object = TeamStatistics.new(@team, 'past_week', 'en') assert_equal({ '2024-01-01' => 2, '2024-01-02' => 2, '2024-01-03' => 0, '2024-01-04' => 0, '2024-01-05' => 0, '2024-01-06' => 0, '2024-01-07' => 0, '2024-01-08' => 0 }, object.number_of_articles_created_by_date) assert_equal({ '2024-01-01' => 0, '2024-01-02' => 1, '2024-01-03' => 0, '2024-01-04' => 0, '2024-01-05' => 0, '2024-01-06' => 0, '2024-01-07' => 0, '2024-01-08' => 0 }, @@ -95,7 +95,7 @@ def teardown sleep 2 - object = TeamStatistics.new(@team, 'last_week', 'en') + object = TeamStatistics.new(@team, 'past_week', 'en') expected = { 'Foo' => 2, 'Bar' => 1 } assert_equal expected, object.top_articles_sent end @@ -130,7 +130,7 @@ def teardown end travel_to Time.parse('2024-01-08') do - object = TeamStatistics.new(@team, 'last_week', 'en', 'whatsapp') + object = TeamStatistics.new(@team, 'past_week', 'en', 'whatsapp') assert_equal 5, object.number_of_messages assert_equal({ '2024-01-01' => 2, '2024-01-02' => 0, '2024-01-03' => 3, '2024-01-04' => 0, '2024-01-05' => 0, '2024-01-06' => 0, '2024-01-07' => 0, '2024-01-08' => 0 }, object.number_of_messages_by_date)