def report_result_json(result_key):
    celery_task, pj = get_celery_task(result_key)
    if not celery_task:
        return json_error('no task exists with id: {0}'.format(result_key))

    if celery_task.ready() and celery_task.successful():
        result = celery_task.get()
        json_result = pj.get_json_result(result)

        return json_response(json_result)
    else:
        return json_response(status=celery_task.status)
def report_result_json(result_key):
    celery_task, pj = get_celery_task(result_key)
    if not celery_task:
        return json_error('no task exists with id: {0}'.format(result_key))

    if celery_task.ready() and celery_task.successful():
        task_result = get_celery_task_result(celery_task, pj)

        return json_response(
            result=task_result,
            parameters=prettify_parameters(pj),
        )
    else:
        return json_response(status=celery_task.status)
def cohort_detail(name_or_id):
    """
    Returns a JSON object of the form:
    {id: 2, name: 'Berlin Beekeeping Society', description: '', wikiusers: [
        {mediawiki_username: '******', mediawiki_userid: 5, project: 'dewiki'},
        {mediawiki_username: '******', mediawiki_userid: 6, project: 'dewiki'},
        {mediawiki_username: '******', mediawiki_userid: 7, project: 'dewiki'},
        {mediawiki_username: '******', mediawiki_userid: 8, project: 'dewiki'},
    ]}
    """
    cohort = None
    db_session = db.get_session()
    try:
        kargs = dict()
        if str(name_or_id).isdigit():
            kargs['by_id'] = int(name_or_id)
        else:
            kargs['by_name'] = name_or_id
        cohort = g.cohort_service.get_for_display(db_session, current_user.id, **kargs)

        cohort_dict = cohort.__dict__
        cohort_dict['tags'] = populate_cohort_tags(cohort.id, db_session)

        cohort_dict['validation'] =\
            populate_cohort_validation_status(cohort, db_session, cohort.size)

    # don't need to roll back session because it's just a query
    except Unauthorized:
        return 'You are not allowed to access this Cohort', 401
    except NoResultFound:
        return 'Could not find this Cohort', 404

    return json_response(cohort_dict)
def unset_public_report(report_id):
    """
    Deletes the specified report from disk, and sets the public flag to False.
    """
    # call would throw an exception if  report cannot be made private
    ReportStore.make_report_private(report_id, current_user.id, g.file_manager)
    return json_response(message='Update successful')
Example #5
0
def unset_public_report(report_id):
    """
    Deletes the specified report from disk, and sets the public flag to False.
    """
    # call would throw an exception if  report cannot be made private
    ReportStore.make_report_private(report_id, current_user.id, g.file_manager)
    return json_response(message='Update successful')
def cohort_detail(name_or_id):
    """
    Returns a JSON object of the form:
    {id: 2, name: 'Berlin Beekeeping Society', description: '', wikiusers: [
        {mediawiki_username: '******', mediawiki_userid: 5, project: 'dewiki'},
        {mediawiki_username: '******', mediawiki_userid: 6, project: 'dewiki'},
        {mediawiki_username: '******', mediawiki_userid: 7, project: 'dewiki'},
        {mediawiki_username: '******', mediawiki_userid: 8, project: 'dewiki'},
    ]}
    """
    full_detail = request.args.get('full_detail', 0)
    
    cohort = None
    db_session = db.get_session()
    try:
        kargs = dict()
        if str(name_or_id).isdigit():
            kargs['by_id'] = int(name_or_id)
        else:
            kargs['by_name'] = name_or_id
        cohort = g.cohort_service.get_for_display(db_session, current_user.id, **kargs)
    except Unauthorized:
        return 'You are not allowed to access this Cohort', 401
    except NoResultFound:
        return 'Could not find this Cohort', 404
    finally:
        db_session.close()
    
    limit = 200 if full_detail == 'true' else 3
    cohort_with_wikiusers = populate_cohort_wikiusers(cohort, limit)
    cohort_with_tags = populate_cohort_tags(cohort_with_wikiusers, cohort.id)
    cohort_with_status = populate_cohort_validation_status(cohort_with_tags)
    return json_response(cohort_with_status)
