Skip to content

Commit ed0dc52

Browse files
authored
Merge pull request #1 from Timjini/ready-branch
all changes
2 parents 0118077 + f6fa9bc commit ed0dc52

14 files changed

+283
-59
lines changed

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# Use the official Python image from Docker Hub
21
FROM python:3.9-slim AS base
32

43
WORKDIR /app

app/app.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,6 @@
1818
time.sleep(5)
1919
print("table not found")
2020

21-
insert_fake_data(connection, "products", 1000)
22-
23-
# try:
24-
# insert_fake_data(connection, "products", 1000)
25-
# insert_fake_data(connection, "shoppers", 1000)
26-
# insert_fake_data(connection, "vendors", 1000)
27-
# # insert_order_items(cursor, num_order_items=5, num_orders=1000, num_products=100, num_vendors=50)
28-
# # insert_vendor_products(cursor, num_vendors=1000, num_products=1000)
29-
# # connection.commit()
30-
# print("Fake data inserted successfully")
31-
# except Exception as e:
32-
# print(f"Error inserting data: {e}")
33-
# connection.rollback()
34-
3521
except Error as e:
3622
print(f"Error: {e}")
3723

0 Bytes
Binary file not shown.
261 Bytes
Binary file not shown.

app/models/db_connection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ def check_tables_exist(connection, tables):
3535
print(f"Table {table} does not exist. Waiting for table to be created...")
3636
return False
3737
return True
38+

app/models/db_create_tables.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import mysql.connector
22
from mysql.connector import Error
3-
from services.data_insertion import insert_fake_data , insert_order_items, insert_vendor_products
4-
from services.order_data_insertion import insert_orders_data, inset_order_items_data
3+
from services.data_insertion import insert_fake_data, insert_vendor_products
4+
from services.order_data_insertion import insert_orders_data, insert_order_items_data, insert_revenue_data, insert_cost_data
5+
from services.shopper_data_insertion import insert_reviews_data, insert_time_spent_data
56

67

78
def create_tables_from_sql(connection, sql_file_path):
@@ -29,12 +30,12 @@ def create_tables_from_sql(connection, sql_file_path):
2930
insert_fake_data(cursor, connection, "shoppers", 1000)
3031
insert_fake_data(cursor, connection, "vendors", 1000)
3132
insert_vendor_products(cursor, connection)
32-
insert_orders_data(cursor, connection)
33-
inset_order_items_data(cursor, connection, max_items_per_order=5)
34-
# insert_reviews_data(cursor, connection)
35-
# insert_time_spent_data(cursor, connection)
36-
# insert_costs_data(cursor, connection)
37-
# insert_revenue_data(cursor, connection)
33+
insert_orders_data(cursor, connection, num_orders=1000)
34+
insert_order_items_data(cursor, connection, max_items_per_order=5)
35+
insert_revenue_data(cursor, connection)
36+
insert_reviews_data(cursor, connection, num_reviews=1000)
37+
insert_time_spent_data(cursor, connection, num_records=1000)
38+
insert_cost_data(cursor, connection)
3839
connection.commit()
3940
print("All tables created successfully.")
4041
return True
108 Bytes
Binary file not shown.
1.89 KB
Binary file not shown.
1.96 KB
Binary file not shown.

app/services/data_insertion.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from faker import Faker
2+
from faker_commerce import Provider as CommerceProvider
23
import random
34

45
fake = Faker()
5-
6+
fake.add_provider(CommerceProvider)
67
def insert_fake_data(cursor, connection, table_name, num_rows=1000):
78
print(f"Inserting data into {table_name}...")
89
try:
910
for _ in range(num_rows):
1011
if table_name == "products":
11-
name = fake.word()
12+
name = fake.ecommerce_name()
1213
cost = round(fake.random_number(digits=2), 2)
13-
description = fake.text(max_nb_chars=200)
14+
description = f"{name}: {fake.sentence()}"
1415
cursor.execute(
1516
f"INSERT INTO {table_name} (name, cost, description) VALUES (%s, %s, %s)",
1617
(name, cost, description)
@@ -38,17 +39,15 @@ def insert_fake_data(cursor, connection, table_name, num_rows=1000):
3839
f"INSERT INTO {table_name} (contact_name, company_name, contact_email, contact_phone, address, tax_number, commission_rate) VALUES (%s, %s, %s, %s, %s, %s, %s)",
3940
(contact_name, company_name, contact_email, contact_phone, address, tax_number, commission_rate)
4041
)
41-
# Commit the transaction
4242
connection.commit()
4343
print(f"{num_rows} rows inserted into {table_name}")
4444
except Exception as e:
45-
# Rollback in case of any error
4645
connection.rollback()
4746
print(f"Error inserting data into {table_name}: {e}")
4847

4948

5049
def insert_orders_for_shoppers(cursor, num_orders):
51-
# Fetch all shopper IDs
50+
# fetch all shopper IDs
5251
cursor.execute("SELECT id FROM shoppers")
5352
shoppers = cursor.fetchall()
5453

@@ -101,7 +100,6 @@ def insert_vendor_products(cursor, connection):
101100
print("No products found.")
102101
return
103102

104-
# vendor-product associations
105103
for vendor in vendors:
106104
vendor_id = vendor[0]
107105
commission_rate = vendor[1]
@@ -110,7 +108,7 @@ def insert_vendor_products(cursor, connection):
110108
product_id = product[0]
111109
product_cost = product[1]
112110

113-
# calcualte vendor product based on commission
111+
# calculate vendor product based on commission
114112
price = round(product_cost * (1 + commission_rate / 100), 2)
115113

116114
try:
@@ -125,6 +123,5 @@ def insert_vendor_products(cursor, connection):
125123
connection.commit()
126124
print("Vendor-product associations inserted successfully.")
127125
except Exception as e:
128-
# rollback when issue
129126
connection.rollback()
130127
print(f"Error inserting vendor-products: {e}")

0 commit comments

Comments
 (0)