コード例 #1
0
def update_fail_retry(data):
    fail_count = data.get('fail_count', 0) + 1
    ApiAccess.get_skynet_db().users.update_one(
        {'username': data['username'].upper()},
        {'$set': {
            'fail_count': fail_count
        }})
    if fail_count == _fail_retry_max:
        timeout_until = int(
            (datetime.now() +
             timedelta(minutes=_fail_retry_timeout)).timestamp())
        ApiAccess.get_skynet_db().users.update_one(
            {'username': data['username'].upper()},
            {'$set': {
                'fail_count': 0,
                'timeout_until': timeout_until
            }})
        return {
            'status': LoginStates.retry_fail.value,
            'timeout': utils.utc_ts_to_local_str(timeout_until)
        }
    elif fail_count > _fail_retry_max:
        ApiAccess.get_skynet_db().users.update_one(
            {'username': data['username'].upper()},
            {'$set': {
                'fail_count': 0
            }})
        return {
            'status': LoginStates.retry_fail.value,
            'timeout': utils.utc_ts_to_local_str(data.get('timeout_until', 0))
        }

    return {'status': LoginStates.fail.value}
コード例 #2
0
def get_strategies(size, page, sort, order, begin, end, name, strategy_id):
    query = {} if not name else {
        '$or': [{
            'name': {
                '$regex': '.*{}.*'.format(name),
                '$options': 'i'
            }
        }, {
            'team_name': {
                '$regex': '.*{}.*'.format(name),
                '$options': 'i'
            }
        }, {
            'description': {
                '$regex': '.*{}.*'.format(name),
                '$options': 'i'
            }
        }, {
            'key': {
                '$regex': '.*{}.*'.format(name),
                '$options': 'i'
            }
        }]
    }
    query = query if not strategy_id else {'_id': ObjectId(strategy_id)}

    if begin and end:
        query['date_added'] = {'$gte': begin, '$lte': end}
    elif begin:
        query['date_added'] = {'$gte': begin}
    elif end:
        query['date_added'] = {'$lte': end}

    total_elements = ApiAccess.get_revenyou_db().signal_provider.find(
        query).count()
    cursor = ApiAccess.get_revenyou_db().signal_provider.\
                find(query).\
                skip(page * size).\
                limit(size).\
                collation({'locale': 'en' })

    if order != '':
        cursor = cursor.sort(sort, -1 if order == 'desc' else 1)

    result = {
        'total_elements': total_elements,
        'total_pages': math.ceil(float(total_elements) / size),
        'current_page': page,
        'content': []
    }

    for document in cursor:
        result['content'].append(document)
        result['content'][-1]['_id'] = str(result['content'][-1]['_id'])
        result['content'][-1]['date_added'] = utils.utc_ts_to_local_str(
            document.get('date_added', None))
        result['content'][-1]['date_updated'] = utils.utc_ts_to_local_str(
            document.get('date_updated', None))

    return result
コード例 #3
0
def get_csv_exportable(cols):
    global _max_limit
    strategies = []
    skip_ = 0
    while True:
        partial = list(ApiAccess.get_revenyou_db().signal_provider.\
            find({}, {'_id': 0}).\
            skip(skip_).\
            limit(_max_limit))
        skip_ += _max_limit
        if len(partial) == 0:
            break
        strategies.extend(partial)

    result = []
    for strategy in strategies:
        result.append({})
        for col in cols:
            if col == 'date_added':
                result[-1]['date_added'] = utils.utc_ts_to_local_str(
                    strategy.get('date_added', None))
                continue
            if col == 'date_updated':
                result[-1]['date_updated'] = utils.utc_ts_to_local_str(
                    strategy.get('date_updated', None))
                continue

            result[-1][col] = strategy.get(col, '')

    return json.dumps(result)
