Example #1
0
def students_with_alerts(cohort_id):
    benchmark = get_benchmarker(f'cohort {cohort_id} students_with_alerts')
    benchmark('begin')
    offset = get_param(request.args, 'offset', 0)
    limit = get_param(request.args, 'limit', 50)
    cohort = CohortFilter.find_by_id(
        cohort_id,
        include_alerts_for_user_id=current_user.get_id(),
        include_students=False,
        alert_offset=offset,
        alert_limit=limit,
    )
    benchmark('fetched cohort')
    if cohort and _can_current_user_view_cohort(cohort):
        _decorate_cohort(cohort)
        students = cohort.get('alerts', [])
        alert_sids = [s['sid'] for s in students]
        alert_profiles = get_summary_student_profiles(alert_sids)
        benchmark('fetched student profiles')
        alert_profiles_by_sid = {p['sid']: p for p in alert_profiles}
        for student in students:
            student.update(alert_profiles_by_sid[student['sid']])
            # The enrolled units count is the one piece of term data we want to preserve.
            if student.get('term'):
                student['term'] = {
                    'enrolledUnits': student['term'].get('enrolledUnits')
                }
    else:
        raise ResourceNotFoundError(
            f'No cohort found with identifier: {cohort_id}')
    benchmark('end')
    return tolerant_jsonify(students)
def _curated_group_with_complete_student_profiles(curated_group_id,
                                                  order_by='last_name',
                                                  offset=0,
                                                  limit=50):
    benchmark = get_benchmarker(
        f'curated group {curated_group_id} with student profiles')
    benchmark('begin')
    curated_group = CuratedGroup.find_by_id(curated_group_id)
    if not curated_group:
        raise ResourceNotFoundError(
            f'Sorry, no curated group found with id {curated_group_id}.')
    if curated_group.owner_id != current_user.get_id():
        raise ForbiddenRequestError(
            f'Current user, {current_user.get_uid()}, does not own curated group {curated_group.id}'
        )
    api_json = curated_group.to_api_json(order_by=order_by,
                                         offset=offset,
                                         limit=limit)
    sids = [s['sid'] for s in api_json['students']]
    benchmark('begin profile query')
    api_json['students'] = get_summary_student_profiles(sids)
    benchmark('begin alerts query')
    Alert.include_alert_counts_for_students(
        viewer_user_id=current_user.get_id(), group=api_json)
    benchmark('end')
    return api_json
def get_students_with_alerts(curated_group_id):
    offset = get_param(request.args, 'offset', 0)
    limit = get_param(request.args, 'limit', 50)
    benchmark = get_benchmarker(
        f'curated group {curated_group_id} students_with_alerts')
    benchmark('begin')
    curated_group = CuratedGroup.find_by_id(curated_group_id)
    if not curated_group:
        raise ResourceNotFoundError(
            f'Sorry, no curated group found with id {curated_group_id}.')
    if curated_group.owner_id != current_user.get_id():
        raise ForbiddenRequestError(
            f'Current user, {current_user.get_uid()}, does not own curated group {curated_group.id}'
        )
    benchmark('begin alerts query')
    students = Alert.include_alert_counts_for_students(
        viewer_user_id=current_user.get_id(),
        group={'sids': CuratedGroup.get_all_sids(curated_group_id)},
        count_only=True,
        offset=offset,
        limit=limit,
    )
    benchmark('end alerts query')
    alert_count_per_sid = {}
    for s in list(filter(lambda s: s.get('alertCount') > 0, students)):
        sid = s.get('sid')
        alert_count_per_sid[sid] = s.get('alertCount')
    sids = list(alert_count_per_sid.keys())
    benchmark('begin profile query')
    students_with_alerts = get_summary_student_profiles(sids=sids)
    benchmark('end profile query')
    for student in students_with_alerts:
        student['alertCount'] = alert_count_per_sid[student['sid']]
    benchmark('end')
    return tolerant_jsonify(students_with_alerts)
Example #4
0
def _curated_group_with_complete_student_profiles(curated_group_id,
                                                  order_by='last_name',
                                                  term_id=None,
                                                  offset=0,
                                                  limit=50):
    benchmark = get_benchmarker(
        f'curated group {curated_group_id} with student profiles')
    benchmark('begin')
    curated_group = CuratedGroup.find_by_id(curated_group_id)
    if not curated_group:
        raise ResourceNotFoundError(
            f'Sorry, no curated group found with id {curated_group_id}.')
    if not _can_current_user_view_curated_group(curated_group):
        raise ForbiddenRequestError(
            f'Current user, {current_user.get_uid()}, cannot view curated group {curated_group.id}'
        )
    api_json = curated_group.to_api_json(order_by=order_by,
                                         offset=offset,
                                         limit=limit)
    sids = [s['sid'] for s in api_json['students']]
    benchmark('begin profile query')
    api_json['students'] = get_summary_student_profiles(
        sids, term_id=term_id, include_historical=True)
    benchmark('begin alerts query')
    Alert.include_alert_counts_for_students(
        viewer_user_id=current_user.get_id(), group=api_json)
    benchmark('end')
    benchmark('begin get_referencing_cohort_ids')
    api_json[
        'referencingCohortIds'] = curated_group.get_referencing_cohort_ids()
    benchmark('end')
    return api_json
