def update_metadata(option: dict, dynamodb_resource: ServiceResource) -> dict:
    """
    metadataを更新する
    """
    table = dynamodb_resource.Table(get_table_name())
    resp = table.update_item(**option)
    return resp['Attributes']
def put_make_history_item(item: dict, table_name: str,
                          dynamodb_resource: ServiceResource):
    table = dynamodb_resource.Table(table_name)
    option = {"Item": item}

    resp = table.put_item(**option)
    logger.info("put make history item", item=item, resp=resp)
def upsert_user_make_history(user_id: str, user_name: str, amount: int,
                             table_name: str,
                             dynamodb_resource: ServiceResource) -> int:
    jst = timezone(offset=timedelta(hours=+9), name="jst")
    now = datetime.now(jst).strftime("%Y年%m月")

    table = dynamodb_resource.Table(table_name)
    key_user_times = f"{user_id}_times"
    option = {
        "Key": {
            "partitionId": "userMakeHistory",
            "sortId": now
        },
        "UpdateExpression":
        "add #user_times :user_times set #user_name = :user_name, #updatedAt = :updatedAt",
        "ExpressionAttributeNames": {
            "#user_times": key_user_times,
            "#user_name": f"{user_id}_name",
            "#updatedAt": "updatedAt",
        },
        "ExpressionAttributeValues": {
            ":user_times": amount,
            ":user_name": user_name,
            ":updatedAt": int(datetime.now(timezone.utc).timestamp() * 1000),
        },
        "ReturnValues": "ALL_NEW",
    }
    resp = table.update_item(**option)
    return int(resp["Attributes"][key_user_times])
Пример #4
0
def put_metadata_item(metadata: dict, dynamodb_resource: ServiceResource):
    """
    metadataをDynamoDBに保存する
    """
    table_name = _get_table_name()
    table = dynamodb_resource.Table(table_name)
    table.put_item(Item=metadata)
Пример #5
0
def fetch_a_metadata(id: str,
                     dynamodb_resource: ServiceResource) -> Optional[dict]:
    """
    DynamoDBからmetadataを単件取得する。該当するmetadataがなければnullを返す(not found)。
    """
    table = table = dynamodb_resource.Table(get_table_name())
    resp = table.get_item(Key={'id': id})
    return resp.get('Item')
def get_a_metadata(id: str,
                   dynamodb_resource: ServiceResource) -> Optional[dict]:
    """
    IDを使って、metadataを取得する
    """
    table = dynamodb_resource.Table(get_table_name())
    resp = table.get_item(Key={'id': id})
    return resp.get('Item')
def scan_table(table_name: str,
               dynamodb_resource: ServiceResource) -> List[dict]:
    table = dynamodb_resource.Table(table_name)
    resp = table.scan()
    table_items = resp['Items']
    while 'LastEvaluatedKey' in resp:
        resp = table.scan(ExclusiveStartKey=resp['LastEvaluatedKey'])
        table_items.extend(resp['Items'])
    return table_items
Пример #8
0
def delete_diff_items(table_name: str, partition_key: str, sort_key: str,
                      diff_keys: List[str],
                      dynamodb_resource: ServiceResource) -> None:
    table = dynamodb_resource.Table(table_name)
    for diff_key in diff_keys:
        key = {partition_key: diff_key.split(KEY_SPLITTER)[0]}
        if sort_key != '':
            sort_key_d = {sort_key: diff_key.split(KEY_SPLITTER)[1]}
            key.update(**sort_key_d)
        table.delete_item(Key=key)
Пример #9
0
def update_sack_counter(amount: int, table_name: str, dynamodb_resource: ServiceResource) -> int:
    table = dynamodb_resource.Table(table_name)
    option = {
        "Key": {"partitionId": "sackCounter", "sortId": "counter"},
        "UpdateExpression": "add #times :amount set #updatedAt = :updatedAt",
        "ExpressionAttributeNames": {"#times": "times", "#updatedAt": "updatedAt"},
        "ExpressionAttributeValues": {
            ":amount": amount,
            ":updatedAt": int(datetime.now(timezone.utc).timestamp() * 1000),
        },
        "ReturnValues": "ALL_NEW",
    }
    resp = table.update_item(**option)
    return resp["Attributes"]["times"]
Пример #10
0
def get_open_sack_history(table_name: str,
                          dynamodb_resource: ServiceResource) -> List[dict]:
    table = dynamodb_resource.Table(table_name)
    option = {
        "KeyConditionExpression": Key("partitionId").eq("sackHistory"),
        "Limit": 5,
        "ScanIndexForward": False,
        "ProjectionExpression": "#sortId, #times",
        "ExpressionAttributeNames": {
            "#sortId": "sortId",
            "#times": "times"
        },
    }
    resp = table.query(**option)
    logger.info("get open sack history result", option=option, response=resp)
    return resp.get("Items", [])
