Skip to content

Commit

Permalink
Addressed comments, reduced overkill on refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
TeachMeTW committed Nov 9, 2024
1 parent 89dfc9a commit e4e8ed9
Showing 1 changed file with 5 additions and 54 deletions.
59 changes: 5 additions & 54 deletions emission/analysis/result/user_stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,6 @@

TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'

def get_time_series(user_id: str) -> esta.TimeSeries:
"""
Fetches the time series data for a given user.
:param user_id: The UUID of the user.
:type user_id: str
:return: The time series object.
:rtype: esta.TimeSeries
"""
logging.debug(f"Fetching time series for user_id: {user_id}")
return esta.TimeSeries.get_time_series(user_id)


def get_timestamp(ts: esta.TimeSeries, trip_key: str, field: str, sort_order: int) -> Optional[int]:
"""
Retrieves a timestamp from the time series.
:param ts: The time series object.
:type ts: esta.TimeSeries
:param trip_key: The key representing the trip data.
:type trip_key: str
:param field: The specific field to retrieve.
:type field: str
:param sort_order: Sorting order (pymongo.ASCENDING or pymongo.DESCENDING).
:type sort_order: int
:return: The timestamp or None if not found.
:rtype: Optional[int]
"""
timestamp = ts.get_first_value_for_field(trip_key, field, sort_order)
logging.debug(f"Retrieved {field}: {timestamp}")
return None if timestamp == -1 else timestamp


def count_trips(ts: esta.TimeSeries, key_list: list, extra_query_list: Optional[list] = None) -> int:
"""
Counts the number of trips based on the provided query.
Expand Down Expand Up @@ -78,20 +45,6 @@ def get_last_call_timestamp(ts: esta.TimeSeries) -> Optional[int]:
return None if last_call_ts == -1 else last_call_ts


def format_timestamp(ts: int) -> str:
"""
Formats a timestamp using the predefined time format.
:param ts: The timestamp to format.
:type ts: int
:return: The formatted timestamp.
:rtype: str
"""
formatted = arrow.get(ts).format(TIME_FORMAT)
logging.debug(f"Formatted timestamp: {formatted}")
return formatted


def update_user_profile(user_id: str, data: Dict[str, Any]) -> None:
"""
Updates the user profile with the provided data.
Expand Down Expand Up @@ -121,10 +74,12 @@ def get_and_store_user_stats(user_id: str, trip_key: str) -> None:
try:
logging.info(f"Starting get_and_store_user_stats for user_id: {user_id}, trip_key: {trip_key}")

ts = get_time_series(user_id)
ts = esta.TimeSeries.get_time_series(user_id)
start_ts_result = ts.get_first_value_for_field(trip_key, "data.start_ts", pymongo.ASCENDING)
start_ts = None if start_ts_result == -1 else start_ts_result

start_ts = get_timestamp(ts, trip_key, "data.start_ts", pymongo.ASCENDING)
end_ts = get_timestamp(ts, trip_key, "data.end_ts", pymongo.DESCENDING)
end_ts_result = ts.get_first_value_for_field(trip_key, "data.end_ts", pymongo.DESCENDING)
end_ts = None if end_ts_result == -1 else end_ts_result

total_trips = count_trips(ts, key_list=["analysis/confirmed_trip"])
labeled_trips = count_trips(
Expand All @@ -148,10 +103,6 @@ def get_and_store_user_stats(user_id: str, trip_key: str) -> None:
"labeled_trips": labeled_trips
}

if last_call_ts is not None:
formatted_last_call = format_timestamp(last_call_ts)
update_data["last_call"] = formatted_last_call

update_user_profile(user_id, update_data)

logging.debug("User profile updated successfully.")
Expand Down

0 comments on commit e4e8ed9

Please sign in to comment.