-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.py
621 lines (516 loc) · 15.8 KB
/
db.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
import collections
import json
import os
import random
import sqlite3
import uuid
from datetime import datetime
HOME_DIR = os.environ.get("HOME_DIR")
if HOME_DIR == None:
raise Exception("HOME_DIR environment variable not set")
WORDS_FILE = os.path.join(HOME_DIR, "rave-training/words.txt")
DB_PATH = os.path.join(HOME_DIR, "houseparty.db")
TIME_FMT = "%Y-%m-%d"
TIME_FMT_SYSTEM = "%Y%m%d%H%M"
"""
The database schema is as follows:
- WorkOrder Table
- WorkOrderId
- WorkOrderStatus
- TrainingJobId
- UserId
- DatasetId
- PriceId
- StripeTransactionId
- TrainingJob Table
- TrainingJobId
- TrainingJobName
- TrainingJobStatus
- OutputS3LocationId
- DatasetId
- InstanceId
- PriceId
- S3Location Table
- S3LocationId
- BucketName
- ObjectName
- User Table
- UserId
- UserName
- UserEmailAddress
- Dataset Table
- DatasetId
- DatasetS3LocationId
- Instance Table
- InstanceId
- LambdaInstanceId
- Price Table
- PriceId
- PriceMetadata
- WorkOrderStatus is an enum with the following possible values:
- CREATED
- PAID
- FAILED
- CANCELLED
- COMPLETED
- TrainingJobStatus is an enum with the following possible values:
- PENDING
- RUNNING
- COMPLETED
- FAILED
- CANCELLED
"""
WORK_ORDER_STATUS_CREATED = "CREATED"
WORK_ORDER_STATUS_PAID = "PAID"
WORK_ORDER_STATUS_FAILED = "FAILED"
WORK_ORDER_STATUS_CANCELLED = "CANCELLED"
WORK_ORDER_STATUS_COMPLETED = "COMPLETED"
WORK_ORDER_STATUS_LIST = [
WORK_ORDER_STATUS_CREATED,
WORK_ORDER_STATUS_PAID,
WORK_ORDER_STATUS_FAILED,
WORK_ORDER_STATUS_CANCELLED,
WORK_ORDER_STATUS_COMPLETED,
]
TRAINING_JOB_STATUS_PENDING = "PENDING"
TRAINING_JOB_STATUS_RUNNING = "RUNNING"
TRAINING_JOB_STATUS_COMPLETED = "COMPLETED"
TRAINING_JOB_STATUS_FAILED = "FAILED"
TRAINING_JOB_STATUS_CANCELLED = "CANCELLED"
TRAINING_JOB_STATUS_LIST = [
TRAINING_JOB_STATUS_PENDING,
TRAINING_JOB_STATUS_RUNNING,
TRAINING_JOB_STATUS_COMPLETED,
TRAINING_JOB_STATUS_FAILED,
TRAINING_JOB_STATUS_CANCELLED,
]
def create_db():
with sqlite3.connect(DB_PATH) as db_connection:
cursor = db_connection.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS WorkOrder (
CreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
WorkOrderId TEXT PRIMARY KEY,
WorkOrderStatus TEXT,
TrainingJobId TEXT,
UserId TEXT,
DatasetId TEXT,
PriceId TEXT,
StripeTransactionId TEXT
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS TrainingJob (
CreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
TrainingJobId TEXT PRIMARY KEY,
TrainingJobName TEXT,
TrainingJobStatus TEXT,
TrainingJobSettings TEXT,
OutputS3LocationId TEXT,
DatasetId TEXT,
InstanceId TEXT
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS S3Location (
CreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
S3LocationId TEXT PRIMARY KEY,
BucketName TEXT,
ObjectName TEXT
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS User (
CreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
UserId TEXT PRIMARY KEY,
UserEmailAddress TEXT
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS Dataset (
CreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
DatasetId TEXT PRIMARY KEY,
DatasetS3LocationId TEXT
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS Instance (
CreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
InstanceId TEXT PRIMARY KEY,
LambdaInstanceId TEXT
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS Price (
CreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
PriceId TEXT PRIMARY KEY,
PriceMetadata TEXT
)
"""
)
db_connection.commit()
def query(q):
db_connection = sqlite3.connect(DB_PATH)
cursor = db_connection.cursor()
result = cursor.execute(q).fetchall()
columns = [description[0] for description in cursor.description]
db_connection.close()
result_dicts = []
for row in result:
row_dict = {}
for i in range(len(row)):
row_dict[columns[i]] = row[i]
result_dicts.append(row_dict)
return result_dicts
def update(query, params):
db_connection = sqlite3.connect(DB_PATH)
cursor = db_connection.cursor()
cursor.execute(query, params)
db_connection.commit()
db_connection.close()
def list_work_orders():
return query("""select * from WorkOrder;""")
def get_unique_work_order_id():
work_orders = list_work_orders()
work_order_ids = [work_order["WorkOrderId"] for work_order in work_orders]
work_order_id = str(uuid.uuid4())
while work_order_id in work_order_ids:
work_order_id = str(uuid.uuid4())
return work_order_id
def insert_work_order(work_order_status, training_job_id, dataset_id, price_id):
work_order_id = get_unique_work_order_id()
update(
"""
INSERT INTO WorkOrder (
WorkOrderId,
WorkOrderStatus,
TrainingJobId,
DatasetId,
PriceId
)
VALUES (?, ?, ?, ?, ?);
""",
(work_order_id, work_order_status, training_job_id, dataset_id, price_id),
)
return work_order_id
def create_work_order(training_job_id, dataset_id, price_id):
work_order_status = WORK_ORDER_STATUS_CREATED
worker_order_id = insert_work_order(
work_order_status, training_job_id, dataset_id, price_id
)
return worker_order_id
def get_work_order(work_order_id):
work_orders = query(
f"""select * from WorkOrder where WorkOrderId = "{work_order_id}";"""
)
if len(work_orders) == 0:
return None
else:
return work_orders[0]
def get_work_order_by_stripe_transaction(stripe_transaction_id):
work_orders = query(
f"""select * from WorkOrder where StripeTransactionId = "{stripe_transaction_id}";"""
)
if len(work_orders) == 0:
return None
else:
return work_orders[0]
def delete_work_order(work_order_id):
update("""delete from WorkOrder where WorkOrderId = ?;""", (work_order_id,))
return work_order_id
def delete_work_order_full(work_order_id):
work_order = get_work_order(work_order_id)
if work_order == None:
return None
else:
training_job_id = work_order["TrainingJobId"]
dataset_id = work_order["DatasetId"]
price_id = work_order["PriceId"]
dataset = get_dataset(dataset_id)
dataset_s3_location_id = dataset["DatasetS3LocationId"]
training_job = get_training_job(training_job_id)
output_s3_location_id = training_job["OutputS3LocationId"]
delete_s3_location(dataset_s3_location_id)
delete_s3_location(output_s3_location_id)
delete_training_job(training_job_id)
delete_dataset(dataset_id)
delete_price(price_id)
delete_work_order(work_order_id)
return work_order_id
def get_random_word():
with open(WORDS_FILE) as f:
words = f.readlines()
return random.choice(words).strip()
def get_random_training_job_name():
words = [get_random_word() for i in range(4)]
return "-".join(words)
def get_unique_training_job_name():
training_jobs = list_training_jobs()
training_job_names = [
training_job["TrainingJobName"] for training_job in training_jobs
]
training_job_name = get_random_training_job_name()
while training_job_name in training_job_names:
training_job_name = get_random_training_job_name()
return training_job_name
def get_unique_training_job_id():
training_jobs = list_training_jobs()
training_job_ids = [training_job["TrainingJobId"] for training_job in training_jobs]
training_job_id = str(uuid.uuid4())
while training_job_id in training_job_ids:
training_job_id = str(uuid.uuid4())
return training_job_id
def create_training_job(dataset_id, training_job_settings):
training_job_id = get_unique_training_job_id()
training_job_name = get_unique_training_job_name()
training_job_status = TRAINING_JOB_STATUS_PENDING
training_job_settings = json.dumps(training_job_settings)
update(
"""
INSERT INTO TrainingJob (
TrainingJobId,
TrainingJobName,
TrainingJobStatus,
TrainingJobSettings,
DatasetId
)
VALUES (?, ?, ?, ?, ?);
""",
(
training_job_id,
training_job_name,
training_job_status,
json.dumps(training_job_settings),
dataset_id,
),
)
return training_job_id
def get_training_job(training_job_id):
training_jobs = query(
f"""select * from TrainingJob where TrainingJobId = "{training_job_id}";"""
)
if len(training_jobs) == 0:
return None
else:
return training_jobs[0]
def update_training_job_status(training_job_id, training_job_status):
if training_job_status not in TRAINING_JOB_STATUS_LIST:
raise Exception(f"Invalid training_job_status: {training_job_status}")
update(
"""
UPDATE TrainingJob
SET TrainingJobStatus = ?
WHERE TrainingJobId = ?;
""",
(training_job_status, training_job_id),
)
return training_job_id
def delete_training_job(training_job_id):
update(
"""
DELETE FROM TrainingJob
WHERE TrainingJobId = ?;
""",
(training_job_id,),
)
return training_job_id
def list_training_jobs():
return query("""select * from TrainingJob;""")
def list_training_jobs_by_status(training_job_status):
if training_job_status not in TRAINING_JOB_STATUS_LIST:
raise Exception(f"Invalid training_job_status: {training_job_status}")
return query(
f"""select * from TrainingJob where TrainingJobStatus = "{training_job_status}";"""
)
def get_unique_s3_location_id():
s3_locations = list_s3_locations()
s3_location_ids = [s3_location["S3LocationId"] for s3_location in s3_locations]
s3_location_id = str(uuid.uuid4())
while s3_location_id in s3_location_ids:
s3_location_id = str(uuid.uuid4())
return s3_location_id
def insert_s3_location(bucket_name, object_name):
s3_location_id = get_unique_s3_location_id()
update(
"""
INSERT INTO S3Location (
S3LocationId,
BucketName,
ObjectName
)
VALUES (
?,
?,
?
);
""",
(s3_location_id, bucket_name, object_name),
)
return s3_location_id
def get_s3_location(s3_location_id):
return query(
f"""select * from S3Location where S3LocationId = "{s3_location_id}";"""
)
def delete_s3_location(s3_location_id):
update("""delete from S3Location where S3LocationId = ?;""", (s3_location_id,))
return s3_location_id
def list_s3_locations():
return query("""select * from S3Location;""")
def get_unique_user_id():
users = list_users()
user_ids = [user["UserId"] for user in users]
user_id = str(uuid.uuid4())
while user_id in user_ids:
user_id = str(uuid.uuid4())
return user_id
def insert_user(user_email_address):
# first check if user already exists
user = query(
"""select * from User where UserEmailAddress = ?;""", (user_email_address,)
)
if len(user) > 0:
return user[0]["UserId"]
# if not, create a new user
user_id = get_unique_user_id()
update(
"""
INSERT INTO User (
UserId,
UserEmailAddress
)
VALUES (
?,
?
);
""",
(user_id, user_email_address),
)
return user_id
def get_user(user_id):
return query(f"""select * from User where UserId = "{user_id}";""")
def delete_user(user_id):
update("""delete from User where UserId = ?;""", (user_id,))
return user_id
def list_users():
return query("""select * from User;""")
def get_unique_dataset_id():
datasets = list_datasets()
dataset_ids = [dataset["DatasetId"] for dataset in datasets]
dataset_id = str(uuid.uuid4())
while dataset_id in dataset_ids:
dataset_id = str(uuid.uuid4())
return dataset_id
def insert_dataset(dataset_s3_location_id):
dataset_id = get_unique_dataset_id()
update(
"""
INSERT INTO Dataset (
DatasetId,
DatasetS3LocationId
)
VALUES (
?,
?
);
""",
(dataset_id, dataset_s3_location_id),
)
return dataset_id
def get_dataset(dataset_id):
return query(f"""select * from Dataset where DatasetId = "{dataset_id}";""")
def delete_dataset(dataset_id):
update("""delete from Dataset where DatasetId = ?;""", (dataset_id,))
return dataset_id
def list_datasets():
return query("""select * from Dataset;""")
def get_unique_instance_id():
instances = list_instances()
instance_ids = [instance["InstanceId"] for instance in instances]
instance_id = str(uuid.uuid4())
while instance_id in instance_ids:
instance_id = str(uuid.uuid4())
return instance_id
def insert_instance(lambda_instance_id):
instance_id = get_unique_instance_id()
update(
"""
INSERT INTO Instance (
InstanceId,
LambdaInstanceId
)
VALUES (
?,
?
);
""",
(instance_id, lambda_instance_id),
)
return instance_id
def get_instance(instance_id):
return query(f"""select * from Instance where InstanceId = "{instance_id}";""")
def delete_instance(instance_id):
update("""delete from Instance where InstanceId = ?;""", (instance_id,))
return instance_id
def list_instances():
return query("""select * from Instance;""")
def get_unique_price_id():
prices = list_prices()
price_ids = [price["PriceId"] for price in prices]
price_id = str(uuid.uuid4())
while price_id in price_ids:
price_id = str(uuid.uuid4())
return price_id
def insert_price(price_metadata):
price_id = get_unique_price_id()
update(
"""
INSERT INTO Price (
PriceId,
PriceMetadata
)
VALUES (
?,
?
);
""",
(price_id, json.dumps(price_metadata)),
)
return price_id
def get_price(price_id):
return query(f"""select * from Price where PriceId = "{price_id}";""")
def delete_price(price_id):
update("""delete from Price where PriceId = ?;""", (price_id,))
return price_id
def list_prices():
return query("""select * from Price;""")
def row_to_dict(row):
pass
def update_dict_recursive(d, u):
for k, v in u.items():
if isinstance(v, collections.abc.Mapping) and k in v:
d[k] = update_dict_recursive(d.get(k, dict()), v)
else:
d[k] = v
return d
def parse_string_to_json_until_list_or_dict(json_data):
if json_data == "" or json_data == None:
return None
while not isinstance(json_data, list) and not isinstance(json_data, dict):
if json_data == "" or json_data == None:
return None
else:
json_data = json.loads(json_data)
return json_data