def lambda_handler(event, context): """ This is the main entry point for performing an action. The action evaluator puts an action message on the actions queue and here's where they're popped off and dealt with. """ records_table = dynamodb.Table(DYNAMODB_TABLE) lambda_init_once() for sqs_record in event["Records"]: # TODO research max # sqs records / lambda_handler invocation action_message = ActionMessage.from_aws_json(sqs_record["body"]) logger.info("Performing action: action_message = %s", action_message) if action_performer := ActionPerformer.get( action_message.action_label.value): action_performer.perform_action(action_message) ActionEvent( content_id=action_message.content_key, performed_at=datetime.datetime.now(), action_label=action_message.action_label.value, # v0 Hacks: the label rules model is super mutable we store basically the whole state # for each action performed. (~gross but until we have a unique id and version # it's what we've got). # Right now this just make json blob for action_performer and a list of # json blobs for action_rules that we can store and recover if needed. action_performer=action_performer.to_aws_json(), action_rules=[ rule.to_aws_json() for rule in action_message.action_rules ], ).write_to_table(records_table)
def test_action_message_serialization_and_deserialization(self): enqueue_mini_castle_for_review_action_label = ActionLabel( "EnqueueMiniCastleForReview") action_rules = [ ActionRule( name="Enqueue Mini-Castle for Review", action_label=enqueue_mini_castle_for_review_action_label, must_have_labels=set([ BankIDClassificationLabel("303636684709969"), ClassificationLabel("true_positive"), ]), must_not_have_labels=set( [BankedContentIDClassificationLabel("3364504410306721")]), ), ] banked_signal = BankedSignal( banked_content_id="4169895076385542", bank_id="303636684709969", bank_source="te", ) banked_signal.add_classification("true_positive") action_message = ActionMessage( content_key="images/mini-castle.jpg", content_hash= "361da9e6cf1b72f5cea0344e5bb6e70939f4c70328ace762529cac704297354a", matching_banked_signals=[banked_signal], action_label=enqueue_mini_castle_for_review_action_label, action_rules=action_rules, ) action_message_aws_json = action_message.to_aws_json() action_message_2 = ActionMessage.from_aws_json(action_message_aws_json) self.assertEqual(action_message_2.action_label, enqueue_mini_castle_for_review_action_label)