示例#1
0
文件: app.py 项目: tikki-fi/tikki
def get_schema():
    log.info('schema')
    try:
        jwt_id = get_jwt_identity()
        type_dict = dict()
        if jwt_id is not None:
            filters = {'user_id': str(jwt_id)}
            records = db_api.get_rows(Record, filters)

            # sort records based on user_id and creation date to pick most recent
            # result per user
            # TODO: sort in db.get_rows

            records.sort(key=lambda x: (x.type_id, x.created_at), reverse=True)
            lag_type_id = None
            for record in records:
                if lag_type_id != record.type_id:
                    lag_type_id = record.type_id
                    type_dict[record.type_id] = record

        rows = db_api.get_rows(RecordType, {})
        result_list = list()
        for row in rows:
            result = row.json_dict
            result['ask'] = 1 if jwt_id is not None and row.category_id == 2 and \
                row.id not in type_dict else 0
            result_list.append(result)
        return utils.flask_return_success(result_list)
    except Exception as ex:
        return utils.flask_handle_exception(ex)
示例#2
0
文件: app.py 项目: tikki-fi/tikki
def get_user_event_link():
    filters = utils.get_args(
        received=request.args,
        optional={
            'user_id': str,
            'event_id': str
        },
    )
    try:
        rows = db_api.get_rows(UserEventLink, filters)
        return utils.flask_return_success([row.json_dict for row in rows])
    except Exception as e:
        return utils.flask_handle_exception(e)
示例#3
0
文件: app.py 项目: tikki-fi/tikki
def get_user():
    filters = utils.get_args(
        received=request.args,
        optional={
            'id': str,
            'username': str
        },
    )
    try:
        users = db_api.get_rows(User, filters)
        return utils.flask_return_success([i.json_dict for i in users])
    except Exception as e:
        return utils.flask_handle_exception(e)
示例#4
0
文件: app.py 项目: tikki-fi/tikki
def get_event():
    filters = utils.get_args(
        received=request.args,
        optional={
            'id': str,
            'user_id': str,
            'type_id': int
        },
    )
    try:
        rows = db_api.get_rows(Event, filters)
        return utils.flask_return_success([row.json_dict for row in rows])
    except Exception as e:
        return utils.flask_handle_exception(e)
示例#5
0
文件: app.py 项目: tikki-fi/tikki
def get_record():
    filters = utils.get_args(
        received=request.args,
        constant={'user_id': get_jwt_identity()},
        optional={
            'id': str,
            'event_id': str,
            'type_id': int
        },
    )
    try:
        rows = db_api.get_rows(Record, filters)
        return utils.flask_return_success([row.json_dict for row in rows])
    except Exception as e:
        return utils.flask_handle_exception(e)
示例#6
0
文件: app.py 项目: tikki-fi/tikki
def get_pushup60test_compstat():
    try:
        user_id = get_jwt_identity()
        if user_id is None:
            return jsonify({'message': 'Undefined user id.'}), 400
        filters = {'type_id': int(db_metadata.RecordTypeEnum.PUSH_UP_60_TEST)}
        records = db_api.get_rows(Record, filters)

        # sort records based on user_id and creation date to pick most recent
        # result per user
        # TODO: sort in db.get_rows

        records.sort(key=lambda x: (x.user_id, x.created_at), reverse=True)
        filtered_records = list()
        lag_user_id = None
        user_record = None
        for record in records:
            if lag_user_id != record.user_id:
                lag_user_id = record.user_id
                filtered_records.append(record)
                if user_id == str(record.user_id):
                    user_record = record

        # sort filtered records and get index for quantile calculation
        filtered_records.sort(key=lambda x: x.payload['pushups'])
        try:
            index = filtered_records.index(user_record)
        except ValueError:
            index = None

        if index is None or len(filtered_records) == 0:
            quantile = 0
        else:
            quantile = (index + 1) / len(filtered_records)
        return jsonify({'result': {'quantile': quantile}}), 200

    except Exception as e:
        return utils.flask_return_exception(e, 500)
示例#7
0
文件: app.py 项目: tikki-fi/tikki
def get_cooperstest_compstat():
    try:
        user_id = get_jwt_identity()
        filters = {'type_id': int(db_metadata.RecordTypeEnum.COOPERS_TEST)}
        records = db_api.get_rows(Record, filters)

        # sort records based on user_id and creation date to pick most recent
        # result per user
        # TODO: do sorting in db.get_rows
        records.sort(key=lambda x: (x.user_id, x.created_at), reverse=True)
        filtered_records = list()
        lag_user_id = None
        user_record = None
        for record in records:
            if lag_user_id != record.user_id:
                lag_user_id = record.user_id
                filtered_records.append(record)
                if user_id == str(record.user_id):
                    user_record = record

        # sort filtered records and get index for quantile calculation
        try:
            index = sorted(
                filtered_records,
                key=lambda x: x.payload['distance']).index(user_record)
        except ValueError:
            index = None

        if index is None or len(filtered_records) == 0:
            quantile = 0
        else:
            quantile = (index + 1) / len(filtered_records)
        return utils.flask_return_success({'quantile': quantile})

    except Exception as e:
        return utils.flask_return_exception(e, 500)