コード例 #4
0
def get_user_subscribed_strategies(user_id, size, page, sort, order, begin,
                                   end):
    query = {'user_id': user_id}

    if begin and end:
        query['date_added'] = {'$gte': begin, '$lte': end}
    elif begin:
        query['date_added'] = {'$gte': begin}
    elif end:
        query['date_added'] = {'$lte': end}

    total_elements = ApiAccess.get_revenyou_db().subscribed.find(query).count()
    cursor = ApiAccess.get_revenyou_db().subscribed.\
                find(query).\
                skip(page * size).\
                limit(size).\
                collation({'locale': 'en' })

    if order != '':
        cursor = cursor.sort(sort, -1 if order == 'desc' else 1)
    cursor = list(cursor)

    result = {
        'total_elements': total_elements,
        'total_pages': math.ceil(float(total_elements) / size),
        'current_page': page,
        'content': []
    }

    signal_providers = ApiAccess.get_revenyou_db().signal_provider.\
        find({'_id': \
            {'$in' : [ObjectId(doc.get('strategy_id', '')) for doc in cursor]}},
            {'name': 1, 'pair': 1, 'icon_url': 1}).\
        limit(size)

    strategy_info = {}
    for item in signal_providers:
        strategy_info[str(item['_id'])] = {
            'strategy_name': item.get('name', ''),
            'strategy_pair': item.get('pair', ''),
            'strategy_icon': item.get('icon_url', '')
        }

    for document in cursor:
        result['content'].append(document)
        result['content'][-1]['_id'] = str(result['content'][-1]['_id'])
        result['content'][-1]['date_added'] = utils.utc_ts_to_local_str(
            document.get('date_added', None))
        result['content'][-1]['date_modified'] = utils.utc_ts_to_local_str(
            document.get('date_modified', None))
        strategy_id = document.get('strategy_id', None)
        if strategy_id is not None and strategy_id in strategy_info:
            result['content'][-1].update(strategy_info[strategy_id])

        pnl = (float(result['content'][-1]['updated_quantity']) - float(result['content'][-1]['quantity'])) / float(result['content'][-1]['quantity']) * 100 \
            if result['content'][-1]['updated_quantity'].isnumeric() else None
        result['content'][-1]['pnl'] = "{:.2f}%".format(pnl) if pnl else ''

    return result
コード例 #5
0
def get_customer_snapshot(user_id):
    cursor = ApiAccess.get_revenyou_db().sign_up.find({'user_id': user_id}, {
        '_id': 0,
        'youhex_name': 1,
        'status': 1,
        'date_added': 1
    })

    if cursor.count() == 0:
        return {}

    date_time = utils.utc_ts_to_local_str(cursor[0].get('date_added',
                                                        None)).split()
    result = {
        'date':
        date_time[0],
        'time': (date_time[1] if len(date_time) > 1 else ''),
        'operation':
        TimelineSteps.user_creation.value['operation'],
        'description':
        '{}: {}'.format(TimelineSteps.user_creation.value['description'],
                        cursor[0].get('youhex_name', '')),
        'status':
        cursor[0].get('status', UserStatus.unknown.value)
    }
    return result
コード例 #6
0
def get_customer_unsubscribed_snapshot(user_id):
    unsub_cursor = ApiAccess.get_revenyou_db().unsubscribed.find(
        {'user_id': user_id}, {
            '_id': 0,
            'bot_name': 1,
            'date_added': 1
        })

    result = []
    for document in unsub_cursor:
        date_time = utils.utc_ts_to_local_str(document.get('date_added',
                                                           None)).split()
        result.append({
            'date':
            date_time[0],
            'time': (date_time[1] if len(date_time) > 1 else ''),
            'operation':
            TimelineSteps.bot_unsubscription.value['operation'],
            'description':
            '{}: {}'.format(
                TimelineSteps.bot_unsubscription.value['description'],
                document.get('bot_name', '')),
            'status':
            SubscriptionStatus.active.value
        })

    return json.dumps(result)
