|
| 1 | +import json |
| 2 | +import math |
| 3 | +import os |
| 4 | +import random |
| 5 | +import time |
| 6 | + |
| 7 | +import barnum |
| 8 | +from kafka import KafkaProducer |
| 9 | +from mysql.connector import Error, connect |
| 10 | + |
| 11 | +# CONFIG |
| 12 | +vendorSeedCount = 100 |
| 13 | +userSeedCount = 10000 |
| 14 | +itemSeedCount = 1000 |
| 15 | +purchaseGenCount = 5000000 |
| 16 | +purchaseGenEveryMS = 100 |
| 17 | +pageviewMultiplier = 75 # Translates to 75x purchases, currently 750/sec or 65M/day |
| 18 | +itemInventoryMin = 1000 |
| 19 | +itemInventoryMax = 5000 |
| 20 | +itemPriceMin = 5 |
| 21 | +itemPriceMax = 500 |
| 22 | +mysqlHost = "mysql" |
| 23 | +mysqlPort = "3306" |
| 24 | +mysqlUser = "mysqluser" |
| 25 | +mysqlPass = "mysqlpw" |
| 26 | +kafkaHostPort = os.getenv("KAFKA_ADDR", "redpanda:9092") |
| 27 | +kafkaTopic = "pageviews" |
| 28 | +debeziumHostPort = "debezium:8083" |
| 29 | +channels = ["organic search", "paid search", "referral", "social", "display"] |
| 30 | +categories = ["widgets", "gadgets", "doodads", "clearance"] |
| 31 | + |
| 32 | +# INSERT TEMPLATES |
| 33 | +vendor_insert = "INSERT INTO shop.vendors (name) VALUES ( %s )" |
| 34 | +item_insert = "INSERT INTO shop.items (vendor_id, name, category, price, inventory) VALUES ( %s, %s, %s, %s, %s )" |
| 35 | +user_insert = "INSERT INTO shop.users (email, is_vip) VALUES ( %s, %s )" |
| 36 | +purchase_insert = "INSERT INTO shop.purchases (user_id, item_id, quantity, purchase_price) VALUES ( %s, %s, %s, %s )" |
| 37 | + |
| 38 | +# Initialize Kafka |
| 39 | +producer = KafkaProducer( |
| 40 | + bootstrap_servers=[kafkaHostPort], |
| 41 | + value_serializer=lambda x: json.dumps(x).encode("utf-8"), |
| 42 | +) |
| 43 | + |
| 44 | +def generatePageview(viewer_id, target_id, page_type): |
| 45 | + return { |
| 46 | + "user_id": viewer_id, |
| 47 | + "url": f"/{page_type}/{target_id}", |
| 48 | + "channel": random.choice(channels), |
| 49 | + "received_at": int(time.time()), |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | +try: |
| 54 | + with connect( |
| 55 | + host=mysqlHost, |
| 56 | + user=mysqlUser, |
| 57 | + password=mysqlPass, |
| 58 | + ) as connection: |
| 59 | + with connection.cursor() as cursor: |
| 60 | + print("Seeding data...") |
| 61 | + print(" - Vendors") |
| 62 | + cursor.executemany( |
| 63 | + vendor_insert, |
| 64 | + [ |
| 65 | + ( |
| 66 | + [barnum.create_company_name()] |
| 67 | + ) |
| 68 | + for i in range(vendorSeedCount) |
| 69 | + ], |
| 70 | + ) |
| 71 | + print(" - Items") |
| 72 | + cursor.executemany( |
| 73 | + item_insert, |
| 74 | + [ |
| 75 | + ( |
| 76 | + random.randint(1, vendorSeedCount), |
| 77 | + barnum.create_nouns(), |
| 78 | + random.choice(categories), |
| 79 | + random.randint(itemPriceMin * 100, itemPriceMax * 100) / 100, |
| 80 | + random.randint(itemInventoryMin, itemInventoryMax), |
| 81 | + ) |
| 82 | + for i in range(itemSeedCount) |
| 83 | + ], |
| 84 | + ) |
| 85 | + cursor.executemany( |
| 86 | + user_insert, |
| 87 | + [ |
| 88 | + (barnum.create_email(), (random.randint(0, 10) > 8)) |
| 89 | + for i in range(userSeedCount) |
| 90 | + ], |
| 91 | + ) |
| 92 | + connection.commit() |
| 93 | + |
| 94 | + print("Getting item ID and PRICEs...") |
| 95 | + cursor.execute("SELECT id, price FROM shop.items") |
| 96 | + item_prices = [(row[0], row[1]) for row in cursor] |
| 97 | + |
| 98 | + print("Preparing to loop + seed kafka pageviews and purchases") |
| 99 | + for i in range(purchaseGenCount): |
| 100 | + # Get a user and item to purchase |
| 101 | + purchase_item = random.choice(item_prices) |
| 102 | + purchase_user = random.randint(0, userSeedCount - 1) |
| 103 | + purchase_quantity = random.randint(1, 5) |
| 104 | + |
| 105 | + # Write purchaser pageview |
| 106 | + producer.send( |
| 107 | + kafkaTopic, |
| 108 | + key=str(purchase_user).encode("ascii"), |
| 109 | + value=generatePageview(purchase_user, purchase_item[0], "products"), |
| 110 | + ) |
| 111 | + |
| 112 | + # Write random pageviews to products or profiles |
| 113 | + pageviewOscillator = int( |
| 114 | + pageviewMultiplier + (math.sin(time.time() / 1000) * 50) |
| 115 | + ) |
| 116 | + for i in range(pageviewOscillator): |
| 117 | + rand_user = random.randint(0, userSeedCount) |
| 118 | + rand_page_type = random.choice(["products", "profiles"]) |
| 119 | + target_id_max_range = ( |
| 120 | + itemSeedCount if rand_page_type == "products" else userSeedCount |
| 121 | + ) |
| 122 | + producer.send( |
| 123 | + kafkaTopic, |
| 124 | + key=str(rand_user).encode("ascii"), |
| 125 | + value=generatePageview( |
| 126 | + rand_user, |
| 127 | + random.randint(0, target_id_max_range), |
| 128 | + rand_page_type, |
| 129 | + ), |
| 130 | + ) |
| 131 | + |
| 132 | + # Write purchase row |
| 133 | + cursor.execute( |
| 134 | + purchase_insert, |
| 135 | + ( |
| 136 | + purchase_user, |
| 137 | + purchase_item[0], |
| 138 | + purchase_quantity, |
| 139 | + purchase_item[1] * purchase_quantity, |
| 140 | + ), |
| 141 | + ) |
| 142 | + connection.commit() |
| 143 | + |
| 144 | + # Pause |
| 145 | + time.sleep(purchaseGenEveryMS / 1000) |
| 146 | + |
| 147 | + connection.close() |
| 148 | + |
| 149 | +except Error as e: |
| 150 | + print(e) |
0 commit comments