def get_recent_items_page( cls, table: Table, exclusive_start_key: t.Optional[DynamoDBCursorKey] = None ) -> RecentItems: """ Get a paginated list of recent match records. Subsequent calls must use `return_value.last_evaluated_key`. """ if not exclusive_start_key: # Evidently, https://github.com/boto/boto3/issues/2813 boto is able # to distinguish fun(Parameter=None) from fun(). So, we can't use # exclusive_start_key's optionality. We have to do an if clause! # Fun! result = table.query( IndexName="GSI-2", Limit=100, ScanIndexForward=False, KeyConditionExpression=Key("GSI2-PK").eq( DynamoDBItem.get_dynamodb_type_key(cls.__name__)), ) else: result = table.query( IndexName="GSI-2", Limit=100, ExclusiveStartKey=exclusive_start_key, ScanIndexForward=False, KeyConditionExpression=Key("GSI2-PK").eq( DynamoDBItem.get_dynamodb_type_key(cls.__name__)), ) return RecentItems( t.cast(DynamoDBCursorKey, result.get("LastEvaluatedKey", None)), cls._result_items_to_records(result["Items"]), )
def get_from_content_id( cls, table: Table, content_id: str, signal_type: t.Optional[t.Type[SignalType]] = None, ) -> t.List["PipelineHashRecord"]: """ Returns all available PipelineHashRecords for a content_id. """ expected_pk = cls.get_dynamodb_content_key(content_id) if signal_type is None: condition_expression = Key("PK").eq(expected_pk) & Key("SK").begins_with( DynamoDBItem.TYPE_PREFIX ) else: condition_expression = Key("PK").eq(expected_pk) & Key("SK").eq( DynamoDBItem.get_dynamodb_type_key(signal_type.get_name()) ) return cls._result_items_to_records( table.query( KeyConditionExpression=condition_expression, ).get("Items", []) )
def get_from_content_id(cls, table: Table, content_id: str) -> t.List["MatchRecord"]: """ Return all matches for a content_id. """ content_key = DynamoDBItem.get_dynamodb_content_key(content_id) source_prefix = DynamoDBItem.SIGNAL_KEY_PREFIX return cls._result_items_to_records( table.query(KeyConditionExpression=Key("PK").eq(content_key) & Key("SK").begins_with(source_prefix), ).get( "Items", []))
def get_from_signal(cls, table: Table, signal_id: t.Union[str, int], signal_source: str) -> t.List["MatchRecord"]: """ Return all matches for a signal. Needs source and id to uniquely identify a signal. """ signal_key = DynamoDBItem.get_dynamodb_signal_key( signal_source, signal_id) return cls._result_items_to_records( table.query( IndexName="GSI-1", KeyConditionExpression=Key("GSI1-PK").eq(signal_key), ).get("Items", []))
def get_recent_items_page( cls, table: Table, signal_type_mapping: HMASignalTypeMapping, exclusive_start_key: t.Optional[DynamoDBCursorKey] = None, ) -> PaginatedResponse["PipelineHashRecord"]: """ Get a paginated list of recent items. """ if not exclusive_start_key: # Evidently, https://github.com/boto/boto3/issues/2813 boto is able # to distinguish fun(Parameter=None) from fun(). So, we can't use # exclusive_start_key's optionality. We have to do an if clause! # Fun! result = table.query( IndexName="GSI-2", ScanIndexForward=False, Limit=100, KeyConditionExpression=Key("GSI2-PK").eq( DynamoDBItem.get_dynamodb_type_key(cls.__name__)), ) else: result = table.query( IndexName="GSI-2", ExclusiveStartKey=exclusive_start_key, ScanIndexForward=False, Limit=100, KeyConditionExpression=Key("GSI2-PK").eq( DynamoDBItem.get_dynamodb_type_key(cls.__name__)), ) return PaginatedResponse( t.cast(DynamoDBCursorKey, result.get("LastEvaluatedKey", None)), cls._result_items_to_records( result["Items"], signal_type_mapping=signal_type_mapping), )