Example #7
0
def report_result_json(result_key):
    celery_task, pj = get_celery_task(result_key)
    if not celery_task:
        return json_error('no task exists with id: {0}'.format(result_key))

    if celery_task.ready() and celery_task.successful():
        result = celery_task.get()
        json_result = pj.get_json_result(result)
        if Aggregation.IND in result[result_key]:
            user_names = get_usernames_for_task_result(result[result_key])
            json_result_with_names = add_user_names_to_json(json_result,
                                                            user_names)
            return json_response(json_result_with_names)
        else:
            return json_response(json_result)
    else:
        return json_response(status=celery_task.status)
def delete_tag(cohort_id, tag_id):
    session = db.get_session()
    session.query(CohortTagStore) \
        .filter(CohortTagStore.cohort_id == cohort_id) \
        .filter(CohortTagStore.tag_id == tag_id) \
        .delete()
    session.commit()

    tags = g.tag_service.get_all_tags(session)
    return json_response(message='success', tagsAutocompleteList=json.dumps(tags))
def delete_tag(cohort_id, tag_id):
    session = db.get_session()
    try:
        session.query(CohortTagStore) \
            .filter(CohortTagStore.cohort_id == cohort_id) \
            .filter(CohortTagStore.tag_id == tag_id) \
            .delete()
        session.commit()
        return json_response(message='success')
    finally:
        session.close()
def add_tag(cohort_id, tag):
    """
    Checks if tag exists in the tag table and then adds tag to the cohort.
    """
    if tag is None:
        return json_error(message='You cannot submit an empty tag.')
    parsed_tag = parse_tag(tag)
    session = db.get_session()
    data = {}
    try:
        t = session.query(TagStore).filter(TagStore.name == parsed_tag).first()
        if not t:
            t = TagStore(
                name=parsed_tag
            )
            session.add(t)
            session.commit()

        # Check if cohort is already tagged with 'tag'
        try:
            if g.cohort_service.get_tag(session, t, cohort_id, current_user.id):
                return json_response(exists=True)
        except Unauthorized:
            return json_error(message='You are not allowed to access this Cohort')

        # Add tag
        try:
            g.cohort_service.add_tag(session, t, cohort_id, current_user.id)
        except Unauthorized:
            return json_error(message='You are not allowed to access this Cohort')

        data['tags'] = populate_cohort_tags(cohort_id, session)

        tagsAutocompleteList = g.tag_service.get_all_tags(session)
        data['tagsAutocompleteList'] = json.dumps(tagsAutocompleteList)

    except DatabaseError as e:
        session.rollback()
        return json_error(e.message)

    return json_response(data)
def add_tag(cohort_id, tag):
    """
    Checks if tag exists in the tag table and then adds tag to the cohort.
    """
    if tag is None:
        return json_error(message='You cannot submit an empty tag.')
    parsed_tag = parse_tag(tag)
    session = db.get_session()
    try:
        t = session.query(TagStore).filter(TagStore.name == parsed_tag).first()
        if not t:
            t = TagStore(
                name=parsed_tag
            )
            session.add(t)
            session.commit()

        # Check if cohort is already tagged with 'tag'
        ct = session.query(CohortTagStore) \
            .filter(CohortTagStore.tag_id == t.id) \
            .filter(CohortTagStore.cohort_id == cohort_id) \
            .all()
        if ct:
            return json_response(exists=True)

        cohort_tag = CohortTagStore(
            tag_id=t.id,
            cohort_id=cohort_id,
        )
        session.add(cohort_tag)
        session.commit()
    except DatabaseError as e:
        session.rollback()
        return json_error(e.message)
    finally:
        session.close()

    data = {}
    populate_cohort_tags(data, cohort_id)
    return json_response(data)