Пример #11
0
def scan_metadata(dynamodb_resource: ServiceResource,
                  last_evaluated_key: Optional[dict] = None) -> List[dict]:
    """
    DynamoDBからmetadataを全件取得する。
    scanではデータ量が多いと一度で取得できない場合があるので、再帰的に処理を行い全件取得するようにしている。
    """
    table = dynamodb_resource.Table(get_table_name())
    option = {}
    if last_evaluated_key is not None:
        option['ExclusiveStartKey'] = last_evaluated_key
    resp = table.scan(**option)
    result = resp.get('Items', [])
    if 'LastEvaluatedKey' in resp:
        result += scan_metadata(dynamodb_resource,
                                last_evaluated_key=resp['LastEvaluatedKey'])
    return result
Пример #12
0
def insert_sack_history(times: int, table_name: str,
                        dynamodb_resouce: ServiceResource) -> None:
    table = dynamodb_resouce.Table(table_name)

    jst = timezone(offset=timedelta(hours=+9), name="jst")
    now = str(datetime.now(jst))

    option = {
        "Item": {
            "partitionId": "sackHistory",
            "sortId": now,
            "times": times
        }
    }

    resp = table.put_item(**option)
    logger.info("insert sack history result", option=option, resp=resp)
Пример #13
0
def reset_sack_counter(table_name: str,
                       dynamodb_resource: ServiceResource) -> None:
    table = dynamodb_resource.Table(table_name)
    option = {
        "Key": {
            "partitionId": "sackCounter",
            "sortId": "counter"
        },
        "UpdateExpression": "set #times = :times, #updatedAt = :updatedAt",
        "ExpressionAttributeNames": {
            "#times": "times",
            "#updatedAt": "updatedAt"
        },
        "ExpressionAttributeValues": {
            ":times": 0,
            ":updatedAt": int(datetime.now(timezone.utc).timestamp() * 1000)
        },
    }
    resp = table.update_item(**option)
    logger.info("reset sack counter result", resp=resp)
Пример #14
0
def update_make_history(user_id: str, user_name: str, ts: str, table_name: str,
                        dynamodb_resource: ServiceResource) -> Tuple[int, int]:
    table = dynamodb_resource.Table(table_name)
    partition_id = "makeHistory"
    option = {
        "Key": {
            "partitionId": partition_id,
            "sortId": ts
        },
        "ConditionExpression":
        Key("partitionId").eq(partition_id)
        & Key("sortId").eq(ts)
        & Attr("isDetected").eq(False),
        "UpdateExpression":
        "set #updatedAt = :updatedAt, #userName = :userName, #isDetected = :isDetected",
        "ExpressionAttributeNames": {
            "#updatedAt": "updatedAt",
            "#userName": f"{user_id}_name",
            "#isDetected": "isDetected",
        },
        "ExpressionAttributeValues": {
            ":updatedAt": int(datetime.now(timezone.utc).timestamp() * 1000),
            ":userName": user_name,
            ":isDetected": True,
        },
        "ReturnValues":
        "ALL_NEW",
    }

    try:
        resp = table.update_item(**option)
        logger.info("update make history result", option=option, response=resp)

        times = resp["Attributes"]["times"]
        amount = resp["Attributes"]["amount"]

        return int(times), int(amount)
    except ClientError as e:
        if e.response["Error"]["Code"] == "ConditionalCheckFailedException":
            raise NotTargetError()
        raise
Пример #15
0
def get_user_make_history(year: int, month: int, table_name: str, dynamodb_resource: ServiceResource) -> dict:
    table = dynamodb_resource.Table(table_name)
    option = {"Key": {"partitionId": "userMakeHistory", "sortId": f"{year}年{month:02}月"}}
    resp = table.get_item(**option)
    logger.info("get user make history result", option=option, response=resp)
    return resp.get("Item")
Пример #16
0
def put_to_table(table_name: str, src_items: List[dict],
                 dynamodb_resource: ServiceResource) -> None:
    table = dynamodb_resource.Table(table_name)
    with table.batch_writer() as batch:
        for item in src_items:
            batch.put_item(Item=item)
Пример #17
0
 def __init__(self, table_name: str, dynamodb_resource: ServiceResource):
     self.table_name = table_name
     self.dynamodb_resource = dynamodb_resource
     self.create()
     self.table = dynamodb_resource.Table(table_name)