Skip to content

Commit

Permalink
Most consecutive activities (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnScolaro authored Sep 10, 2023
1 parent 74c5982 commit 6c77ebc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
52 changes: 52 additions & 0 deletions active_statistics/statistics/trivia/summary_trivia.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,57 @@ def get_activity_id(self) -> Optional[int]:
return self.latest_activity_id


class MostConsecutiveDaysOfActivities(TriviaTidbitBase):
def __init__(self) -> None:
self.date_list: list[dt.date] = []

def process_activity(self, activity: Activity) -> None:
if activity.start_date_local is not None:
self.date_list.append(activity.start_date_local.date())

def get_tidbit(self) -> Optional[str]:
# Sort the date list in ascending order
self.date_list.sort()

max_consecutive_days = 1
current_consecutive_days = 1
start_date = self.date_list[0]
end_date = self.date_list[0]

# Initialize variables to track the start and end of the longest consecutive days
max_start_date = self.date_list[0]
max_end_date = self.date_list[0]

for i in range(1, len(self.date_list)):
# Calculate the difference between consecutive datetimes
time_difference = self.date_list[i] - self.date_list[i - 1]

# If there are multiple activities on the same day, just continue.
if time_difference == dt.timedelta(days=0):
continue

# Check if the difference is one day
if time_difference == dt.timedelta(days=1):
current_consecutive_days += 1
end_date = self.date_list[i]
else:
# If the difference is not one day, reset the current count
current_consecutive_days = 1
start_date = self.date_list[i]
end_date = self.date_list[i]

# Update the maximum consecutive days count and dates if needed
if current_consecutive_days > max_consecutive_days:
max_consecutive_days = current_consecutive_days
max_start_date = start_date
max_end_date = end_date

return f"{max_consecutive_days} days ({max_start_date} to {max_end_date})"

def get_description(self) -> str:
return "Most Consecutive Days of Activities"


general_trivia = TriviaProcessor()

general_trivia.register_tidbit(HottestActivityTidbit())
Expand All @@ -328,3 +379,4 @@ def get_activity_id(self) -> Optional[int]:
general_trivia.register_tidbit(EarliestActivityTidbit())
general_trivia.register_tidbit(LatestActivityTidbit())
general_trivia.register_tidbit(TotalKudosRecievedTidbit())
general_trivia.register_tidbit(MostConsecutiveDaysOfActivities())
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import datetime as dt

from active_statistics.statistics.trivia import TriviaProcessor
from active_statistics.statistics.trivia.summary_trivia import (
MostConsecutiveDaysOfActivities,
)
from tests.factories.activity_factories import ActivityFactory


def test_general_trivia(some_basic_runs_and_rides) -> None:
"""
Tests that the most consecutive days calculator is correct.
"""
processor = TriviaProcessor()
processor.register_tidbit(MostConsecutiveDaysOfActivities())

activities = [
ActivityFactory(start_date_local=dt.datetime(2023, 1, 1, 2)),
ActivityFactory(start_date_local=dt.datetime(2023, 1, 2, 19)),
ActivityFactory(start_date_local=dt.datetime(2023, 1, 4, 3)),
ActivityFactory(start_date_local=dt.datetime(2023, 1, 5, 10)),
ActivityFactory(start_date_local=dt.datetime(2023, 1, 6, 1)),
ActivityFactory(start_date_local=dt.datetime(2023, 1, 7, 22)),
ActivityFactory(start_date_local=dt.datetime(2023, 1, 7, 23)),
ActivityFactory(start_date_local=dt.datetime(2023, 1, 10, 1)),
ActivityFactory(start_date_local=dt.datetime(2023, 1, 10, 2)),
ActivityFactory(start_date_local=dt.datetime(2023, 1, 10, 3)),
ActivityFactory(start_date_local=dt.datetime(2023, 1, 11, 4)),
ActivityFactory(start_date_local=dt.datetime(2023, 1, 12, 5)),
]

result = processor.get_data(activity for activity in activities)
assert result[0][1] == "4 days (2023-01-04 to 2023-01-07)"

0 comments on commit 6c77ebc

Please sign in to comment.