def step_impl(context): context.event = { 'Records': [{ 'messageAttributes': { 'consultant': { 'stringValue': 'BehaveTest1', 'dataType': 'String' }, 'type': { 'stringValue': 'scheduled', 'dataType': 'String.type' }, 'checkin-date': { 'stringValue': '2020-01-01', 'dataType': 'String' } } }] } prediction_arn = get_export('predict-topic-arn') os.environ['TIMES_UP_MACHINE'] = 'times_up_machine' context.response = checkin_consumer( context.event, None, context.sns_client, context.sfn_client, context.checkin_model, context.consultant_model, context.request_model, context.consultant_model, context.contract_model) assert context.sns_client.publish.call_args.kwargs[ 'TopicArn'] == prediction_arn
class Meta: '''Online Statuses Meta Class''' if 'CustomersTableName' in os.environ: table_name = os.environ['CustomersTableName'] else: table_name = get_export('database-CustomersTableName') region = 'eu-central-1' host = 'https://dynamodb.eu-central-1.amazonaws.com'
class Meta: '''Consultants Meta Class''' if 'ConsultantsTableName' in os.environ: table_name = os.environ['ConsultantsTableName'] else: table_name = get_export('database-ConsultantsTableName') region = 'eu-central-1' host = 'https://dynamodb.eu-central-1.amazonaws.com'
def send_to_sqs(self, action_id: str, action_type: str, value: str,\ message_type: Types, consultant_uuid = None): ''' Sends message to SQS - :param action_id: action id of message :param action_type: type of message :param value: value of message ''' if action_type == 'checkboxes': value = [{'value': x['value'].split(';')[1]} for x in value] if action_type == 'static_select': value = value['value'].split(';')[1] message_payload = { "action_id": action_id, "type": action_type, "value": value } if self.metadata is not None: if 'current_activity' in self.metadata and\ self.metadata['current_activity'] is not None: message_payload['customer'] = self.metadata['current_activity'] elif 'current_customer' in self.metadata and\ self.metadata['current_customer'] is not None: message_payload['customer'] = self.metadata['current_customer'] checkin = self.checkin_model.get(self.metadata['checkin_id']) message = write_message(checkin.consultant_uuid, message_type, checkin_id=checkin.uuid, device_id=self.payload['api_app_id'], trigger_id=self.payload['trigger_id'], payload=message_payload) else: message = write_message(consultant_uuid, message_type, checkin_id=None, device_id=self.payload['api_app_id'], trigger_id=self.payload['trigger_id'], payload=message_payload) slack_app_url = get_export('checkin-action-queue-url') send = self.sqs_client.send_message(QueueUrl=slack_app_url, MessageBody='Slack User Input', MessageAttributes=message, MessageGroupId='UserInput', MessageDeduplicationId=str( uuid.uuid4())) print("SEND TO SQS:", send) return send
def publisher(event: Dict, context, sqs_client: Boto3SQS) -> None: ''' Publishes to Slack App Queue - :param event: AWS event :param context: AWS context :param sqs_client: Boto3 SQS client ''' print("Request ID: ", context.aws_request_id) print("Event: ", event) slack_app_queue_url = get_export('slack-app-queue-url') send_message = sqs_client.send_message(QueueUrl=slack_app_queue_url, MessageBody='Prediction done', MessageAttributes=event) print(send_message)
def scheduled(message: Dict, unique_id: str, sns_client: Boto3SNS, stepfunctions_client: Boto3SFN, checkin_model: PynamoDBCheckIn)\ -> Tuple[Boto3SNS, Boto3SFN]: ''' Method for scheduled action. Saves "null" checkin in database and publish message to prediction queue. - :param message: The message from start "event" e.g. message will be received at 9am. :param unique_id: Unique id for checkin in database. :param sns_client: boto3 client for Simple Notification Server. :param stepfunctions_client: boto3 client for stepfunctions. :param checkin_model: PynamoDB CheckIn Model ''' prediction_arn = get_export('predict-topic-arn') times_up_machine = os.environ['TIMES_UP_MACHINE'] reminder = 'checkin-id' in message if not reminder: checkin_date = datetime.strptime(message['checkin-date'], '%Y-%m-%d').date() checkin = checkin_model(uuid=unique_id, consultant_uuid=message['consultant'], date=str(checkin_date), completed='False', predictions='[]', user_input='[]') # Write the checkin to the database checkin.save() # Send message to predict sns_pub = sns_client.publish( TopicArn=prediction_arn, Message='Ready for prediction', MessageAttributes=write_message( message['consultant'], Types.Predict, checkin_id=unique_id) ) print(sns_pub) # Start TimesUp start = stepfunctions_client.start_execution( stateMachineArn=times_up_machine, input=json.dumps(write_message( message['consultant'], Types.Slack if not reminder else Types.Slack_Reminder,\ checkin_id=unique_id if not reminder else message['checkin-id'])) ) print(start)
import os import boto3 import time from src.modules.get_export import get_export from src.modules.queue_message_writer import write_message from src.modules.type_enum import Types from src.predictors.predict_customers import make_prediction from src.dependencies.boto3_sqs_provider import get_sqs_provider from src.dependencies.pynamodb_online_statuses_provider import get_online_statuses_provider from src.dependencies.pynamodb_customers_provider import get_customers_provider from src.dependencies.pynamodb_checkin_provider import get_checkin_provider PREDICT_TOPIC_ARN = get_export('predict-topic-arn') ENV_TYPE = get_export('pipeline-enviroment-type') @given(u'a lambda function') def step_impl(context): # Pass as the import to the lambda didn't fail context.sqs_client = get_sqs_provider(test=True) context.online_statuses_model = get_online_statuses_provider(test=True) context.customer_model = get_customers_provider(test=True) context.checkin_model = get_checkin_provider(test=True) pass @when(u'a message is consumed from a predict topic') def step_impl(context): context.event = { 'Records': [{ 'Sns': {