def validate_cohort(cohort_id):
    name = None
    session = db.get_session()
    try:
        c = g.cohort_service.get_for_display(session, current_user.id, by_id=cohort_id)
        name = c.name
        # TODO we need some kind of global config that is not db specific
        vc = ValidateCohort(c)
        vc.task.delay(vc)
        return json_response(message='Validating cohort "{0}"'.format(name))
    except Unauthorized:
        return json_error('You are not allowed to access this cohort')
    except NoResultFound:
        return json_error('This cohort does not exist')
def cohorts_list():
    include_invalid = request.args.get('include_invalid', 'false') == 'true'
    db_session = db.get_session()
    if include_invalid:
        cohorts = g.cohort_service.get_list_for_display(db_session, current_user.id)
    else:
        cohorts = g.cohort_service.get_list(db_session, current_user.id)

    return json_response(cohorts=[{
        'id': c.id,
        'name': c.name,
        'description': c.description,
        'default_project': c.default_project,
        'centralauth': type(c) == CentralAuthCohort,
    } for c in cohorts])
def cohort_invalid_detail(cohort_id):
    session = db.get_session()
    try:
        cohort = g.cohort_service.get_for_display(
            session, current_user.id, by_id=cohort_id
        )
        wikiusers = session\
            .query(WikiUserStore.mediawiki_username, WikiUserStore.reason_invalid)\
            .filter(WikiUserStore.validating_cohort == cohort.id) \
            .filter(WikiUserStore.valid.in_([False, None])) \
            .all()
        return json_response(invalid_wikiusers=[wu._asdict() for wu in wikiusers])
    except Exception, e:
        app.logger.exception(str(e))
        return json_error('Error fetching invalid users for {0}'.format(cohort_id))
def delete_cohort_wikiuser(cohort_id):
    """
    Deletes all WikiUsers from the given Cohort with
    raw_id_or_name == <raw_id_or_name>. If the invalidOnly
    flag is passed, it will delete the ones that are invalid only.
    """
    raw_id_or_name = request.form.get('raw_id_or_name')
    invalid_only = True if request.form.get('invalidOnly') == 'true' else False
    session = db.get_session()
    try:
        g.cohort_service.delete_cohort_wikiuser(raw_id_or_name, cohort_id,
                                                current_user.id, session, invalid_only)
        return json_response(message='success')
    except Exception as e:
        app.logger.exception(str(e))
        return json_error(e.message)
def reports_list():
    db_session = db.get_session()
    try:
        reports = db_session.query(ReportStore)\
            .filter(ReportStore.user_id == current_user.id)\
            .filter(ReportStore.created > thirty_days_ago())\
            .filter(ReportStore.show_in_ui)\
            .all()
        # TODO: update status for all reports at all times (not just show_in_ui ones)
        # update status for each report
        for report in reports:
            report.update_status()

        # TODO fix json_response to deal with ReportStore objects
        reports_json = json_response(reports=[report._asdict() for report in reports])
    finally:
        db_session.close()
    return reports_json
Example #17
0
def set_public_report(report_id):
    """
    Client facing method with pretty url to set/uset one report as public
    """

    # in order to move code to the PersistenReport class need to fetch report
    # data here
    db_session = db.get_session()
    result_key = db_session.query(ReportStore.result_key)\
        .filter(ReportStore.id == report_id)\
        .one()[0]

    data = report_result_json(result_key).data

    # call would throw an exception if report cannot be made public
    ReportStore.make_report_public(
        report_id, current_user.id, g.file_manager, data
    )

    return json_response(message='Update successful')
def cohort_membership(cohort_id):
    """
    If full_detail flag is set to 'true', it returns a json with
    a list of wikiusers grouped by username.
    Otherwise, it renders the html with basic cohort information.
    """
    session = db.get_session()
    try:
        cohort = g.cohort_service.get_for_display(
            session, current_user.id, by_id=cohort_id
        )
        if request.args.get('full_detail') == 'true':
            membership = g.cohort_service.get_membership(cohort, session)
            return json_response({'membership': membership})
        else:
            return render_template('cohort_membership.html', cohort=cohort)
    except Exception, e:
        # don't need to roll back session because it's just a query
        app.logger.exception(str(e))
        return 'Error fetching membership for this cohort', 500
