Skip to content

Commit d2e0c48

Browse files
authored
Merge pull request #153 from TeachMeTW/Refactor-User-Stats
Integrate New User Statistics into Profile Retrieval
2 parents 79ec20d + 186fc73 commit d2e0c48

File tree

3 files changed

+28
-50
lines changed

3 files changed

+28
-50
lines changed

utils/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"inferred_section_summary",
3636
]
3737

38-
valid_uuids_columns = [
38+
VALID_UUIDS_COLS = [
3939
'user_token',
4040
'user_id',
4141
'update_ts',

utils/db_utils.py

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -437,57 +437,35 @@ def add_user_stats(user_data, batch_size=5):
437437
def process_user(user):
438438
with ect.Timer() as process_user_timer:
439439
user_uuid = UUID(user['user_id'])
440-
441-
# Fetch aggregated data for all users once and cache it
442-
ts_aggregate = esta.TimeSeries.get_aggregate_time_series()
443-
444-
# Fetch data for the user, cached for repeated queries
445440
profile_data = edb.get_profile_db().find_one({'user_id': user_uuid})
441+
# Fetch data for the user, cached for repeated queries
442+
logging.info(f'keyspr: {profile_data}')
443+
if not profile_data:
444+
profile_data = {}
445+
# Assign existing profile attributes to the user dictionary
446+
user['platform'] = profile_data.get('curr_platform')
447+
user['manufacturer'] = profile_data.get('manufacturer')
448+
user['app_version'] = profile_data.get('client_app_version')
449+
user['os_version'] = profile_data.get('client_os_version')
450+
user['phone_lang'] = profile_data.get('phone_lang')
446451

447-
total_trips = ts_aggregate.find_entries_count(
448-
key_list=["analysis/confirmed_trip"],
449-
extra_query_list=[{'user_id': user_uuid}]
450-
)
451-
labeled_trips = ts_aggregate.find_entries_count(
452-
key_list=["analysis/confirmed_trip"],
453-
extra_query_list=[{'user_id': user_uuid}, {'data.user_input': {'$ne': {}}}]
454-
)
452+
# Assign newly stored statistics to the user dictionary
453+
user['total_trips'] = profile_data.get('total_trips')
454+
user['labeled_trips'] = profile_data.get('labeled_trips')
455455

456-
user['total_trips'] = total_trips
457-
user['labeled_trips'] = labeled_trips
458-
459-
if profile_data:
460-
user['platform'] = profile_data.get('curr_platform')
461-
user['manufacturer'] = profile_data.get('manufacturer')
462-
user['app_version'] = profile_data.get('client_app_version')
463-
user['os_version'] = profile_data.get('client_os_version')
464-
user['phone_lang'] = profile_data.get('phone_lang')
465-
466-
if total_trips > 0:
467-
ts = esta.TimeSeries.get_time_series(user_uuid)
468-
first_trip_ts = ts.get_first_value_for_field(
469-
key='analysis/confirmed_trip',
470-
field='data.end_ts',
471-
sort_order=pymongo.ASCENDING
472-
)
473-
if first_trip_ts != -1:
474-
user['first_trip'] = arrow.get(first_trip_ts).format(time_format)
475-
476-
last_trip_ts = ts.get_first_value_for_field(
477-
key='analysis/confirmed_trip',
478-
field='data.end_ts',
479-
sort_order=pymongo.DESCENDING
480-
)
481-
if last_trip_ts != -1:
482-
user['last_trip'] = arrow.get(last_trip_ts).format(time_format)
483-
484-
last_call_ts = ts.get_first_value_for_field(
485-
key='stats/server_api_time',
486-
field='data.ts',
487-
sort_order=pymongo.DESCENDING
488-
)
489-
if last_call_ts != -1:
490-
user['last_call'] = arrow.get(last_call_ts).format(time_format)
456+
# Retrieve and assign pipeline range
457+
pipeline_range = profile_data.get('pipeline_range', {})
458+
start_ts = pipeline_range.get('start_ts')
459+
end_ts = pipeline_range.get('end_ts')
460+
if start_ts:
461+
user['first_trip'] = arrow.get(start_ts).format(time_format)
462+
if end_ts:
463+
user['last_trip'] = arrow.get(end_ts).format(time_format)
464+
465+
# Retrieve and assign last API call timestamp
466+
last_call_ts = profile_data.get('last_call_ts')
467+
if last_call_ts:
468+
user['last_call'] = arrow.get(last_call_ts).format('YYYY-MM-DD')
491469

492470
esdsq.store_dashboard_time(
493471
"admin/db_utils/add_user_stats/process_user",

utils/permissions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def get_allowed_trip_columns():
9393

9494

9595
def get_uuids_columns():
96-
columns = set(constants.valid_uuids_columns)
96+
columns = set(constants.VALID_UUIDS_COLS)
9797
for column in permissions.get("data_uuids_columns_exclude", []):
9898
columns.discard(column)
9999
return columns

0 commit comments

Comments
 (0)