-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (66 loc) · 2.33 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import faker
import psycopg2
from datetime import datetime
import random
fake = faker.Faker()
def generate_transaction():
user = fake.simple_profile()
return {
"transactionId": fake.uuid4(),
"userId": user['username'],
"timestamp": datetime.utcnow().timestamp(),
"amount": round(random.uniform(10, 1000), 2),
"currency": random.choice(['USD', 'GBP']),
'city': fake.city(),
"country": fake.country(),
"merchantName": fake.company(),
"paymentMethod": random.choice(['credit_card', 'debit_card', 'online_transfer']),
"ipAddress": fake.ipv4(),
"voucherCode": random.choice(['', 'DISCOUNT10', '']),
'affiliateId': fake.uuid4()
}
def create_table(conn):
cursor = conn.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS transactions (
transaction_id VARCHAR(255) PRIMARY KEY,
user_id VARCHAR(255),
timestamp TIMESTAMP,
amount DECIMAL,
currency VARCHAR(255),
city VARCHAR(255),
country VARCHAR(255),
merchant_name VARCHAR(255),
payment_method VARCHAR(255),
ip_address VARCHAR(255),
voucher_code VARCHAR(255),
affiliateId VARCHAR(255)
)
""")
cursor.close()
conn.commit()
if __name__ == "__main__":
conn = psycopg2.connect(
host='localhost',
database='hr',
user='hr',
password='hr',
port=5432
)
create_table(conn)
transaction = generate_transaction()
cur = conn.cursor()
print(transaction)
cur.execute(
"""
INSERT INTO transactions(transaction_id, user_id, timestamp, amount, currency, city, country, merchant_name, payment_method,
ip_address, affiliateId, voucher_code)
VALUES (%s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
""", (transaction["transactionId"], transaction["userId"], datetime.fromtimestamp(transaction["timestamp"]).strftime('%Y-%m-%d %H:%M:%S'),
transaction["amount"], transaction["currency"], transaction["city"], transaction["country"],
transaction["merchantName"], transaction["paymentMethod"], transaction["ipAddress"],
transaction["affiliateId"], transaction["voucherCode"])
)
cur.close()
conn.commit()