|
| 1 | +import sys |
| 2 | +import json |
| 3 | +import jsonlines |
| 4 | +import random |
| 5 | +from datetime import datetime, timedelta |
| 6 | +from faker import Faker |
| 7 | + |
| 8 | +fake = Faker() |
| 9 | + |
| 10 | +def generate_log(timestamp, service, logLevel, correlation_id, message): |
| 11 | + return { |
| 12 | + "timestamp": timestamp.isoformat(), |
| 13 | + "service": service, |
| 14 | + "logLevel": logLevel, |
| 15 | + "X-Correlation-ID": correlation_id, |
| 16 | + "message": message |
| 17 | + } |
| 18 | + |
| 19 | +def generate_search_log(user_id, correlation_id, timestamp): |
| 20 | + location = fake.city() |
| 21 | + guests = random.randint(1, 4) |
| 22 | + checkin = fake.date_between(start_date="today", end_date="+30d") |
| 23 | + checkout = fake.date_between(start_date=checkin, end_date=checkin + timedelta(days=10)) |
| 24 | + message = f"User {user_id} searching available hotels with criteria: {{\"location\":\"{location}\", \"checkin\":\"{checkin}\", \"checkout\":\"{checkout}\", \"guests\":{guests}}}." |
| 25 | + return generate_log(timestamp, "Search", "INFO", correlation_id, message) |
| 26 | + |
| 27 | +def generate_booking_log(user_id, correlation_id, timestamp): |
| 28 | + room_types = ["Standard", "Deluxe", "Suite"] |
| 29 | + room_type = random.choices(room_types, weights = [6, 3, 1], k=1)[0] |
| 30 | + |
| 31 | + if room_type == "Standard": |
| 32 | + low, high = (100, 200) |
| 33 | + elif room_type == "Deluxe": |
| 34 | + low, high = (150, 400) |
| 35 | + else: |
| 36 | + low, high = (300, 1000) |
| 37 | + |
| 38 | + price = random.randint(low, high) |
| 39 | + |
| 40 | + checkin = fake.date_between(start_date="+30d", end_date="+60d") |
| 41 | + checkout = fake.date_between(start_date=checkin, end_date=checkin + timedelta(days=10)) |
| 42 | + message = f"User {user_id} selected a hotel room with details: {{\"roomType\":\"{room_type}\", \"price\":{price}, \"checkin\":\"{checkin}\", \"checkout\":\"{checkout}\"}}." |
| 43 | + return generate_log(timestamp, "Booking", "INFO", correlation_id, message) |
| 44 | + |
| 45 | +def generate_payment_log(user_id, correlation_id, timestamp, success=True): |
| 46 | + payment_methods = ["Credit Card", "PayPal", "Bank Transfer"] |
| 47 | + payment_method = random.choice(payment_methods) |
| 48 | + amount = random.randint(100, 1000) |
| 49 | + if success: |
| 50 | + message = f"Processing payment for user ID {user_id}, amount: {amount} USD, payment method: {payment_method}." |
| 51 | + logLevel = "INFO" |
| 52 | + else: |
| 53 | + message = f"Payment failed for user ID {user_id}, amount: {amount} USD, reason: Insufficient funds." |
| 54 | + logLevel = "ERROR" |
| 55 | + return generate_log(timestamp, "Payment", logLevel, correlation_id, message) |
| 56 | + |
| 57 | +def generate_journey_logs(num_users): |
| 58 | + start_time = datetime.now() |
| 59 | + logs = [] |
| 60 | + |
| 61 | + for _ in range(1, num_users + 1): |
| 62 | + user_id = fake.uuid4().split("-")[0] |
| 63 | + correlation_id = fake.uuid4() |
| 64 | + timestamp = start_time + timedelta(seconds=random.randint(0, num_users * 10)) |
| 65 | + |
| 66 | + # User starts with a search |
| 67 | + for _ in range(1, random.randint(1, 20)): |
| 68 | + logs.append(generate_search_log(user_id, correlation_id, timestamp)) |
| 69 | + |
| 70 | + # Randomly decide if user drops out after search |
| 71 | + if random.random() < 0.2: |
| 72 | + continue |
| 73 | + |
| 74 | + timestamp += timedelta(seconds=5) |
| 75 | + # User proceeds to booking |
| 76 | + logs.append(generate_booking_log(user_id, correlation_id, timestamp)) |
| 77 | + |
| 78 | + # Randomly decide if user drops out after booking |
| 79 | + if random.random() < 0.1: |
| 80 | + continue |
| 81 | + |
| 82 | + timestamp += timedelta(seconds=5) |
| 83 | + # User proceeds to payment |
| 84 | + payment_success = random.random() >= 0.1 # 10% chance of payment failure |
| 85 | + logs.append(generate_payment_log(user_id, correlation_id, timestamp, success=payment_success)) |
| 86 | + |
| 87 | + if not payment_success: |
| 88 | + continue |
| 89 | + |
| 90 | + return logs |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + num_users = 100000 # Number of users to simulate |
| 94 | + logs = generate_journey_logs(num_users) |
| 95 | + |
| 96 | + # Print logs as JSON |
| 97 | + with jsonlines.Writer(sys.stdout) as out: |
| 98 | + for log in logs: |
| 99 | + out.write(log) |
0 commit comments