-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_all.py
667 lines (576 loc) · 19.1 KB
/
init_all.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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
import hdf5_getters as GETTERS
import random
import cx_Oracle as oracle
import unicodedata
import sql_command as sql
import hardcode_var as hard
import sys
from datetime import datetime
dataName = 'msd_summary_file.h5'
dataPath = '../' + dataName
USER_NAME = 'yw0'
PASSWD = 'DBdb1234'
HOST = 'oracle.cise.ufl.edu'
PORT = '1521'
SID = 'orcl'
usernames = []
passwd = '12345'
h5 = None
'Options used to automatically generate tuples'
street_opts = []
state_opts = []
city_opts = []
country_opts = []
first_name1_opts = []
first_name2_opts = []
last_name_opts = []
company_opts = []
genre_opts = []
'Records of options selected'
providers = []
genre_ids = []
customers = {}
songs = {}
orders = {}
def strip_accents(str):
return unicodedata.normalize('NFKD', str).encode('ascii','ignore')
def bytes2str(bytes):
return strip_accents(bytes.decode('utf-8')).decode('utf-8')
def init():
global h5, usernames, passwd
global street_opts, state_opts, city_opts, country_opts
global first_name1_opts, first_name2_opts, last_name_opts
global company_opts
global genre_opts
global providers, customers, genre_ids, singers
h5 = GETTERS.open_h5_file_read(dataPath)
usernames = ['user' + str(i).zfill(4) for i in range(hard.NUM_CUSTOMER)]
passwd = '12345'
street_opts = ['4000 SW' + str(i).zfill(3) for i in range(1, 101)]
state_opts = [None, 'FL', 'CA', 'WA', 'AZ', 'NY']
city_opts = [None, 'Gainesville', 'New York', 'Austin', 'Washinton', 'Seattle']
country_opts = [None, 'US', 'CH', 'CN', 'JP', 'MX']
first_name1_opts = ['Mary', 'Amy', 'Ben', 'Jim', 'Ken', 'Sam', 'Kim', 'Bob', 'Alice', 'Tom']
first_name2_opts = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X']
last_name_opts = ['Li', 'Lee', 'Park', 'Snade', 'Snake', 'Wu', 'JK', 'Timblake', 'Mat', 'Green']
company_opts = ['Sony', 'Rock', 'Rolling stone', 'Alee', 'Park']
genre_opts = ['Alternative', 'Anime', 'Blues', 'Classical', 'Country', 'Rap']
def get_conn():
conn = oracle.connect(USER_NAME, PASSWD, oracle.makedsn(HOST, PORT, SID))
return conn
def get_cursor(conn):
return conn.cursor()
def close_all(conn, cursor):
if cursor:
cursor.close()
if conn:
conn.close()
class User:
# username, passwd, street, zip, state, city, country
def __init__(self, username, passwd, street, zip, state, city, country):
self.username = username
self.passwd = passwd
self.street = street
self.zip = zip
self.state = state
self.city = city
self.country = country
def get_username(self):
return self.username
class Provider(User):
def __init__(self, username, passwd, company, street=None, zip=None,
state=None, city=None, country=None):
super().__init__(username, passwd, street, zip, state, city, country)
self.company = company
class Customer(User):
def __init__(self, username, passwd, fname, lname, street=None, zip=None,
state=None, city=None, country=None, gender=None, age=None):
super().__init__(username, passwd, street, zip, state, city, country)
self.fname = fname
self.lname = lname
self.gender = gender
self.age = age
class Song:
def __init__(self, id, title, avg_rate, release_date, duration,
price, provider_name, genre_id, singer_id, download):
self.id = id
self.title = title
self.avg_rate = avg_rate
self.release_date = release_date
self.duration = duration
self.price = price
self.provider_name = provider_name
self.genre_id = genre_id
self.singer_id = singer_id
self.download = download
def get_id(self):
return self.id
def get_title(self):
return self.title
def get_price(self):
return self.price
class Order:
def __init__(self, id, time, price):
self.id = id
self.time = time
self.price = price
def get_price(self):
return self.price
def get_time(self):
return self.time
def show_all_tables():
conn = get_conn()
cursor = conn.cursor()
cursor.execute("select * from user_tables")
row = cursor.fetchone()
while row:
print(row)
row = cursor.fetchone()
def create_customer_table():
print('Creating CUSTOMER table')
conn = get_conn()
cursor = get_cursor(conn)
try:
cursor.execute(sql.CREATE_CUSTOMER_TABLE)
return 0
except Exception as e:
print(e)
return -1
finally:
conn.commit()
close_all(conn, cursor)
def create_provider_table():
print('Creating PROVIDER table')
conn = get_conn()
cursor = get_cursor(conn)
try:
cursor.execute(sql.CREATE_PROVIDER_TABLE)
return 0
except Exception as e:
print(e, 'create provider table error')
return -1
finally:
conn.commit()
close_all(conn, cursor)
def create_genre_table():
print('Creating GENRE table')
conn = get_conn()
cursor = get_cursor(conn)
try:
cursor.execute(sql.CREATE_GENRE_TABLE)
return 0
except Exception as e:
print(e, 'create genre table error')
return -1
finally:
conn.commit()
close_all(conn, cursor)
def create_song_table():
print('Creating SONG table')
conn = get_conn()
cursor = get_cursor(conn)
try:
cursor.execute(sql.CREATE_SONG_TABLE)
return 0
except Exception as e:
print(e, 'create song table error')
return -1
finally:
conn.commit()
close_all(conn, cursor)
def create_singer_table():
print('Creating SINGER table')
conn = get_conn()
cursor = get_cursor(conn)
try:
cursor.execute(sql.CREATE_SINGER_TABLE)
return 0
except Exception as e:
print(e, 'create singer table error')
return -1
finally:
conn.commit()
close_all(conn, cursor)
def create_favorite_genre_table():
print('Creating FAVORITE_GENRE table')
conn = get_conn()
cursor = get_cursor(conn)
try:
cursor.execute(sql.CREATE_FAVORITE_GENRE_TABLE)
return 0
except Exception as e:
print(e, 'create favorite_genre table error')
return -1
finally:
conn.commit()
close_all(conn, cursor)
def create_favorite_singer_table():
print('Creating FAVORITE_SINGER table')
conn = get_conn()
cursor = get_cursor(conn)
try:
cursor.execute(sql.CREATE_FAVORITE_SINGER_TABLE)
return 0
except Exception as e:
print(e, 'create favorite_singer table error')
return -1
finally:
conn.commit()
close_all(conn, cursor)
def create_order_table():
print('Creating ORDER table')
conn = get_conn()
cursor = get_cursor(conn)
try:
cursor.execute(sql.CREATE_ORDER_TABLE)
return 0
except Exception as e:
print(e, 'create order table error')
return -1
finally:
conn.commit()
close_all(conn, cursor)
def create_rate_table():
print('Creating RATE table')
conn = get_conn()
cursor = get_cursor(conn)
try:
cursor.execute(sql.CREATE_RATE_TABLE)
return 0
except Exception as e:
print(e, 'create rate table error')
return -1
finally:
conn.commit()
close_all(conn, cursor)
def insert_customer():
print('Inserting customer tuples')
conn = get_conn()
cursor = get_cursor(conn)
try:
for i in range(hard.NUM_CUSTOMER):
__username = usernames[i]
__street, __city, __state, __zip, __country = InfoGenerator.gen_address()
__fname, __lname = InfoGenerator.gen_name()
__gender = InfoGenerator.gen_gender()
__age = InfoGenerator.gen_age()
cursor.execute(sql.INSERT_CUSTOMER,
username = __username, passwd = passwd,
street = __street, city = __city, state = __state, zip = __zip, country = __country,
fname = __fname, lname = __lname, gender = __gender, age = __age)
customers[i] = Customer(__username, passwd,
__street, __city, __state, __zip, __country,
__fname, __lname, __gender, __age)
return 0
except Exception as e:
print(e, 'insert customer tuple error')
return -1
finally:
conn.commit()
close_all(conn, cursor)
def insert_provider():
print('Inserting provider tuples')
conn = get_conn()
cursor = get_cursor(conn)
try:
for i in range(hard.NUM_PROVIDER):
__username = usernames[i]
__street, __city, __state, __zip, __country = InfoGenerator.gen_address()
__company = InfoGenerator.gen_company(i)
cursor.execute(sql.INSERT_PROVIDER,
username = __username, passwd = passwd,
street = __street, city = __city, state = __state, zip = __zip, country = __country,
company = __company)
providers.append(__company)
return 0
except Exception as e:
print(e)
return -1
finally:
conn.commit()
close_all(conn, cursor)
def insert_genre():
print('Inserting genre tuples')
conn = get_conn()
cursor = get_cursor(conn)
__id = None
__name = None
try:
for i in range(len(genre_opts)):
__id = i
__name = InfoGenerator.gen_genre_name(i)
cursor.execute(sql.INSERT_GENRE,
id = __id, name = __name)
genre_ids.append(__id)
return 0
except Exception as e:
print(e, 'insert genre tuple error')
print('id:',__id, 'name:',__name)
return -1
finally:
conn.commit()
close_all(conn, cursor)
def insert_song():
print('Inserting song tuples')
conn = get_conn()
cursor = get_cursor(conn)
__id = None
__title = None
__avg_rate = None
__release_date = None
__duration = None
__price = None
__provider_name = None
__genre_id = None
__singer_id = None
__download = None
try:
for i in range(hard.NUM_SONGS):
__id = bytes2str(GETTERS.get_song_id(h5, i))
__title = bytes2str(GETTERS.get_title(h5, i))
__avg_rate = 0.0
# use int() to transform the numpy.int32 to int which is supported by Oracle
__release_date = int(GETTERS.get_year(h5, i))
if __release_date == 0:
__release_date = None
__duration = int(GETTERS.get_duration(h5, i))
__price = InfoGenerator.gen_price()
__provider_name = InfoGenerator.get_provider_name()
__genre_id = InfoGenerator.get_genre_id()
__singer_id = bytes2str(GETTERS.get_artist_id(h5, i))
__download = 0
cursor.execute(sql.INSERT_SONG,
id = __id, title = __title, avg_rate = __avg_rate, release_date = __release_date,
duration = __duration, price = __price, provider_name = __provider_name,
genre_id = __genre_id, singer_id = __singer_id, download = __download)
songs[i] = Song(__id, __title, __avg_rate, __release_date, __duration, __price,
__provider_name, __genre_id, __singer_id, __download)
return 0
except Exception as e:
print(e)
print('i:', i, '\nid:',__id, '\ntitle:', __title, '\navg_rate:', __avg_rate,
'\nrelease_date:', __release_date, '\nduration', __duration, '\nprice', __price,
'provider_name:',__provider_name, '\ngenre_id:', __genre_id, '\nsinger_id', __singer_id,
'download:',__download)
return -1
finally:
conn.commit()
close_all(conn, cursor)
def insert_singer():
print('Inserting singer tuples')
conn = get_conn()
cursor = get_cursor(conn)
unique = set()
__name = None
__singer_id = None
__initial_hotness = None
try:
for i in range(hard.NUM_SINGERS):
__singer_id = bytes2str(GETTERS.get_artist_id(h5, i))
if __singer_id not in unique:
unique.add(__singer_id)
__name = bytes2str(GETTERS.get_artist_name(h5, i))
__initial_hotness = 0
cursor.execute(sql.INSERT_SINGER,
id = __singer_id, name = __name,
hotness = __initial_hotness)
return 0
except Exception as e:
print(e, 'insert singer tuple error')
print('name:',__name, 'singer_id:',__singer_id, 'hotness:',__initial_hotness)
return -1
finally:
conn.commit()
close_all(conn, cursor)
def insert_favorite_genre():
print('Inserting favorite_genre tuples')
conn = get_conn()
cursor = get_cursor(conn)
try:
for customer in customers.values():
rows = 3
unique = set()
for i in range(rows):
__genre_id = genre_ids[InfoGenerator.gen_int(0, len(genre_ids) - 1)]
if __genre_id not in unique:
unique.add(__genre_id)
cursor.execute(sql.INSERT_FAVORITE_GENRE,
username = customer.get_username(), genre_id = __genre_id)
return 0
except Exception as e:
print(e, 'insert favorite_genre tuple error')
return -1
finally:
conn.commit()
close_all(conn, cursor)
def insert_favorite_singer():
print('Inserting favorite_singer tuples')
conn = get_conn()
cursor = get_cursor(conn)
__singer_id = None
try:
for customer in customers.values():
rows = 3
unique = set()
for i in range(rows):
index = InfoGenerator.gen_int(0, len(songs) - 1)
__singer_id = bytes2str(GETTERS.get_artist_id(h5, index))
if __singer_id not in unique:
unique.add(__singer_id)
cursor.execute(sql.INSERT_FAVORITE_SINGER,
username = customer.get_username(), singer_id = __singer_id)
return 0
except Exception as e:
print(e, 'insert favorite_singer tuple error')
print('singer_id:',__singer_id)
return -1
finally:
conn.commit()
close_all(conn, cursor)
def insert_order():
print('Inserting order tuples')
conn = get_conn()
cursor = get_cursor(conn)
__customer = None
__order_id = None
__song_id = None
__time = None
__price = None
__paid = None
unique = set()
try:
for i in range(hard.NUM_ORDER):
index_song = InfoGenerator.gen_int(0, len(songs) - 1)
__customer = customers[InfoGenerator.gen_int(0, len(customers) - 1)].get_username()
__song_id = songs[index_song].get_id()
__order_id = __customer + __song_id
__price = songs[index_song].get_price()
__time = datetime.now()
__paid = 0
if __customer + __song_id not in unique:
unique.add(__customer + __song_id)
cursor.execute(sql.INSERT_ORDER,
customer = __customer, order_id = __order_id, song_id = __song_id,
time = __time, price = __price, paid = __paid)
"""generate a rate for each order"""
__score = InfoGenerator.gen_int(1, 5)
cursor.execute(sql.INSERT_RATE,
score = __score, customer = __customer, song_id = __song_id)
return 0
except Exception as e:
print(e, 'insert order tuple error')
print('customer:',__customer, 'order_id:',__order_id, 'song_id:',__song_id,
'time:',__time, 'price:',__price, 'paid:',[False,True][__paid])
return -1
finally:
conn.commit()
close_all(conn, cursor)
# def insert_rate():
# print('Inserting rate tuples')
# conn = get_conn()
# cursor = get_cursor(conn)
#
# __customer = None
# __song_id = None
# __score = None
#
# try:
# for customer in customers.values():
# for i in range(hard.NUM_RATE):
# return 0
# except Exception as e:
# print(e, 'insert rate tuple error')
# print('customer:',__customer, 'song_id:',__song_id, 'score:',__score)
# return -1
# finally:
# conn.commit()
# close_all(conn, cursor)
class InfoGenerator:
@staticmethod
def gen_int(start, end):
return random.randint(start, end)
# 1.23
@staticmethod
def gen_float(start, end):
return random.uniform(start, end)
@staticmethod
def gen_genre_name(i):
return genre_opts[i % len(genre_opts)]
@staticmethod
def gen_company(i):
return company_opts[i % len(company_opts)]
@staticmethod
def gen_address():
street = street_opts[InfoGenerator.gen_int(0, 99)]
zip = str(InfoGenerator.gen_int(0, 99999)).zfill(5)
state = state_opts[InfoGenerator.gen_int(0, 5)]
city = city_opts[InfoGenerator.gen_int(0, 5)]
country = country_opts[InfoGenerator.gen_int(0, 5)]
return street, city, state, zip, country
@staticmethod
def gen_age():
return InfoGenerator.gen_int(5, 70)
@staticmethod
def gen_gender():
return ['M', 'N', 'F'][InfoGenerator.gen_int(0, 2)]
@staticmethod
def gen_name():
fname = first_name1_opts[InfoGenerator.gen_int(0, 9)] + ' ' + first_name2_opts[InfoGenerator.gen_int(0, 9)]
lname = last_name_opts[InfoGenerator.gen_int(0, 9)]
return fname, lname
@staticmethod
def gen_price():
return float("%.2f" % InfoGenerator.gen_float(0.0, 9.99))
@staticmethod
def get_provider_name():
return providers[InfoGenerator.gen_int(0, len(providers) - 1)]
@staticmethod
def get_customer_name():
return customers[InfoGenerator.gen_int(0, len(customers) - 1)]
@staticmethod
def get_singer_id():
return singers[InfoGenerator.gen_int(0, len(singers) - 1)]
@staticmethod
def get_genre_id():
return genre_ids[InfoGenerator.gen_int(0, len(genre_ids) - 1)]
def create_tables():
if (create_customer_table()
or create_provider_table()
or create_genre_table()
or create_singer_table()
or create_song_table()
or create_favorite_singer_table()
or create_favorite_genre_table()
or create_order_table()
or create_rate_table()):
sys.exit(0)
def insert_tuples():
if (insert_customer()
or insert_provider()
or insert_genre()
or insert_singer()
or insert_song()
or insert_favorite_genre()
or insert_favorite_singer()
or insert_order()):
# or insert_rate()):
sys.exit(0)
def create_triggers():
print('Creating triggers')
conn = get_conn()
cursor = get_cursor(conn)
try:
cursor.execute(sql.TRIGGER_DOWNLOAD)
cursor.execute(sql.TRIGGER_AVG_RATE)
return 0
except Exception as e:
print(e, 'create trigger error')
return -1
finally:
conn.commit()
close_all(conn, cursor)
if __name__ == '__main__':
init()
create_tables()
create_triggers()
insert_tuples()