Example #5
0
def my_cohorts():
    uid = current_user.get_id()
    cohorts = [
        decorate_cohort(c, include_alerts_for_uid=uid, include_students=False)
        for c in CohortFilter.all_owned_by(uid)
    ]
    alert_sids = []
    for cohort in cohorts:
        alert_sids += [a['sid'] for a in cohort.get('alerts', [])]
    alert_profiles = get_summary_student_profiles(alert_sids)
    alert_profiles_by_sid = {p['sid']: p for p in alert_profiles}
    for cohort in cohorts:
        for alert in cohort.get('alerts', []):
            alert.update(alert_profiles_by_sid[alert['sid']])
            strip_analytics(alert)
    return tolerant_jsonify(cohorts)
Example #6
0
def get_curated_cohort(curated_cohort_id):
    cohort = CuratedCohort.find_by_id(curated_cohort_id)
    if not cohort:
        raise ResourceNotFoundError(
            f'Sorry, no cohort found with id {curated_cohort_id}.')
    if cohort.owner_id != current_user.id:
        raise ForbiddenRequestError(
            f'Current user, {current_user.uid}, does not own cohort {cohort.id}'
        )
    cohort = cohort.to_api_json(sids_only=True)
    sids = [s['sid'] for s in cohort['students']]
    cohort['students'] = get_summary_student_profiles(sids)
    cohort['students'] = api_util.sort_students_by_name(cohort['students'])
    Alert.include_alert_counts_for_students(viewer_uid=current_user.uid,
                                            cohort=cohort)
    return tolerant_jsonify(cohort)
Example #7
0
def my_curated_cohorts():
    alert_counts = Alert.current_alert_counts_for_viewer(current_user.id)
    curated_cohorts = CuratedCohort.get_curated_cohorts_by_owner_id(
        current_user.id)
    curated_cohorts = sorted(curated_cohorts,
                             key=lambda curated_cohort: curated_cohort.name)
    curated_cohorts = [c.to_api_json(sids_only=True) for c in curated_cohorts]
    for curated_cohort in curated_cohorts:
        students = curated_cohort['students']
        api_util.add_alert_counts(alert_counts, students)
        # Only get detailed data for students with alerts.
        sids_with_alerts = [s['sid'] for s in students if s.get('alertCount')]
        students_with_alerts = get_summary_student_profiles(sids_with_alerts)
        for data in students_with_alerts:
            data['alertCount'] = next(
                s.get('alertCount') for s in students
                if s['sid'] == data['sid'])
            api_util.strip_analytics(data)
        curated_cohort['students'] = api_util.sort_students_by_name(
            students_with_alerts)
    return tolerant_jsonify(curated_cohorts)
Example #8
0
def get_cohort_events(cohort_id):
    cohort = CohortFilter.find_by_id(cohort_id, include_students=False)
    if not cohort or not _can_current_user_view_cohort(cohort):
        raise ResourceNotFoundError(
            f'No cohort found with identifier: {cohort_id}')
    if cohort['domain'] != 'default':
        raise BadRequestError(
            f"Cohort events are not supported in domain {cohort['domain']}")

    offset = get_param(request.args, 'offset', 0)
    limit = get_param(request.args, 'limit', 50)
    results = CohortFilterEvent.events_for_cohort(cohort_id, offset, limit)
    count = results['count']
    events = results['events']
    event_sids = [e.sid for e in events]
    event_profiles_by_sid = {
        e['sid']: e
        for e in get_summary_student_profiles(event_sids,
                                              include_historical=True)
    }

    def _event_feed(event):
        profile = event_profiles_by_sid.get(event.sid, {})
        return {
            'createdAt': event.created_at.isoformat(),
            'eventType': event.event_type,
            'firstName': profile.get('firstName'),
            'lastName': profile.get('lastName'),
            'sid': event.sid,
            'uid': profile.get('uid'),
        }

    feed = {
        'count': count,
        'events': [_event_feed(e) for e in events],
    }
    return tolerant_jsonify(feed)