""" Script for filling test SQL table. Need to set and in env variables of OS or in configuration of runner. For load testing. """ import base64 from datetime import datetime import json import random from uuid import uuid4 from sqlalchemy import Column, DateTime, Enum, Integer, String, Text from ytownership import config from ytownership.connectors import sqs from ytownership.connectors.mysql import BaseModel from ytownership.connectors.mysql import session_scope TEST_AMOUNT = 20 ISRCS = config.TEST_ISRCS BASE_TERRITORIES_LIST = ['US', 'CA', 'UA', 'RU', 'GB', 'AU', 'AT', 'AE', 'BE', 'BR', 'CN', 'CY', 'CZ', 'DE', 'EE', 'ES', 'FI', 'FR', 'GE', 'GL', 'HK', 'HR', 'IL', 'IN', 'IS'] YOUTUBE_SQS_QUEUE_TEST = '{}-yt-ownership-2'.format(config.ENVIRONMENT) class YouTubeApiLogTest(BaseModel): """Model that stores YouTube API call results """ __tablename__ = 'yt_api_log_test' id = Column(Integer, primary_key=True) content_owner_id = Column(String(100), nullable=False) correlation_id = Column(String(100), nullable=False) isrc = Column(String(20), nullable=False) territories = Column(Text, nullable=False) created_date_utc = Column(DateTime, nullable=False) status = Column(Enum('success', 'error'), nullable=False) code = Column(Integer, nullable=False) message = Column(Text, nullable=False) def random_territory_list(btl): """Return random list of 5 countries. Args: btl (list): base territories list. Returns: List of 5 random territories. """ result = list() while len(result) < 5: country = random.choice(btl) if country not in result: result.append(country) else: result = ['WW'] break return result def generate_batch(): """Generate one batch. Up to 10 messages in batch. Returns: Single batch. """ batch = list() for i in range(0, 10): # Body of message. payload = { 'content_owner_id': 'theorchardmusic', 'isrc': random.choice(ISRCS), 'territories': random_territory_list(BASE_TERRITORIES_LIST) } # generate correlation_id for message correlation_id = { 'Correlation-Id': { 'data_type': 'String', 'string_value': str(uuid4()) } } # push it to MySQL DB. push_to_sql(payload, correlation_id) payload_value = json.dumps(payload) payload_value = (base64. b64encode(payload_value.encode('utf-8')). decode('utf-8')) # push it to batch, for sending to SQS in batches. batch.append((str(uuid4()), payload_value, 0, correlation_id)) return batch def generate_batch_list(): """Compose batchs in list Returns: List of batches. """ batch_pull = list() for i in range(0, TEST_AMOUNT//10): batch_pull.append(generate_batch()) return batch_pull def push_to_sql(payload, correlation_id): """Push single message to MySQL. Args: payload (dict): message body. correlation_id (dict): generated correlation_id. Returns: True if no exceptions. """ with session_scope() as db_session: log = YouTubeApiLogTest() log.status = 'success' log.code = 200 log.message = 'OK' log.territories = ','.join(payload['territories']) log.isrc = payload['isrc'] log.content_owner_id = payload['content_owner_id'] log.correlation_id = (correlation_id ['Correlation-Id'] ['string_value']) log.created_date_utc = datetime.utcnow() db_session.add(log) return True def push_to_sqs(): """Push all generated batches to SQS. """ batches_list = generate_batch_list() sqs_queue = sqs.get_queue(YOUTUBE_SQS_QUEUE_TEST) for batch in batches_list: sqs_queue.write_batch(batch) if __name__ == '__main__': print('Start working') push_to_sqs() print('Finish working')