-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_clickstream_with_time.py
63 lines (50 loc) · 1.95 KB
/
generate_clickstream_with_time.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Standard library imports
import os
import sys
import random
import datetime
import urllib.parse
# Third-party imports
from dotenv import load_dotenv
import mysql.connector
# Load environment variables from .env file
load_dotenv()
# Debugging: Print the environment variable
# print(f"DB_PASSWORD from os.environ: {os.environ.get('DB_PASSWORD')}")
db_password = os.getenv('DB_PASSWORD')
if not db_password:
sys.exit("Error: The DB_PASSWORD environment variable is not set.")
# URL-encode the password
db_password_encoded = urllib.parse.quote_plus(db_password)
# Establish a connection to the database
connection = mysql.connector.connect(
host="localhost",
user="root",
password=db_password,
database="intent_based_segmentation"
)
cursor = connection.cursor()
# Function to generate synthetic data for clickstream with time_spent
def generate_clickstream_data(num_records):
data = []
for _ in range(num_records):
user_id = random.randint(1, 1000)
timestamp = datetime.datetime.now() - datetime.timedelta(days=random.randint(0, 30))
url = f"https://example.com/page{random.randint(1, 100)}"
referrer = random.choice(["https://google.com", "https://facebook.com", "https://twitter.com", None])
device = random.choice(["desktop", "mobile", "tablet"])
time_spent = random.randint(5, 300) # Time spent in seconds (between 5 and 300 seconds)
data.append((user_id, timestamp, url, referrer, device, time_spent))
return data
# Generate and insert clickstream data with time_spent into the database
clickstream_data = generate_clickstream_data(1000)
for record in clickstream_data:
cursor.execute("""
INSERT INTO clickstream (user_id, timestamp, url, referrer, device, time_spent)
VALUES (%s, %s, %s, %s, %s, %s)
""", record)
# Commit the changes
connection.commit()
# Close the connection
connection.close()
print("Clickstream data with time_spent added to the database.")