Skip to content

Commit 2e7049d

Browse files
committed
stash
1 parent d38696b commit 2e7049d

File tree

7 files changed

+299
-89
lines changed

7 files changed

+299
-89
lines changed

backend/app/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
FROM python:3.8
2+
RUN apt-get update && apt-get install -y \
3+
default-mysql-client \
4+
&& rm -rf /var/lib/apt/lists/*
25
EXPOSE 5000
36
WORKDIR /app
47
COPY requirements.txt /app

backend/app/app.py

Lines changed: 100 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from flask_cors import CORS
55
from apscheduler.schedulers.background import BackgroundScheduler
66
from datetime import datetime, timedelta
7+
import time
78

89
app = Flask(__name__)
910
CORS(app, resources={r"/api/*": {"origins": "http://localhost:3000"}}, methods=["POST", "OPTIONS", "PUT"])
@@ -23,18 +24,52 @@
2324
'user': 'root',
2425
'password': 'root',
2526
'host': 'warehouse_db',
26-
'port': '3308',
27+
'port': '3309',
2728
'database': 'BrewandBrain_warehouse'
2829
}
30+
try:
31+
connection = mysql.connector.connect(**warehouse_db_config)
32+
print("Connected to the warehouse database successfully!")
33+
connection.close()
34+
except mysql.connector.Error as err:
35+
print(f"Error connecting to the warehouse database: {err}")
36+
def wait_for_databases():
37+
max_retries = 30
38+
retry_interval = 2 # seconds
39+
40+
operational_connection = None
41+
warehouse_connection = None
42+
43+
for _ in range(max_retries):
44+
try:
45+
operational_connection = mysql.connector.connect(**db_config)
46+
print("Connected to the operational database successfully!")
47+
48+
warehouse_connection = mysql.connector.connect(**warehouse_db_config)
49+
print("Connected to the warehouse database successfully!")
50+
51+
operational_connection.close()
52+
warehouse_connection.close()
53+
return
54+
except mysql.connector.Error as err:
55+
print(f"Error connecting to databases: {err}")
56+
time.sleep(retry_interval)
57+
58+
print("Unable to connect to databases after retries.")
2959

60+
wait_for_databases()
3061

3162
def get_db_connection(config):
63+
connection = None
3264
try:
3365
connection = mysql.connector.connect(**config)
34-
return connection
3566
except mysql.connector.Error as err:
3667
print(f"Error connecting to the database: {err}")
37-
return None
68+
return connection
69+
70+
def close_connection(connection):
71+
if connection:
72+
connection.close()
3873

3974
# User-related functions
4075
def write_to_users(data):
@@ -144,69 +179,79 @@ def get_all_reservations():
144179
return results
145180
except mysql.connector.Error as err:
146181
print(f"Error Fetching Reservations: {err}")
182+
147183
def perform_warehouse_process():
184+
operational_connection = get_db_connection(db_config)
185+
warehouse_connection = get_db_connection(warehouse_db_config)
186+
148187
try:
149-
# Connect to operational database
150-
operational_connection = get_db_connection(db_config)
151-
if operational_connection:
152-
operational_cursor = operational_connection.cursor(dictionary=True)
188+
if not operational_connection or not warehouse_connection:
189+
return # Return early if unable to connect to either database
190+
191+
with operational_connection.cursor(dictionary=True) as operational_cursor, \
192+
warehouse_connection.cursor() as warehouse_cursor:
153193

154-
# Connect to warehouse database
155-
warehouse_connection = get_db_connection(warehouse_db_config)
156-
if warehouse_connection:
157-
warehouse_cursor = warehouse_connection.cursor()
194+
operational_cursor.execute("""
195+
SELECT Users.UserID, Users.School, Users.Occupation, Reservations.StartTime, Reservations.EndTime
196+
FROM Users
197+
LEFT JOIN Reservations ON Users.UserID = Reservations.UserID
198+
""")
199+
users_data = operational_cursor.fetchall()
158200

201+
for user in users_data:
159202
try:
160-
# Extract data from the operational database
161-
operational_cursor.execute("""
162-
SELECT UserID, School, Occupation
163-
FROM Users
164-
""")
165-
users_data = operational_cursor.fetchall()
166-
167-
# Transform and load data into the warehouse
168-
for user in users_data:
169-
# Fetch reservations for the current user
170-
operational_cursor.execute("""
171-
SELECT StartTime, EndTime
172-
FROM Reservations
173-
WHERE UserID = %s
174-
""", (user['UserID'],))
175-
reservations_data = operational_cursor.fetchall()
176-
177-
# Insert user and reservation data into the warehouse
178-
for reservation in reservations_data:
179-
warehouse_cursor.execute(
180-
"""
181-
INSERT INTO UserSummary (UserID, School, Occupation, StartTime, EndTime)
182-
VALUES (%s, %s, %s, %s, %s)
183-
""",
184-
(user['UserID'], user['School'], user['Occupation'],
185-
reservation['StartTime'], reservation['EndTime'])
186-
)
187-
188-
# Commit changes to the warehouse database
189-
warehouse_connection.commit()
190-
print("Warehouse process completed successfully.")
191-
except Exception as e:
192-
print(f"Error during ETL process: {e}")
193-
warehouse_connection.rollback() # Rollback changes in case of an error
194-
195-
warehouse_cursor.close()
196-
warehouse_connection.close()
197-
198-
operational_cursor.close()
199-
operational_connection.close()
203+
warehouse_cursor.execute(
204+
"""
205+
INSERT INTO UserSummary (UserID, School, Occupation, StartTime, EndTime)
206+
VALUES (%s, %s, %s, %s, %s)
207+
ON DUPLICATE KEY UPDATE School=VALUES(School), Occupation=VALUES(Occupation),
208+
StartTime=VALUES(StartTime), EndTime=VALUES(EndTime)
209+
""",
210+
(user['UserID'], user['School'], user['Occupation'],
211+
user['StartTime'], user['EndTime'])
212+
)
213+
except mysql.connector.Error as e:
214+
print(f"Error processing user {user['UserID']}: {e}")
215+
216+
warehouse_connection.commit()
217+
print("Warehouse process completed successfully.")
218+
except mysql.connector.Error as e:
219+
print(f"Error during ETL process: {e}")
220+
warehouse_connection.rollback()
221+
finally:
222+
close_connection(operational_connection)
223+
close_connection(warehouse_connection)
200224

201-
except Exception as e:
202-
print(f"Error performing warehouse process: {e}")
203225

204226
# Initialize the BackgroundScheduler
205227
scheduler = BackgroundScheduler()
206228

207-
# Add the scheduled job to run the ETL process every 168 hours or 1 week
208-
scheduler.add_job(perform_warehouse_process, 'interval', hours=168)
229+
# Add the scheduled job to run the ETL process every 1 week
230+
scheduler.add_job(perform_warehouse_process, 'interval', weeks=1)
231+
232+
@app.route('/api/get-warehouse-data', methods=['GET'])
233+
def get_warehouse_data():
234+
try:
235+
warehouse_connection = get_db_connection(warehouse_db_config)
236+
if not warehouse_connection:
237+
return jsonify(error='Unable to connect to the warehouse database'), 500
238+
239+
with warehouse_connection.cursor(dictionary=True) as warehouse_cursor:
240+
warehouse_cursor.execute("SELECT * FROM UserSummary")
241+
warehouse_data = warehouse_cursor.fetchall()
209242

243+
return jsonify({'message': 'Warehouse data fetched successfully', 'warehouse_data': warehouse_data})
244+
245+
except Exception as e:
246+
print(f"Error fetching warehouse data: {e}")
247+
return jsonify(error='Error fetching warehouse data'), 500
248+
249+
finally:
250+
if warehouse_connection:
251+
warehouse_connection.close()
252+
print("Warehouse connection closed.")
253+
254+
210255
@app.route('/api/create-account', methods=['POST'])
211256
def create_account():
212257
try:

backend/app/tempCodeRunnerFile.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
sign
1+
connection.close()
2+
3+
# User-related functions
4+
def write_to_users(data):
5+
connection = get_db_connection(db_config)
6+
if connection:
7+
try:

backend/db/init.sql

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ CREATE TABLE QR_Codes (
3838
);
3939

4040

41-
INSERT INTO Users (GoogleID, Username, Email, Password, FirstName, LastName, PhoneNumber, UName, Birthday, Gender, School, Occupation, Level)
42-
VALUES
43-
('google_id_1', 'user_1', 'user1@example.com', 'password1', 'John', 'Doe', '1234567890', 'UName1', '1990-01-01', 'Male', 'School1', 'Occupation1', 'User'),
44-
('google_id_2', 'admin', 'user2@example.com', 'password', 'Jane', 'Doe', '9876543210', 'UName2', '1985-05-15', 'Female', 'School2', 'Occupation2', 'Admin'),
41+
-- INSERT INTO Users (GoogleID, Username, Email, Password, FirstName, LastName, PhoneNumber, UName, Birthday, Gender, School, Occupation, Level)
42+
-- VALUES
43+
-- ('google_id_1', 'user_1', 'user1@example.com', 'password1', 'John', 'Doe', '1234567890', 'UName1', '1990-01-01', 'Male', 'School1', 'Occupation1', 'User'),
44+
-- ('google_id_2', 'admin', 'user2@example.com', 'password', 'Jane', 'Doe', '9876543210', 'UName2', '1985-05-15', 'Female', 'School2', 'Occupation2', 'Admin'),
4545

46-
('google_id_3', 'user_3', 'user3@example.com', 'password3', 'Alice', 'Johnson', '5555555555', 'UName3', '1988-08-22', 'Female', 'School3', 'Occupation3', 'User'),
47-
('google_id_4', 'user_4', 'user4@example.com', 'password4', 'Bob', 'Smith', '6666666666', 'UName4', '1975-12-10', 'Male', 'School4', 'Occupation4', 'User');
46+
-- ('google_id_3', 'user_3', 'user3@example.com', 'password3', 'Alice', 'Johnson', '5555555555', 'UName3', '1988-08-22', 'Female', 'School3', 'Occupation3', 'User'),
47+
-- ('google_id_4', 'user_4', 'user4@example.com', 'password4', 'Bob', 'Smith', '6666666666', 'UName4', '1975-12-10', 'Male', 'School4', 'Occupation4', 'User');
4848

4949

50-
INSERT INTO Reservations (UserID, StartTime, EndTime, Seat)
51-
VALUES
52-
(1, '2023-01-01 08:00:00', '2023-01-01 12:00:00', 'A1'),
53-
(2, '2023-01-02 09:00:00', '2023-01-02 13:00:00', 'B2'),
50+
-- INSERT INTO Reservations (UserID, StartTime, EndTime, Seat)
51+
-- VALUES
52+
-- (1, '2023-01-01 08:00:00', '2023-01-01 12:00:00', 'A1'),
53+
-- (2, '2023-01-02 09:00:00', '2023-01-02 13:00:00', 'B2'),
5454

55-
(3, '2023-01-03 10:00:00', '2023-01-03 14:00:00', 'C3'),
56-
(4, '2023-01-04 11:00:00', '2023-01-04 15:00:00', 'D4');
55+
-- (3, '2023-01-03 10:00:00', '2023-01-03 14:00:00', 'C3'),
56+
-- (4, '2023-01-04 11:00:00', '2023-01-04 15:00:00', 'D4');

backend/db/warehouse_init.sql

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
1-
CREATE DATABASE BrewandBrain_warehouse;
1+
-- Create the BrewandBrain_warehouse database if not exists
2+
CREATE DATABASE IF NOT EXISTS BrewandBrain_warehouse;
23

4+
-- Use the BrewandBrain_warehouse database
35
USE BrewandBrain_warehouse;
46

5-
CREATE TABLE IF NOT EXISTS UserSummary(
7+
-- Drop the UserSummary table if it exists
8+
DROP TABLE IF EXISTS UserSummary;
9+
10+
-- Create the UserSummary table
11+
CREATE TABLE UserSummary (
612
UserID INT NOT NULL,
7-
School VARCHAR (100),
13+
ReservationID INT NOT NULL,
14+
School VARCHAR(100),
815
Occupation VARCHAR(100),
9-
StartTime DATETIME,
10-
EndTime DATETIME,
16+
StartTime VARCHAR(255) NOT NULL,
17+
EndTime VARCHAR(225) NOT NULL,
18+
Seat VARCHAR(50),
19+
Gender ENUM('Male', 'Female', 'Other'),
1120
PRIMARY KEY (UserID)
1221
);
1322

14-
CREATE INDEX idx_UserSumamry_UserID ON UserSummary(UserID);
23+
-- Create an index on the UserID column
24+
CREATE INDEX idx_UserSummary_UserID ON UserSummary(UserID);
25+
26+
-- INSERT INTO UserSummary (UserID, ReservationID, School, Occupation, StartTime, EndTime, Seat, Gender)
27+
-- VALUES
28+
-- (1, 101, 'University of the Philippines', 'Student', '2023-01-01 08:00:00', '2023-01-01 10:00:00', 'A1', 'Male'),
29+
-- (2, 102, 'Tech Institute', 'Engineer', '2023-01-02 09:00:00', '2023-01-02 12:00:00', 'B2', 'Female'),
30+
-- (3, 103, 'Business School', 'Entrepreneur', '2023-01-03 10:00:00', '2023-01-03 11:30:00', 'C3', 'Other');

backend/docker-compose.yml

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
version: "2"
1+
version: "3.8"
2+
23
services:
34
app:
45
build: ./app
5-
links:
6-
- db
76
ports:
87
- "5000:5000"
98
depends_on:
@@ -14,6 +13,12 @@ services:
1413
limits:
1514
cpus: "2"
1615
memory: "512M"
16+
environment:
17+
- SQLALCHEMY_DATABASE_URI=mysql://root:root@warehouse_db:3308/BrewandBrain_warehouse
18+
- FLASK_RUN_HOST=0.0.0.0
19+
- FLASK_ENV=development
20+
networks:
21+
- mynetwork
1722

1823
db:
1924
image: mysql:8.2
@@ -23,22 +28,19 @@ services:
2328
MYSQL_ROOT_PASSWORD: root
2429
volumes:
2530
- ./db/init.sql:/docker-entrypoint-initdb.d/init.sql
26-
deploy:
27-
resources:
28-
limits:
29-
cpus: "2"
30-
memory: "512M"
31+
networks:
32+
- mynetwork
3133

3234
warehouse_db:
3335
image: mysql:8.2
3436
ports:
35-
- "3308:3306"
37+
- "3309:3308"
3638
environment:
3739
MYSQL_ROOT_PASSWORD: root
3840
volumes:
3941
- ./db/warehouse_init.sql:/docker-entrypoint-initdb.d/warehouse_init.sql
40-
deploy:
41-
resources:
42-
limits:
43-
cpus: "2"
44-
memory: "512M"
42+
networks:
43+
- mynetwork
44+
45+
networks:
46+
mynetwork:

0 commit comments

Comments
 (0)