Skip to content

Commit e4e8ed9

Browse files
committed
Addressed comments, reduced overkill on refactor
1 parent 89dfc9a commit e4e8ed9

File tree

1 file changed

+5
-54
lines changed

1 file changed

+5
-54
lines changed

emission/analysis/result/user_stat.py

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,6 @@
99

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

12-
def get_time_series(user_id: str) -> esta.TimeSeries:
13-
"""
14-
Fetches the time series data for a given user.
15-
16-
:param user_id: The UUID of the user.
17-
:type user_id: str
18-
:return: The time series object.
19-
:rtype: esta.TimeSeries
20-
"""
21-
logging.debug(f"Fetching time series for user_id: {user_id}")
22-
return esta.TimeSeries.get_time_series(user_id)
23-
24-
25-
def get_timestamp(ts: esta.TimeSeries, trip_key: str, field: str, sort_order: int) -> Optional[int]:
26-
"""
27-
Retrieves a timestamp from the time series.
28-
29-
:param ts: The time series object.
30-
:type ts: esta.TimeSeries
31-
:param trip_key: The key representing the trip data.
32-
:type trip_key: str
33-
:param field: The specific field to retrieve.
34-
:type field: str
35-
:param sort_order: Sorting order (pymongo.ASCENDING or pymongo.DESCENDING).
36-
:type sort_order: int
37-
:return: The timestamp or None if not found.
38-
:rtype: Optional[int]
39-
"""
40-
timestamp = ts.get_first_value_for_field(trip_key, field, sort_order)
41-
logging.debug(f"Retrieved {field}: {timestamp}")
42-
return None if timestamp == -1 else timestamp
43-
44-
4512
def count_trips(ts: esta.TimeSeries, key_list: list, extra_query_list: Optional[list] = None) -> int:
4613
"""
4714
Counts the number of trips based on the provided query.
@@ -78,20 +45,6 @@ def get_last_call_timestamp(ts: esta.TimeSeries) -> Optional[int]:
7845
return None if last_call_ts == -1 else last_call_ts
7946

8047

81-
def format_timestamp(ts: int) -> str:
82-
"""
83-
Formats a timestamp using the predefined time format.
84-
85-
:param ts: The timestamp to format.
86-
:type ts: int
87-
:return: The formatted timestamp.
88-
:rtype: str
89-
"""
90-
formatted = arrow.get(ts).format(TIME_FORMAT)
91-
logging.debug(f"Formatted timestamp: {formatted}")
92-
return formatted
93-
94-
9548
def update_user_profile(user_id: str, data: Dict[str, Any]) -> None:
9649
"""
9750
Updates the user profile with the provided data.
@@ -121,10 +74,12 @@ def get_and_store_user_stats(user_id: str, trip_key: str) -> None:
12174
try:
12275
logging.info(f"Starting get_and_store_user_stats for user_id: {user_id}, trip_key: {trip_key}")
12376

124-
ts = get_time_series(user_id)
77+
ts = esta.TimeSeries.get_time_series(user_id)
78+
start_ts_result = ts.get_first_value_for_field(trip_key, "data.start_ts", pymongo.ASCENDING)
79+
start_ts = None if start_ts_result == -1 else start_ts_result
12580

126-
start_ts = get_timestamp(ts, trip_key, "data.start_ts", pymongo.ASCENDING)
127-
end_ts = get_timestamp(ts, trip_key, "data.end_ts", pymongo.DESCENDING)
81+
end_ts_result = ts.get_first_value_for_field(trip_key, "data.end_ts", pymongo.DESCENDING)
82+
end_ts = None if end_ts_result == -1 else end_ts_result
12883

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

151-
if last_call_ts is not None:
152-
formatted_last_call = format_timestamp(last_call_ts)
153-
update_data["last_call"] = formatted_last_call
154-
155106
update_user_profile(user_id, update_data)
156107

157108
logging.debug("User profile updated successfully.")

0 commit comments

Comments
 (0)