Skip to content

Commit

Permalink
Merge pull request #57 from AlirezaRa94/user_stats
Browse files Browse the repository at this point in the history
Add some trip stats to the user screen
  • Loading branch information
shankari authored Jul 11, 2023
2 parents 21c460e + eb16b9b commit 6f0c381
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pages/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from dash import dcc, html, Input, Output, callback, register_page, dash_table

# Etc
import logging
import pandas as pd
from dash.exceptions import PreventUpdate

from utils import permissions as perm_utils
from utils import db_utils

register_page(__name__, path="/data")

Expand All @@ -28,6 +30,14 @@
)


def clean_location_data(df):
if 'data.start_loc.coordinates' in df.columns:
df['data.start_loc.coordinates'] = df['data.start_loc.coordinates'].apply(lambda x: f'({x[0]}, {x[1]})')
if 'data.end_loc.coordinates' in df.columns:
df['data.end_loc.coordinates'] = df['data.end_loc.coordinates'].apply(lambda x: f'({x[0]}, {x[1]})')
return df


@callback(
Output('tabs-content', 'children'),
Input('tabs-datatable', 'value'),
Expand All @@ -38,6 +48,7 @@ def render_content(tab, store_uuids, store_trips):
data, columns, has_perm = None, [], False
if tab == 'tab-uuids-datatable':
data = store_uuids["data"]
data = db_utils.add_user_stats(data)
columns = perm_utils.get_uuids_columns()
has_perm = perm_utils.has_permission('data_uuids')
elif tab == 'tab-trips-datatable':
Expand All @@ -52,11 +63,7 @@ def render_content(tab, store_uuids, store_trips):
return None

df = df.drop(columns=[col for col in df.columns if col not in columns])

if 'data.start_loc.coordinates' in df.columns:
df['data.start_loc.coordinates'] = df['data.start_loc.coordinates'].apply(lambda x: f'({x[0]}, {x[1]})')
if 'data.end_loc.coordinates' in df.columns:
df['data.end_loc.coordinates'] = df['data.end_loc.coordinates'].apply(lambda x: f'({x[0]}, {x[1]})')
df = clean_location_data(df)

return populate_datatable(df)

Expand Down
10 changes: 10 additions & 0 deletions utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@
'user_token',
'user_id',
'update_ts',
'total_trips',
'labeled_trips',
'first_trip',
'last_trip',
'last_call',
'platform',
'manufacturer',
'app_version',
'os_version',
'phone_lang',
]
65 changes: 65 additions & 0 deletions utils/db_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
from datetime import datetime, timezone
from uuid import UUID

import arrow

import pandas as pd
import pymongo

import emission.core.get_database as edb
import emission.storage.timeseries.abstract_timeseries as esta
Expand Down Expand Up @@ -94,3 +97,65 @@ def query_confirmed_trips(start_date, end_date):
# logging.debug("After filtering, the actual data is %s" % df.head())
# logging.debug("After filtering, the actual data is %s" % df.head().trip_start_time_str)
return df


def add_user_stats(user_data):
for user in user_data:
user_uuid = UUID(user['user_id'])

# TODO: Use the time-series functions when the needed functionality is added.
total_trips = edb.get_analysis_timeseries_db().count_documents(
{
'user_id': user_uuid,
'metadata.key': 'analysis/confirmed_trip',
}
)
user['total_trips'] = total_trips

labeled_trips = edb.get_analysis_timeseries_db().count_documents(
{
'user_id': user_uuid,
'metadata.key': 'analysis/confirmed_trip',
'data.user_input': {'$ne': {}},
}
)
user['labeled_trips'] = labeled_trips

profile_data = edb.get_profile_db().find_one({'user_id': user_uuid})
user['platform'] = profile_data.get('curr_platform')
user['manufacturer'] = profile_data.get('manufacturer')
user['app_version'] = profile_data.get('client_app_version')
user['os_version'] = profile_data.get('client_os_version')
user['phone_lang'] = profile_data.get('phone_lang')




if total_trips > 0:
time_format = 'YYYY-MM-DD HH:mm:ss'
ts = esta.TimeSeries.get_time_series(user_uuid)
start_ts = ts.get_first_value_for_field(
key='analysis/confirmed_trip',
field='data.end_ts',
sort_order=pymongo.ASCENDING
)
if start_ts != -1:
user['first_trip'] = arrow.get(start_ts).format(time_format)

end_ts = ts.get_first_value_for_field(
key='analysis/confirmed_trip',
field='data.end_ts',
sort_order=pymongo.DESCENDING
)
if end_ts != -1:
user['last_trip'] = arrow.get(end_ts).format(time_format)

last_call = ts.get_first_value_for_field(
key='stats/server_api_time',
field='data.ts',
sort_order=pymongo.DESCENDING
)
if last_call != -1:
user['last_call'] = arrow.get(last_call).format(time_format)

return user_data

0 comments on commit 6f0c381

Please sign in to comment.