def get_count_of_words_learnt_today(user_uuid, dynamodb_client):
    """
    :param user_uuid: "fca9cff4-f6c3-4518-aab7-7c926539fe27"
    :param dynamodb_client: 
    :return: array of dicts with word and rank
    """
    today = datetime.date.today().isoformat()

    response = dynamo_helper.query(
        table_name='user_progress',
        index_name='learn_at_index',
        select='COUNT',
        expression_attribute_values={
            ':user_uuid': {
                'S': user_uuid
            },
            ':today': {
                'S': today
            }
        },
        key_condition_expression=('user_uuid = :user_uuid '
                                  'AND learnt_at >= :today'),
        consistent_read=True,
        dynamodb_client=dynamodb_client)

    return response['Count']
def get_words_learnt_today(user_uuid, dynamodb_client):
    """
    :param user_uuid: "fca9cff4-f6c3-4518-aab7-7c926539fe27"
    :param dynamodb_client: 
    :return: array of dicts with word and rank
    """
    today = datetime.date.today().isoformat()

    response = dynamo_helper.query(
        table_name='user_progress',
        index_name='learn_at_index',
        expression_attribute_values={
            ':user_uuid': {
                'S': user_uuid
            },
            ':today': {
                'S': today
            }
        },
        key_condition_expression=('user_uuid = :user_uuid '
                                  'AND learnt_at >= :today'),
        dynamodb_client=dynamodb_client)

    return [{
        'word': item['word']['S'],
        'rank': int(item['rank']['N'])
    } for item in response['Items']]
def get_next_words(list_version, latest_word_rank, num_words_left,
                   dynamodb_client):
    if num_words_left < 1:
        return []
    starting_rank = latest_word_rank + 1 if latest_word_rank else 1
    ending_rank = starting_rank + num_words_left - 1

    response = dynamo_helper.query(
        table_name='words_rank',
        expression_attribute_values={
            ':version': {
                'S': list_version
            },
            ':start_rank': {
                'N': str(starting_rank)
            },
            ':end_rank': {
                'N': str(ending_rank)
            }
        },
        key_condition_expression=('version = :version '
                                  'AND #R BETWEEN :start_rank AND :end_rank'),
        expression_attribute_names={'#R': 'rank'},
        dynamodb_client=dynamodb_client)

    return parse_dynamo_response(response)
Esempio n. 4
0
    def test_query__index_name_and_filter_expression_are_None__query_called_without_those_params(
            self):
        dynamodb_client_mock = mock.MagicMock()
        dynamodb_client_mock.query.return_value = {
            'ResponseMetadata': {
                'HTTPStatusCode': 200
            }
        }

        dynamo_helper.query(table_name='T',
                            index_name=None,
                            expression_attribute_values='eav',
                            key_condition_expression='kce',
                            filter_expression=None,
                            dynamodb_client=dynamodb_client_mock)

        dynamodb_client_mock.query.assert_called_once_with(
            TableName='T',
            ExpressionAttributeValues='eav',
            KeyConditionExpression='kce')
def get_utc_date_for_next_review(user_uuid, dynamodb_client):
    response = dynamo_helper.query(
        table_name='user_progress',
        index_name='next_review_index',
        expression_attribute_values={':user_uuid': {
            'S': user_uuid
        }},
        key_condition_expression=('user_uuid = :user_uuid'),
        limit=1,
        dynamodb_client=dynamodb_client)
    assert response['Count'] == 1
    return dynamo_helper.get_str_attr(item=response['Items'][0],
                                      attr='next_review_at')
def do_query_for_review_items(now,
                              user_uuid,
                              dynamodb_client,
                              exclusive_start_key=None):
    response = dynamo_helper.query(
        table_name='user_progress',
        index_name='next_review_index',
        expression_attribute_values={
            ':user_uuid': {
                'S': user_uuid
            },
            ':now': {
                'S': now
            }
        },
        key_condition_expression=('user_uuid = :user_uuid '
                                  'AND next_review_at <= :now'),
        dynamodb_client=dynamodb_client)
    return response
def get_latest_word_rank_word_version_and_num_remaining_today(
        user_uuid, dynamodb_client):
    response = dynamo_helper.query(
        table_name='user_progress',
        index_name='learn_at_index',
        expression_attribute_values={':user_uuid': {
            'S': user_uuid
        }},
        key_condition_expression=('user_uuid = :user_uuid'),
        consistent_read=True,
        limit=config.MAX_WORDS_PER_DAY,
        scan_index_forward=False,
        dynamodb_client=dynamodb_client)

    if response['Count'] == 0:
        return (None, config.LATEST_WORD_LIST_VERSION, config.MAX_WORDS_PER_DAY
                )  # if no words learnt yet

    num_remaining = get_num_remaining(response)

    return (int(response['Items'][0]['rank']['N']),
            response['Items'][0]['version']['S'], num_remaining)