コード例 #7
0
def get_customer_subscribed_snapshot(user_id):
    sub_cursor = ApiAccess.get_revenyou_db().subscribed.find(
        {'user_id': user_id}, {
            '_id': 0,
            'quantity': 1,
            'strategy_id': 1,
            'status': 1,
            'date_added': 1
        })

    result = []
    for document in sub_cursor:
        strategy_name = ApiAccess.get_revenyou_db().signal_provider.find_one({
            '_id':
            ObjectId(document.get('strategy_id', ''))
        }).get('name', '')
        date_time = utils.utc_ts_to_local_str(document.get('date_added',
                                                           None)).split()
        result.append({
            'date':
            date_time[0],
            'time': (date_time[1] if len(date_time) > 1 else ''),
            'operation':
            TimelineSteps.bot_subscription.value['operation'],
            'description':
            '{}: {}/{}'.format(
                TimelineSteps.bot_subscription.value['description'],
                strategy_name, document.get('quantity', '')),
            'status':
            document.get('status', SubscriptionStatus.unknown.value)
        })

    return json.dumps(result)
コード例 #8
0
def check_fail_retry(data):
    timeout = data.get('timeout_until', 0)
    if timeout > int(datetime.now().timestamp()):
        return {
            'status': LoginStates.retry_fail.value,
            'timeout': utils.utc_ts_to_local_str(timeout)
        }
    return None
コード例 #9
0
def get_user_unsubscribed_strategies(user_id, size, page, sort, order, begin,
                                     end):
    query = {'user_id': user_id}

    if begin and end:
        query['date_added'] = {'$gte': begin, '$lte': end}
    elif begin:
        query['date_added'] = {'$gte': begin}
    elif end:
        query['date_added'] = {'$lte': end}

    total_elements = ApiAccess.get_revenyou_db().unsubscribed.find(
        query).count()
    cursor = ApiAccess.get_revenyou_db().unsubscribed.\
                find(query).\
                skip(page * size).\
                limit(size).\
                collation({'locale': 'en' })

    if order != '':
        cursor = cursor.sort(sort, -1 if order == 'desc' else 1)

    result = {
        'total_elements': total_elements,
        'total_pages': math.ceil(float(total_elements) / size),
        'current_page': page,
        'content': []
    }

    for document in cursor:
        result['content'].append(document)
        result['content'][-1]['_id'] = str(result['content'][-1]['_id'])
        result['content'][-1]['date_added'] = utils.utc_ts_to_local_str(
            document.get('date_added', None))
        result['content'][-1]['date_modified'] = utils.utc_ts_to_local_str(
            document.get('date_modified', None))
        result['content'][-1]['date_subscribed'] = utils.utc_ts_to_local_str(
            document.get('date_subscribed', None))
        balance = float(result['content'][-1]['deposit_balance'])
        result['content'][-1]['pnl'] = '{:.2f}%'.format(
            (float(result['content'][-1]['profit']) + balance - balance) /
            balance * 100)

    return result
コード例 #10
0
def get_notes(user_id):
    notes = list(ApiAccess.get_revenyou_db().notes.\
        find({'user_id': user_id}, {'_id': False}).\
        sort('date_added', -1).\
        limit(10))

    for note in notes:
        note.update((k, utils.utc_ts_to_local_str(v)) for k, v in note.items()
                    if k == 'date_added')

    return {'notes': notes}
コード例 #11
0
def get_customer_internal(user_id, is_corporate):
    db = ApiAccess.get_revenyou_db().corporate_account if is_corporate \
        else ApiAccess.get_revenyou_db().sign_up
    cursor = list(db.find({'user_id': user_id}))
    if len(cursor) == 0:
        return {}

    user = cursor[0]
    carrier_id = user.get('carrier_id', None)
    country = user.get('country', None)
    carrier = ApiAccess.get_revenyou_db().carrier.find_one(
        {'_id': ObjectId(carrier_id)}) if carrier_id is not None else {}
    country = pycountry.countries.get(
        alpha_3=country.upper()) if country is not None else None
    user['mobile'] = carrier.get('non_formated_phone_number',
                                 '') if carrier else ''
    user['_id'] = str(user['_id'])
    user['country'] = country.name if country is not None else 'Unknown'
    user['date_added'] = utils.utc_ts_to_local_str(user.get(
        'date_added', None))
    user['date_modified'] = utils.utc_ts_to_local_str(
        user.get('date_modified', None))
    user['full_name'] = '{} {}'.format(user.get('first_name', ''),
                                       user.get('last_name', ''))

    try:
        if 'document_front_url' in user:
            user['document_front_url'] = cloudinary.CloudinaryImage(
                user['document_front_url']).build_url()
        if 'document_back_url' in user:
            user['document_back_url'] = cloudinary.CloudinaryImage(
                user['document_back_url']).build_url()
    except Exception:
        pass

    return user
