-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
109 lines (96 loc) · 2.83 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
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
from connector import Connector
from inserter import read_all_users
from utils import linebreak
from time import time
import argparse
import queries
def execute_script(db: Connector, filename: str):
file = open(filename, "r")
# Read lines and strip line breaks
lines = [x.strip().replace("\n", "") for x in file.read().split(";")]
# Remove empty lines
sql = [x + ";" for x in lines if x]
file.close()
# Execute every command from the input file
for command in sql:
# This will skip and report errors
try:
db.cursor.execute(command)
db.connection.commit()
except Exception as e:
print("Command skipped: ", command)
print("Error:", e)
def main(args):
linebreak()
db = Connector()
start = time()
if args.init:
linebreak()
print("Dropping and then creating all tables...")
execute_script(db, "create_tables.sql")
print("Successfully created tables")
if args.index:
linebreak()
print("Creating all relevant indexes...")
execute_script(db, "create_indexes.sql")
print("Successfully created indexes")
if args.fill:
linebreak()
print(f"Inserting the first {args.fill} users from the dataset...")
read_all_users(db, args.fill)
if args.query is not None:
linebreak()
queries.run_query(db, args.query)
if args.queries is not None:
first = args.queries[0]
last = args.queries[1]
print(f"Running queries {first} to {last}")
for i in range(first, last + 1):
linebreak()
queries.run_query(db, i)
linebreak()
end = time()
elapsed = round(end - start, 2)
print(f"Total time elapsed: {elapsed} seconds")
db.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"--init",
dest="init",
action="store_true",
help="Initialize all relevant tables.",
)
parser.add_argument(
"--index",
dest="index",
action="store_true",
help="Create all relevant indexes.",
)
parser.add_argument(
"--fill",
choices=range(1, 182),
metavar="[1-182]",
type=int,
help="Fill the database with sanitized data for up to the specified number of users.",
)
parser.add_argument(
"-q",
"--query",
choices=range(0, 13),
metavar="[0-12]",
type=int,
help="Execute a specific query.",
)
parser.add_argument(
"--queries",
nargs=2,
choices=range(0, 13),
metavar="[0-12]",
type=int,
help="Execute all queries in the specified range.",
)
args = parser.parse_args()
main(args)