def report_result_csv(result_key):
    celery_task, pj = get_celery_task(result_key)
    if not celery_task:
        return json_error('no task exists with id: {0}'.format(result_key))

    if celery_task.ready() and celery_task.successful():
        task_result = get_celery_task_result(celery_task, pj)
        p = prettify_parameters(pj)

        if 'Metric_timeseries' in p and p['Metric_timeseries'] != TimeseriesChoices.NONE:
            csv_io = get_timeseries_csv(task_result, pj, p)
        else:
            csv_io = get_simple_csv(task_result, pj, p)

        res = Response(csv_io.getvalue(), mimetype='text/csv')
        res.headers['Content-Disposition'] =\
            'attachment; filename={0}.csv'.format(pj.name)
        return res
    else:
        return json_response(status=celery_task.status)
Example #20
0
def report_result_csv(result_key):
    celery_task, pj = get_celery_task(result_key)
    if not celery_task:
        return json_error('no task exists with id: {0}'.format(result_key))

    if celery_task.ready() and celery_task.successful():
        result = celery_task.get()
        task_result = pj.get_result_safely(result)
        p = pj.pretty_parameters()

        user_names = get_usernames_for_task_result(task_result)

        if 'Metric_timeseries' in p and p['Metric_timeseries'] != TimeseriesChoices.NONE:
            csv_io = get_timeseries_csv(task_result, pj, p, user_names)
        else:
            csv_io = get_simple_csv(task_result, pj, p, user_names)

        res = Response(csv_io.getvalue(), mimetype='text/csv')
        res.headers['Content-Disposition'] =\
            'attachment; filename={0}.csv'.format(pj.name)
        return res
    else:
        return json_response(status=celery_task.status)
def cohorts_list():
    include_invalid = request.args.get('include_invalid', 'false')
    db_session = db.get_session()
    try:
        cohorts = db_session\
            .query(CohortStore.id, CohortStore.name, CohortStore.description)\
            .join(CohortUserStore)\
            .join(UserStore)\
            .filter(UserStore.id == current_user.id)\
            .filter(CohortUserStore.role.in_(CohortUserRole.SAFE_ROLES))\
            .filter(CohortStore.enabled)\
            .filter(or_(
                CohortStore.validated,
                (include_invalid == 'true')
            ))\
            .all()
    finally:
        db_session.close()
    
    return json_response(cohorts=[{
        'id': c.id,
        'name': c.name,
        'description': c.description,
    } for c in cohorts])
Example #22
0
def reports_list():
    db_session = db.get_session()
    # Joins with TaskError to get the error message for failed reports.
    report_tuples = db_session.query(ReportStore, TaskErrorStore.message)\
        .outerjoin(TaskErrorStore)\
        .filter(ReportStore.user_id == current_user.id)\
        .filter(or_(ReportStore.created > thirty_days_ago(), ReportStore.recurrent))\
        .filter(ReportStore.show_in_ui)\
        .filter(or_(
            TaskErrorStore.task_type == 'report',
            TaskErrorStore.task_type == None))\
        .all()
    # TODO: update status for all reports at all times (not just show_in_ui ones)
    # update status for each report and build response
    reports = []
    for report_tuple in report_tuples:
        report = report_tuple.ReportStore
        report.update_status()
        report_dict = report._asdict()
        report_dict['error_message'] = report_tuple.message
        reports.append(report_dict)

    # TODO fix json_response to deal with ReportStore objects
    return json_response(reports=reports)
Example #23
0
def report_status(result_key):
    celery_task, pj = get_celery_task(result_key)
    return json_response(status=celery_task.status)
def report_status(result_key):
    celery_task, pj = get_celery_task(result_key)
    return json_response(status=celery_task.status)
Example #25
0
def rerun_report(report_id):
    session = db.get_session()
    report = session.query(ReportStore).get(report_id)
    RunReport.rerun(report)
    return json_response(message='Report scheduled for rerun')