コード例 #12
0
def get_csv_exportable(max, cols, begin, end):
    query = {}
    if begin and end:
        query['date_added'] = {'$gte': begin, '$lte': end}
    elif begin:
        query['date_added'] = {'$gte': begin}
    elif end:
        query['date_added'] = {'$lte': end}

    cursor = list(ApiAccess.get_revenyou_db().sign_up.\
                find(query, {'_id': 0}).\
                limit(max).\
                collation({'locale': 'en'}))

    if 'phone_number' in cols:
        carriers = ApiAccess.get_revenyou_db().carrier.\
        find({'_id': \
            {'$in' : [ObjectId(doc.get('carrier_id', '')) for doc in cursor]}},
            {'non_formated_phone_number': 1}).\
        limit(max)

        phone_numbers = {}
        for item in carriers:
            phone_numbers[str(item['_id'])] = item.get(
                'non_formated_phone_number', '')

    result = []
    for document in cursor:
        result.append({})
        for col in cols:
            if col == 'phone_number':
                result[-1]['phone_number'] = phone_numbers.get(
                    document.get('carrier_id', ''), '')
                continue
            if col == 'full_name':
                result[-1]['full_name'] = '{} {}'.format(
                    document.get('first_name', ''),
                    document.get('last_name', ''))
                continue
            if col == 'date_added':
                result[-1]['date_added'] = utils.utc_ts_to_local_str(
                    document.get('date_added', None))
                continue

            result[-1][col] = document.get(col, '')

    return json.dumps(result)
コード例 #13
0
def get_corporates_internal(size, page, sort, order, begin, end, name):
    pipeline = [{
        '$addFields': {
            'full_name': {
                '$concat':
                ['$first_name', ' ', '$middle_name', ' ', '$last_name']
            }
        }
    }]

    if begin and end:
        pipeline.append(
            {'$match': {
                'date_added': {
                    '$gte': begin,
                    '$lte': end
                }
            }})
    elif begin:
        pipeline.append({'$match': {'date_added': {'$gte': begin}}})
    elif end:
        pipeline.append({'$match': {'date_added': {'$lte': end}}})

    if name:
        initials = name.split()
        full_name_regex = '.*{}.*'.format(initials[0])
        for initial in initials[1:]:
            full_name_regex += ' .*{}.*'.format(initial)

        pipeline.append({
            '$match': {
                '$or': [{
                    'full_name': {
                        '$regex': full_name_regex,
                        '$options': 'i'
                    }
                }, {
                    'email': {
                        '$regex': '.*{}.*'.format(name),
                        '$options': 'i'
                    }
                }]
            }
        })

    count_pipline = pipeline.copy()
    count_pipline.append({'$count': 'count'})

    maybe_count = list(
        ApiAccess.get_revenyou_db().corporate_account.aggregate(count_pipline))
    total_elements = maybe_count[0]['count'] if maybe_count else 0

    pipeline.extend([{
        '$addFields': {
            'full_name_cap': {
                '$toLower': '$full_name'
            }
        }
    }, {
        '$sort': {
            sort: (-1 if order == 'desc' else 1)
        }
    }, {
        '$limit': size + (page * size)
    }, {
        '$skip': (page * size)
    }])

    cursor = list(
        ApiAccess.get_revenyou_db().corporate_account.aggregate(pipeline))

    result = {
        'total_elements': total_elements,
        'total_pages': math.ceil(float(total_elements) / size),
        'current_page': page,
        'content': []
    }

    for document in cursor:
        result['content'].append(document)
        result['content'][-1]['_id'] = str(result['content'][-1]['_id'])
        result['content'][-1]['date_added'] = utils.utc_ts_to_local_str(
            document.get('date_added', None))
        result['content'][-1]['date_modified'] = utils.utc_ts_to_local_str(
            document.get('date_modified', None))

    return result
