9
9
10
10
TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'
11
11
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
-
45
12
def count_trips (ts : esta .TimeSeries , key_list : list , extra_query_list : Optional [list ] = None ) -> int :
46
13
"""
47
14
Counts the number of trips based on the provided query.
@@ -78,20 +45,6 @@ def get_last_call_timestamp(ts: esta.TimeSeries) -> Optional[int]:
78
45
return None if last_call_ts == - 1 else last_call_ts
79
46
80
47
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
-
95
48
def update_user_profile (user_id : str , data : Dict [str , Any ]) -> None :
96
49
"""
97
50
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:
121
74
try :
122
75
logging .info (f"Starting get_and_store_user_stats for user_id: { user_id } , trip_key: { trip_key } " )
123
76
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
125
80
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
128
83
129
84
total_trips = count_trips (ts , key_list = ["analysis/confirmed_trip" ])
130
85
labeled_trips = count_trips (
@@ -148,10 +103,6 @@ def get_and_store_user_stats(user_id: str, trip_key: str) -> None:
148
103
"labeled_trips" : labeled_trips
149
104
}
150
105
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
-
155
106
update_user_profile (user_id , update_data )
156
107
157
108
logging .debug ("User profile updated successfully." )
0 commit comments