def user_activity_intervals() -> Tuple[(mark_safe, Dict[(str, float)])]:
    day_end = timestamp_to_datetime(time.time())
    day_start = (day_end - timedelta(hours=24))
    output = 'Per-user online duration for the last 24 hours:\n'
    total_duration = timedelta(0)
    all_intervals = UserActivityInterval.objects.filter(end__gte=day_start, start__lte=day_end).select_related('user_profile', 'user_profile__realm').only('start', 'end', 'user_profile__delivery_email', 'user_profile__realm__string_id').order_by('user_profile__realm__string_id', 'user_profile__delivery_email')
    by_string_id = (lambda row: row.user_profile.realm.string_id)
    by_email = (lambda row: row.user_profile.delivery_email)
    realm_minutes = {}
    for (string_id, realm_intervals) in itertools.groupby(all_intervals, by_string_id):
        realm_duration = timedelta(0)
        output += f'''<hr>{string_id}
'''
        for (email, intervals) in itertools.groupby(realm_intervals, by_email):
            duration = timedelta(0)
            for interval in intervals:
                start = max(day_start, interval.start)
                end = min(day_end, interval.end)
                duration += (end - start)
            total_duration += duration
            realm_duration += duration
            output += f'''  {email:<37}{duration}
'''
        realm_minutes[string_id] = (realm_duration.total_seconds() / 60)
    output += f'''
Total duration:                      {total_duration}
'''
    output += f'''
Total duration in minutes:           {(total_duration.total_seconds() / 60.0)}
'''
    output += f'Total duration amortized to a month: {((total_duration.total_seconds() * 30.0) / 60.0)}'
    content = mark_safe((('<pre>' + output) + '</pre>'))
    return (content, realm_minutes)
def user_activity_intervals() -> Tuple[(mark_safe, Dict[(str, float)])]:
    day_end = timestamp_to_datetime(time.time())
    day_start = (day_end - timedelta(hours=24))
    output = 'Per-user online duration for the last 24 hours:\n'
    total_duration = timedelta(0)
    all_intervals = UserActivityInterval.objects.filter(end__gte=day_start, start__lte=day_end).select_related('user_profile', 'user_profile__realm').only('start', 'end', 'user_profile__delivery_email', 'user_profile__realm__string_id').order_by('user_profile__realm__string_id', 'user_profile__delivery_email')
    by_string_id = (lambda row: row.user_profile.realm.string_id)
    by_email = (lambda row: row.user_profile.delivery_email)
    realm_minutes = {}
    for (string_id, realm_intervals) in itertools.groupby(all_intervals, by_string_id):
        realm_duration = timedelta(0)
        output += f'<hr>{string_id}
'
        for (email, intervals) in itertools.groupby(realm_intervals, by_email):
            duration = timedelta(0)
            for interval in intervals:
                start = max(day_start, interval.start)
                end = min(day_end, interval.end)
                duration += (end - start)
            total_duration += duration
            realm_duration += duration
            output += f'  {email:<37}{duration}
'
        realm_minutes[string_id] = (realm_duration.total_seconds() / 60)
    output += f'
Total duration:                      {total_duration}
'
    output += f'
Total duration in minutes:           {(total_duration.total_seconds() / 60.0)}
'
    output += f'Total duration amortized to a month: {((total_duration.total_seconds() * 30.0) / 60.0)}'
    content = mark_safe((('<pre>' + output) + '</pre>'))
    return (content, realm_minutes)