コード例 #14
0
def get_customers_by_id(ids, size, page, sort, order, name=None):
    if ids is None or len(ids.get('content', [])) == 0:
        return {
            'total_elements': 0,
            'total_pages': 0,
            'current_page': 0,
            'content': []
        }

    ids_raw = [doc['_id'] for doc in ids.get('content', []) if '_id' in doc]
    pipeline = [{
        '$addFields': {
            'full_name': {
                '$concat': ['$first_name', ' ', '$last_name']
            }
        }
    }, {
        '$match': {
            'user_id': {
                '$in': ids_raw
            }
        }
    }]

    if name:
        initials = name.split()
        full_name_regex = '.*{}.*'.format(initials[0])
        for initial in initials[1:]:
            full_name_regex += ' .*{}.*'.format(initial)

        pipeline.append({
            '$match': {
                '$or': [{
                    'full_name': {
                        '$regex': full_name_regex,
                        '$options': 'i'
                    }
                }, {
                    'email': {
                        '$regex': '.*{}.*'.format(name),
                        '$options': 'i'
                    }
                }, {
                    'dob': {
                        '$regex': '.*{}.*'.format(name),
                        '$options': 'i'
                    }
                }]
            }
        })

    count_pipline = pipeline.copy()
    count_pipline.append({'$count': 'count'})

    maybe_count = list(
        ApiAccess.get_revenyou_db().sign_up.aggregate(count_pipline))
    total_elements = maybe_count[0]['count'] if maybe_count else 0

    pipeline.append(
        {'$addFields': {
            'full_name_cap': {
                '$toLower': '$full_name'
            }
        }})

    if sort == 'external':
        pipeline.extend([{
            '$addFields': {
                '__order': {
                    '$indexOfArray': [ids_raw, '$user_id']
                }
            }
        }, {
            '$sort': {
                '__order': (-1 if order == 'desc' else 1)
            }
        }])
    else:
        pipeline.append({'$sort': {sort: (-1 if order == 'desc' else 1)}})

    pipeline.extend([{
        '$limit': size + (page * size)
    }, {
        '$skip': (page * size)
    }])

    cursor = list(ApiAccess.get_revenyou_db().sign_up.aggregate(pipeline))

    result = {
        'total_elements': total_elements,
        'total_pages': math.ceil(float(total_elements) / size),
        'current_page': page,
        'content': []
    }

    carriers = ApiAccess.get_revenyou_db().carrier.\
        find({'_id': {'$in' : [ObjectId(doc.get('carrier_id', '')) for doc in cursor]}}, {'non_formated_phone_number': 1})
    phone_numbers = {}
    for item in carriers:
        phone_numbers[str(item['_id'])] = item.get('non_formated_phone_number',
                                                   '')

    for document in cursor:
        result['content'].append(document)
        result['content'][-1]['_id'] = str(result['content'][-1]['_id'])
        result['content'][-1]['date_added'] = utils.utc_ts_to_local_str(
            document.get('date_added', None))
        result['content'][-1]['date_modified'] = utils.utc_ts_to_local_str(
            document.get('date_modified', None))
        result['content'][-1]['mobile'] = phone_numbers.get(
            document.get('carrier_id', ''), '')

    return result
