def handler(event: dict, context: dict) -> dict:
    verify_deploy_stage()
    event_batches = [
        batch_from_dict(dynamodb_utils.deserialize(record["dynamodb"]["NewImage"]))
        for record in event["Records"]
        if record["dynamodb"].get("NewImage")
    ]

    _publish_event_batches_to_kinesis(event_batches)

    return {"statusCode": 200}
예제 #2
0
def handler(event, context):
    verify_deploy_stage()
    event_batches = [
        batch_from_dict(dynamodb_utils.deserialize(record["dynamodb"]["NewImage"]))
        for record in event["Records"]
        if record["dynamodb"].get("NewImage")
    ]

    status.handle_dialog_event_batches(event_batches)

    return {"statusCode": 200}
예제 #3
0
 def fetch_dialog_state(self, phone_number: str) -> DialogState:
     response = self.dynamodb.get_item(
         TableName=self.state_table_name(),
         Key={"phone_number": {
             "S": phone_number
         }},
         ConsistentRead=True,
     )
     if "Item" not in response:
         return DialogState(phone_number=phone_number, seq="0")
     dialog_dict = dynamodb_utils.deserialize(response["Item"])
     return DialogState(**dialog_dict)
예제 #4
0
    def fetch_dialog_event_batch(self, phone_number: str,
                                 batch_id: uuid.UUID) -> DialogEventBatch:
        response = self.dynamodb.get_item(
            TableName=self.event_batch_table_name(),
            Key={
                "phone_number": {
                    "S": phone_number
                },
                "batch_id": {
                    "S": str(batch_id)
                }
            },
            ConsistentRead=True,
        )
        dialog_dict = dynamodb_utils.deserialize(response["Item"])

        return batch_from_dict(dialog_dict)
예제 #5
0
def _get_dialog_events(phone_number: str, stage: str) -> Iterator[DialogEventBatch]:
    dynamodb = get_boto3_client("dynamodb")
    table_name = f"dialog-event-batches-{stage}"
    args: Dict[str, str] = {}
    while True:
        result = dynamodb.query(
            TableName=table_name,
            IndexName="by_created_time",
            KeyConditionExpression="phone_number=:phone_number",
            ExpressionAttributeValues={":phone_number": {"S": phone_number}},
            **args,
        )
        for item in result["Items"]:
            yield batch_from_dict(dynamodb_utils.deserialize(item))
        if not result.get("LastEvaluatedKey"):
            break
        args["ExclusiveStartKey"] = result["LastEvaluatedKey"]
예제 #6
0
def handler(event, context):
    verify_deploy_stage()
    event_batches = [
        batch_from_dict(dynamodb_utils.deserialize(record["dynamodb"]["NewImage"]))
        for record in event["Records"]
        if record["dynamodb"].get("NewImage")
    ]

    dialog_events = []
    for batch in event_batches:
        for event in batch.events:
            dialog_events.append(event)

    enqueue_outbound_sms_commands(dialog_events)
    for batch in event_batches:
        logging.info(f"Enqueue SMS commands for {batch.phone_number} at seq {batch.seq}")

    return {"statusCode": 200}
예제 #7
0
def handler(event, context):
    verify_deploy_stage()
    drill_progresses_to_schedule = {}
    for record in event["Records"]:
        if record["eventName"] == "REMOVE":
            item = dynamodb_utils.deserialize(record["dynamodb"]["OldImage"])
            drill_progresses_to_schedule[
                item["idempotency_key"]] = DrillProgressSchema().load(
                    item["drill_progress"])
    initiator = DrillInitiator()

    for idempotency_key, drill_progress in drill_progresses_to_schedule.items(
    ):
        slug = drill_progress.next_drill_slug_to_trigger()
        if slug is None:
            logging.warning(f"Got a request to trigger drill_slug=None "
                            f"for {drill_progress.phone_number}. Ignoring.")
            continue
        initiator.trigger_drill_if_not_stale(drill_progress.phone_number, slug,
                                             idempotency_key)

    return {"statusCode": 200}
예제 #8
0
 def get_scheduled_drill(
         self, drill_progress: DrillProgress) -> Optional[ScheduledDrill]:
     response = self.dynamodb.get_item(
         TableName=self._table_name(),
         Key={
             "phone_number": {
                 "S": drill_progress.phone_number
             },
             "idempotency_key": {
                 "S": self._idempotency_key(drill_progress)
             },
         },
         ConsistentRead=True,
     )
     if "Item" not in response:
         return None
     dialog_dict = dynamodb_utils.deserialize(response["Item"])
     return ScheduledDrill(
         idempotency_key=dialog_dict["idempotency_key"],
         trigger_ts=int(dialog_dict["trigger_ts"]),
         drill_progress=DrillProgressSchema().load(
             dialog_dict["drill_progress"]),
     )