Skip to content

Commit

Permalink
Ticket CV2-5389: Implementing changes to date ranges as requested by …
Browse files Browse the repository at this point in the history
…Alex
  • Loading branch information
caiosba committed Oct 22, 2024
1 parent b489545 commit 860f8a7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
16 changes: 10 additions & 6 deletions lib/team_statistics.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/graphql_controller_11_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions test/lib/team_statistics_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 },
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 860f8a7

Please sign in to comment.