-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
543 lines (447 loc) · 16.2 KB
/
Copy pathproject.py
File metadata and controls
543 lines (447 loc) · 16.2 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import sys
import mysql.connector
import csv
# SQL SET UP -------------------------------------------------------------------------------------------------------------------------------
USER = "test"
PASSWORD = "password"
DATABASE = "cs122a"
DB_SETUP = """
DROP DATABASE IF EXISTS cs122a;
CREATE DATABASE cs122a;
USE cs122a;
-- User Table
CREATE TABLE User (
UCINetID VARCHAR(20) PRIMARY KEY NOT NULL,
FirstName VARCHAR(50),
MiddleName VARCHAR(50),
LastName VARCHAR(50)
);
CREATE TABLE UserEmail (
UCINetID VARCHAR(20) NOT NULL,
Email VARCHAR(255),
PRIMARY KEY (UCINetID, Email),
FOREIGN KEY (UCINetID) REFERENCES User(UCINetID)
ON DELETE CASCADE
);
-- Student Delta Table
CREATE TABLE Student (
UCINetID VARCHAR(20) PRIMARY KEY NOT NULL,
FOREIGN KEY (UCINetID) REFERENCES User(UCINetID)
ON DELETE CASCADE
);
-- Administrator Delta Table
CREATE TABLE Administrator (
UCINetID VARCHAR(20) PRIMARY KEY NOT NULL,
FOREIGN KEY (UCINetID) REFERENCES User(UCINetID)
ON DELETE CASCADE
);
-- Course Table
CREATE TABLE Course (
CourseID INT PRIMARY KEY NOT NULL ,
Title VARCHAR(100),
Quarter VARCHAR(20)
);
-- Project Table
CREATE TABLE Project (
ProjectID INT PRIMARY KEY NOT NULL,
Name VARCHAR(100),
Description TEXT,
CourseID INT NOT NULL,
FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
ON DELETE CASCADE
);
-- Machine Table
CREATE TABLE Machine (
MachineID INT PRIMARY KEY NOT NULL,
Hostname VARCHAR(255),
IPAddress VARCHAR(15),
OperationalStatus VARCHAR(50),
Location VARCHAR(255)
);
-- Use Relationship Table
CREATE TABLE StudentUse (
ProjectID INT,
UCINetID VARCHAR(20),
MachineID INT,
StartDate DATE,
EndDate DATE,
PRIMARY KEY (ProjectID, UCINetID, MachineID),
FOREIGN KEY (ProjectID) REFERENCES Project(ProjectID)
ON DELETE CASCADE,
FOREIGN KEY (UCINetID) REFERENCES Student(UCINetID)
ON DELETE CASCADE,
FOREIGN KEY (MachineID) REFERENCES Machine(MachineID)
ON DELETE CASCADE
);
-- Administrator Machine Management Table
CREATE TABLE AdministratorManageMachines (
AdministratorUCINetID VARCHAR(20),
MachineID INT,
PRIMARY KEY (AdministratorUCINetID, MachineID),
FOREIGN KEY (AdministratorUCINetID) REFERENCES Administrator(UCINetID)
ON DELETE CASCADE,
FOREIGN KEY (MachineID) REFERENCES Machine(MachineID)
ON DELETE CASCADE
);
"""
# UTILITY FILE -----------------------------------------------------------------------------------------------------------
def getCsvDataQuery(file_path):
query = "INSERT INTO"
if ('admins.csv' in file_path):
query += " Administrator "
elif ('courses.csv' in file_path):
query += " Course "
elif ('emails.csv' in file_path):
query += " UserEmail "
elif ('machines.csv' in file_path):
query += " Machine "
elif ('manage.csv' in file_path):
query += " AdministratorManageMachines "
elif ('projects.csv' in file_path):
query += " Project "
elif ('students.csv' in file_path):
query += " Student "
elif ('use.csv' in file_path):
query += " StudentUse "
elif ('users.csv' in file_path):
query += " User "
query += "VALUES \n"
with open(file_path, 'r') as file:
csv_reader = csv.reader(file)
for line in csv_reader:
query += "("
for value in line:
query += "\'" + value + "\', "
query = query[:-2] + "), \n"
query = query[:-3] + ";"
return query
def execute_command(db_connection, cursor, sql_command):
'''
Executes sql command in connection.
:return: Bool, optional results
'''
try:
# print("Successfully connected to the database")
# print("Initialization begin")
cursor.execute(sql_command)
result = cursor.fetchall()
# print(result)
db_connection.commit()
# print("Initialization end successfully")
return "Success", result, cursor
except Exception as e:
print(e)
return "Fail", []
def printRows(result):
'''
Formats result printing from table to csv
- arg: result table
- returns: None
'''
if result[0] == 'Fail':
print(result)
elif result[0] == 'Success':
for record in result[1]:
formatted_record = ','.join(str(value) for value in record)
print(formatted_record)
else:
for record in result:
formatted_record = ','.join(str(value) for value in record)
print(formatted_record)
# MANUSH FILE -------------------------------------------------------------------------------------------------------------------
def deleteStudent(db_connection, cursor, argv): # task 4
'''
Delete the student in both the User and Student table.
argv - UCINetID
return: bool
'''
sql_command = f"""
DELETE FROM User
WHERE UCINetID = '{argv[2]}';
"""
delete = execute_command(db_connection, cursor, sql_command)
student_deleted = delete[2].rowcount > 0
if student_deleted and delete[0] == "Success":
print("Success")
else:
print("Fail")
def updateCourse(db_connection, cursor, argv): # task 7
'''
Update the title of a course.
argv: CourseId, title
:return: Bool
'''
sql_command = f"""
UPDATE Course
SET Title = '{argv[3]}'
WHERE CourseID = {argv[2]};
"""
update = execute_command(db_connection, cursor, sql_command)
course_updated = update[2].rowcount > 0
if course_updated and update[0] == "Success":
print("Success")
else:
print("Fail")
def adminEmails(db_connection, cursor, argv): # task 10
'''
Given a machine ID, find all administrators of that machine. List the emails of those administrators. Ordered by netid ascending.
argv: machineId
:return: Table - UCINETId,first name,middle name,last name,list of email
'''
sql_command = f"""
SELECT
User.UCINetID,
User.FirstName,
User.MiddleName,
User.LastName,
GROUP_CONCAT(UserEmail.Email ORDER BY UserEmail.Email ASC SEPARATOR ';') AS ListOfEmail
FROM
Administrator
INNER JOIN
AdministratorManageMachines ON Administrator.UCINetID = AdministratorManageMachines.AdministratorUCINetID
INNER JOIN
User ON Administrator.UCINetID = User.UCINetID
INNER JOIN
UserEmail ON User.UCINetID = UserEmail.UCINetID
WHERE
AdministratorManageMachines.MachineID = {argv[2]}
GROUP BY
User.UCINetID
ORDER BY
User.UCINetID ASC;
"""
res = execute_command(db_connection, cursor, sql_command)
printRows(res)
# MEW FILE ----------------------------------------------------------------------------------------------------------------------
def addEmail(db_connection, cursor, argv): # task 3
'''
Add email to a user email table.
argv - UCINetID, email
return: bool
'''
sql_command = f"""
INSERT INTO UserEmail (UCINetID, email)
VALUES ('{argv[2]}', '{argv[3]}');
"""
email = execute_command(db_connection, cursor, sql_command)
email_inserted = email[2].rowcount > 0
if email_inserted and email[0] == "Success":
print("Success")
else:
print("Fail")
def insertUse(db_connection, cursor, argv): # task 6
'''
Insert a new use record for student use machine for project.
argv: ProjID, UCINetID, MachineID, start, end
ex: 2005 testID 102 2023-01-09 2023-03-10
:return: Bool
'''
sql_command = f"""
INSERT INTO StudentUse
(ProjectID, UCINetID, MachineID, StartDate, EndDate)
VALUES ({argv[2]}, '{argv[3]}', {argv[4]}, '{argv[5]}', '{argv[6]}');
"""
use = execute_command(db_connection, cursor, sql_command)
use_inserted = use[2].rowcount > 0
if use_inserted and use[0] == "Success":
print("Success")
else:
print("Fail")
def popularCourse(db_connection, cursor, argv): # task 9
'''
List the top N course that has the most students attended.
Ordered by studentCount, courseID descending.
argv: N
:return: Table - CourseId,title,studentCount
'''
sql_command = f"""
SELECT
C.CourseID, C.Title, COUNT(DISTINCT S.UCINetID) AS studentCount
FROM
Course as C
JOIN
Project as P ON P.CourseID = C.CourseID
JOIN
StudentUse as S ON S.ProjectID = P.ProjectID
GROUP BY
C.CourseID, C.Title
ORDER BY
studentCount DESC, C.CourseID
LIMIT
{argv[2]};
"""
res = execute_command(db_connection, cursor, sql_command)
printRows(res)
def machineUsage(db_connection, cursor, argv): # task 12
'''
Given a course id, count the number of usage of each machine in that course. Each unique
record in the MachineUse table counts as one usage. Machine that are not used in the course
should have a count of 0 instead of NULL. Ordered by machineId descending.
argv: courseID
:return: Table - machineID,hostname,ipAddr,count
'''
sql_command = f"""
SELECT M.MachineID, M.Hostname, M.IPAddress, Count(M.MachineID) as useCount
FROM StudentUse as U
LEFT JOIN Machine as M ON M.MachineID=U.MachineID
LEFT JOIN Project as P ON P.ProjectID=U.ProjectID
WHERE P.CourseID = {argv[2]}
GROUP BY M.MachineID, M.Hostname, M.IPAddress, P.CourseID
UNION
SELECT M1.MachineID, M1.Hostname, M1.IPAddress, 0
FROM Machine as M1
WHERE M1.MachineID NOT IN (
SELECT M2.MachineID
FROM StudentUse as U2, Machine as M2, Project as P2
WHERE M2.MachineID=U2.MachineID and P2.ProjectID=U2.ProjectID and P2.CourseID = {argv[2]}
GROUP BY M2.MachineID, M2.Hostname, M2.IPAddress, P2.CourseID
)
ORDER BY MachineID DESC;
"""
res = execute_command(db_connection, cursor, sql_command)
printRows(res)
# JOCELYN FILE ------------------------------------------------------------------------------------------------------------------------
def insertStudent(db_connection, cursor, argv): #task 2
'''
Insert a new student into the related tables.
argv - UCINetID, email, First, Middle, Last
return: bool
'''
user_insert = f"""
INSERT INTO User (UCINetID, FirstName, MiddleName, LastName)
SELECT '{argv[2]}', '{argv[4]}', '{argv[5]}', '{argv[6]}'
WHERE NOT EXISTS (
SELECT 1 FROM User WHERE UCINetID = '{argv[2]}'
);
"""
email_insert = f"""
INSERT INTO UserEmail (UCINetID, Email)
VALUES ('{argv[2]}', '{argv[3]}');
"""
student_insert = f"""
INSERT INTO Student (UCINetID)
VALUES ('{argv[2]}');
"""
user = execute_command(db_connection, cursor, user_insert)
user_inserted = user[2].rowcount > 0
if user_inserted and user[0] == "Success":
execute_command(db_connection, cursor, email_insert)
execute_command(db_connection, cursor, student_insert)
print("Success")
else:
print("Fail")
def insertMachine(db_connection, cursor, argv): #task 5
'''
Insert a new machine.
argv - MachineID, hostname, IPAddr, status, location
return: bool
'''
sql_command = f"""
INSERT INTO Machine (MachineID, hostname, IPAddr, status)
SELECT '{argv[2]}', '{argv[3]}', '{argv[4]}', '{argv[5]}'
WHERE NOT EXISTS (
SELECT 1 FROM Machine WHERE MachineID = '{argv[2]}'
);
"""
machine = execute_command(db_connection, cursor, sql_command)
machine_inserted = machine[2].rowcount > 0
if machine_inserted and machine[0] == "Success":
print("Success")
else:
print("Fail")
def listCourse(db_connection, cursor, argv): # task 8
'''
Given a student ID, list all unique courses the student attended. Ordered by courseId ascending.
argv - UCINetID
return: Table - CourseId,title,quarter
'''
sql_command = f"""
SELECT DISTINCT
c.CourseID, c.Title, c.Quarter
FROM
StudentUse su, Project p, Course c
WHERE
su.UCINetID = '{argv[2]}' and
su.ProjectID = p.ProjectID and
p.CourseID = c.CourseID
ORDER BY
c.CourseID ASC;
"""
res = execute_command(db_connection, cursor, sql_command)
printRows(res)
def activeStudent(db_connection, cursor, argv): # task 11
'''
Given a machine Id, find all active students that used it more than N times (including N) in a
specific time range (including start and end date). Ordered by netid ascending. N will be at least 1.
argv - MachineID, N, start, end
return: Table - UCINetId,first name,middle name,last name
'''
sql_command = f"""
SELECT
u.UCINetID, u.FirstName, u.MiddleName, u.LastName
FROM
StudentUse su, User u
WHERE
su.MachineID = {argv[2]} and
su.StartDate >= '{argv[4]}' and
su.EndDate <= '{argv[5]}' and
su.UCINetID = u.UCINetID
GROUP BY
u.UCINetID
HAVING
Count(*) >= {argv[3]}
"""
res = execute_command(db_connection, cursor, sql_command)
printRows(res)
def main():
args = sys.argv[1:]
func = args[0]
sql_file_path = 'cs122a.sql'
db_connection = mysql.connector.connect(user=USER, password=PASSWORD, database=DATABASE)
cursor = db_connection.cursor()
if func == "import":
sql_command = f"""
SELECT
(SELECT COUNT(*) FROM User) AS NumberOfUsers,
(SELECT COUNT(*) FROM Machine) AS NumberOfMachines,
(SELECT COUNT(*) FROM Course) AS NumberOfCourses;
"""
try:
# print("Successfully connected to the database")
# print("Initialization begin")
for statement in DB_SETUP.split(';'):
if statement.strip():
# print("running: ", statement)
cursor.execute(statement)
folder = args[1]
cursor.execute(getCsvDataQuery(f"{folder}/users.csv"))
cursor.execute(getCsvDataQuery(f"{folder}/students.csv"))
cursor.execute(getCsvDataQuery(f"{folder}/admins.csv"))
cursor.execute(getCsvDataQuery(f"{folder}/courses.csv"))
cursor.execute(getCsvDataQuery(f"{folder}/projects.csv"))
cursor.execute(getCsvDataQuery(f"{folder}/machines.csv"))
cursor.execute(getCsvDataQuery(f"{folder}/emails.csv"))
cursor.execute(getCsvDataQuery(f"{folder}/use.csv"))
cursor.execute(getCsvDataQuery(f"{folder}/manage.csv"))
cursor.execute(sql_command)
result = cursor.fetchall()
printRows(result)
db_connection.commit()
# print("Initialization end successfully")
except mysql.connector.Error as error:
print(f"Failed to execute SQL script: {error}")
print('False')
else:
try:
function_selected = globals()[sys.argv[1]]
function_selected(db_connection, cursor, sys.argv)
except mysql.connector.Error as error:
print(f"Failed to execute SQL script: {error}")
print('False')
if db_connection.is_connected():
cursor.close()
db_connection.close()
# print("MySQL connection is closed")
return 0
if __name__ == "__main__":
main()