def check(event, context):
    """This micro function is used by API Gateway and run the check processing of the DNA.
    It first hash the DNA to search for already processed results, if already processed, it
    returns the boolean result, else the processing will occur and a message will be sent
    to the SQS queue in charge and saving the item to database

    Args:
        event (mixed): The input event of the micro function.
        context (mixed): The input context of the micro function.

    Returns:
        mixed: The micro function response
    """

    body = json.loads(event['body'])

    # calculating DNA hash
    dna_hash = hashlib.md5(json.dumps(body['dna']).encode("utf-8")).hexdigest()

    # looking for existing result for this DNA
    item = DYNAMODB_CLIENT.Table(os.getenv('DNA_ANALYSE_TABLE_NAME'))\
        .get_item(Key={
            'hash': dna_hash
        }, ProjectionExpression='mutant'
    )

    # If already analyzed, get the result
    if hasattr(item, 'Item'):

        result = item['Item']['mutant']

    # Else we calculate and set to DB via SQS Queue
    else:

        result = checker.is_mutant(body['dna'])

        SQS_CLIENT.send_message(QueueUrl=os.getenv('DNA_ANALYSE_QUEUE_URL'),
                                MessageBody=json.dumps({
                                    'hash': dna_hash,
                                    'dna': body['dna'],
                                    'mutant': result
                                }))

    # Responding
    response = {"statusCode": 200, "body": json.dumps(result)}

    return response
예제 #2
0
 def test_human(self):
     self.assertEqual(
         checker.is_mutant(
             ["ATGCGA", "CAGTGC", "TTATTT", "AGACGG", "GCGTCA", "TCACTG"]),
         False)
예제 #3
0
 def test_mutant_diagonal_left(self):
     self.assertEqual(
         checker.is_mutant(
             ["TTGCAA", "CAGTGC", "TTATCT", "AGACGG", "ACCCTA", "TCACTA"]),
         True)
예제 #4
0
 def test_mutant_diagonal_right(self):
     self.assertEqual(
         checker.is_mutant(
             ["ATGCAA", "CAGTGC", "TTATGT", "AGAAGG", "ACCCTA", "TCACTA"]),
         True)
예제 #5
0
 def test_mutant_vertical(self):
     self.assertEqual(
         checker.is_mutant(
             ["TTGCGA", "CAGTGC", "TTATGT", "AGAAGG", "ACCCTA", "TCACTA"]),
         True)
예제 #6
0
 def test_mutant_horizontal(self):
     self.assertEqual(
         checker.is_mutant(
             ["TTGCAA", "CAGTGC", "TTATGT", "AGAAGG", "CCCCTA", "TCACTA"]),
         True)
def main():
    """This main function allow us to test the "is_mutant" funciton faster using command line
    An example of use is: python3 src/main.py ABCDEF FEDCBA ABCDEF FEDCBA ABCDEF AAAAAA
    """
    print('Result: ', checker.is_mutant(sys.argv[1:]))