def index_old(self): from gviz_data_table import Table from rockpack.mainsite.services.user.models import User, UserActivity user_count = readonly_session.query(func.count(User.id)).\ filter(User.refresh_token != '').scalar() header = ('user count', 'max lifetime', 'avg lifetime', 'stddev lifetime', 'max active days', 'avg active days', 'stddev active days') lifetime = func.date_part('days', func.max(UserActivity.date_actioned) - func.min(UserActivity.date_actioned)).label('lifetime') active_days = func.count(func.distinct(func.date( UserActivity.date_actioned))).label('active_days') activity = readonly_session.query(UserActivity.user, lifetime, active_days).\ group_by(UserActivity.user) ctx = {} for key, having_expr in ('all', None), ('1day', lifetime > 1), ('7day', lifetime > 7): data = activity.having(having_expr).from_self( func.count('*'), func.max(lifetime), func.avg(lifetime), func.stddev_samp(lifetime), func.max(active_days), func.avg(active_days), func.stddev_samp(active_days) ).one() table = Table([ dict(id='metric', type=str), dict(id='value', type=float), dict(id='%', type=str), ]) pdata = ('%d%%' % (data[0] * 100 / user_count),) + ('',) * 6 table.extend(zip(*(header, map(float, data), pdata))) ctx['ret_%s_data' % key] = table.encode() return self.render('admin/retention_stats_old.html', **ctx)
def index(self): from gviz_data_table import Table from rockpack.mainsite.services.user.models import User from .models import AppDownloadRecord ctx = {} for action in 'download', 'update': table = Table((dict(id='date', type=date), dict(id='count', type=long))) table.extend( readonly_session.query(AppDownloadRecord).filter_by(action=action). group_by('1').order_by('1'). values(AppDownloadRecord.date, func.sum(AppDownloadRecord.count)) ) ctx['%s_data' % action] = table.encode() table = Table([dict(id='date', type=date)] + [dict(id=i, type=long) for i in 'Total', 'US', 'UK', 'Other'])