コード例 #15
0
def get_customers_internal_from_phone(size, begin, end, name):
    query = {
        'non_formated_phone_number': {
            '$regex': '.*{}.*'.format(name),
            '$options': 'i'
        }
    }

    if begin and end:
        query['date_added'] = {'$gte': begin, '$lte': end}
    elif begin:
        query['date_added'] = {'$gte': begin}
    elif end:
        query['date_added'] = {'$lte': end}

    total_elements = ApiAccess.get_revenyou_db().carrier.find(query).count()
    carriers = list(ApiAccess.get_revenyou_db().carrier.\
        find(query, {'non_formated_phone_number': 1}).\
        sort('_id', -1).\
        limit(size))

    phone_numbers = {}
    for item in carriers:
        phone_numbers[str(item['_id'])] = item.get('non_formated_phone_number',
                                                   '')

    pipeline = [{
        '$match': {
            'carrier_id': {
                '$in': list(phone_numbers.keys())
            }
        }
    }, {
        '$addFields': {
            'full_name': {
                '$concat': ['$first_name', ' ', '$last_name']
            }
        }
    }, {
        '$addFields': {
            'full_name_cap': {
                '$toLower': '$full_name'
            }
        }
    }, {
        '$sort': {
            '_id': -1
        }
    }, {
        '$limit': size
    }]

    cursor = list(ApiAccess.get_revenyou_db().sign_up.aggregate(pipeline))

    result = {
        'total_elements': total_elements,
        'total_pages': 1,
        'current_page': 0,
        'content': []
    }

    for document in cursor:
        result['content'].append(document)
        result['content'][-1]['_id'] = str(result['content'][-1]['_id'])
        result['content'][-1]['date_added'] = utils.utc_ts_to_local_str(
            document.get('date_added', None))
        result['content'][-1]['date_modified'] = utils.utc_ts_to_local_str(
            document.get('date_modified', None))
        result['content'][-1]['mobile'] = phone_numbers.get(
            document.get('carrier_id', ''), '')

    return result
コード例 #16
0
def get_customers_internal(size, page, sort, order, begin, end, name,
                           advanced):
    pipeline = [{
        '$addFields': {
            'full_name': {
                '$concat': ['$first_name', ' ', '$last_name']
            }
        }
    }]

    if len(advanced) != 0:
        pipeline.extend(get_advance_filters(json.loads(advanced)))

    if begin and end:
        pipeline.append(
            {'$match': {
                'date_added': {
                    '$gte': begin,
                    '$lte': end
                }
            }})
    elif begin:
        pipeline.append({'$match': {'date_added': {'$gte': begin}}})
    elif end:
        pipeline.append({'$match': {'date_added': {'$lte': end}}})

    if name:
        initials = name.split()
        full_name_regex = '.*{}.*'.format(initials[0])
        for initial in initials[1:]:
            full_name_regex += ' .*{}.*'.format(initial)

        pipeline.append({
            '$match': {
                '$or': [{
                    'full_name': {
                        '$regex': full_name_regex,
                        '$options': 'i'
                    }
                }, {
                    'email': {
                        '$regex': '.*{}.*'.format(name),
                        '$options': 'i'
                    }
                }, {
                    'dob': {
                        '$regex': '.*{}.*'.format(name),
                        '$options': 'i'
                    }
                }]
            }
        })

    count_pipline = pipeline.copy()
    count_pipline.append({'$count': 'count'})

    maybe_count = list(
        ApiAccess.get_revenyou_db().sign_up.aggregate(count_pipline))
    total_elements = maybe_count[0]['count'] if maybe_count else 0

    pipeline.extend([{
        '$addFields': {
            'full_name_cap': {
                '$toLower': '$full_name'
            }
        }
    }, {
        '$sort': {
            sort: (-1 if order == 'desc' else 1)
        }
    }, {
        '$limit': size + (page * size)
    }, {
        '$skip': (page * size)
    }])

    cursor = list(ApiAccess.get_revenyou_db().sign_up.aggregate(pipeline))

    result = {
        'total_elements': total_elements,
        'total_pages': math.ceil(float(total_elements) / size),
        'current_page': page,
        'content': []
    }

    carriers = ApiAccess.get_revenyou_db().carrier.\
        find({'_id': \
            {'$in' : [ObjectId(doc.get('carrier_id', '')) for doc in cursor]}},
            {'non_formated_phone_number': 1}).\
        limit(size)

    phone_numbers = {}
    for item in carriers:
        phone_numbers[str(item['_id'])] = item.get('non_formated_phone_number',
                                                   '')

    for document in cursor:
        result['content'].append(document)
        result['content'][-1]['_id'] = str(result['content'][-1]['_id'])
        result['content'][-1]['date_added'] = utils.utc_ts_to_local_str(
            document.get('date_added', None))
        result['content'][-1]['date_modified'] = utils.utc_ts_to_local_str(
            document.get('date_modified', None))
        result['content'][-1]['mobile'] = phone_numbers.get(
            document.get('carrier_id', ''), '')

    return result