-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySql_wrapper.py
409 lines (371 loc) · 16.7 KB
/
MySql_wrapper.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
#!usr/bin/env python
from warnings import filterwarnings
import MySQLdb
import farmer_log
from common import *
filterwarnings("ignore", category = MySQLdb.Warning)
class Mysql_wrapper():
def __init__(self, host, user, passwd, dataset):
self.host = host
self.user = user
self.passwd = passwd
self.dataset = dataset
self.connection = \
MySQLdb.Connect(host="%s" % (host,), user="%s" % (user,), passwd="%s" % (passwd,), db="%s" % (dataset,))
def cursor(self):
try:
self.connection.ping()
except Exception, e:
self.connection = MySQLdb.Connect(self.host, self.user, self.passwd, self.dataset)
farmer_log.info("The connection is too long, reconnect.")
return self.connection.cursor()
def __del__(self):
self.connection.close()
def init_database(self):
"""
Perpare for MYSQL database and table
Args:
NULL
Returns:
"""
cursor = self.cursor()
create_accounts_table_cmd = """CREATE TABLE IF NOT EXISTS accounts
(id INT NOT NULL AUTO_INCREMENT,
USER VARCHAR(500) NOT NULL,
PASSWORD VARCHAR(500) NOT NULL,
MAIL VARCHAR(500) NOT NULL,
HAVE_LOGINED BOOL NOT NULL,
PRIMARY KEY (id));"""
create_docker_images_table_cmd = """CREATE TABLE IF NOT EXISTS docker_images
(id INT NOT NULL AUTO_INCREMENT,
REPOSITORY VARCHAR(500) NOT NULL,
TAG VARCHAR(500) NOT NULL,
CUDA_VERSION FLOAT NOT NULL,
CUDA_VERSION_STRING VARCHAR(500) NOT NULL,
CUDNN_VERSION FLOAT NOT NULL,
CUDNN_VERSION_STRING VARCHAR(500) NOT NULL,
TENSORFLOW BOOL NOT NULL,
CAFFE BOOL NOT NULL,
PRIMARY KEY (id));"""
create_request_reports_table_cmd = """CREATE TABLE IF NOT EXISTS request_reports
(id INT NOT NULL AUTO_INCREMENT,
REQUEST_ID VARCHAR(500) NOT NULL,
DOCKER_ID VARCHAR(500) NOT NULL,
GPU_MODEL VARCHAR(500) NOT NULL,
MAIL_ADDRESS VARCHAR(500) NOT NULL,
FRAMEWORK VARCHAR(500) NOT NULL,
TOPOLOGY VARCHAR(500) NOT NULL,
BATCH_SIZE VARCHAR(500) NOT NULL,
ITERATION VARCHAR(500) NOT NULL,
REQUEST_TIME DATETIME NOT NULL DEFAULT NOW(),
PRIMARY KEY (id));"""
create_result_report_table_cmd = """CREATE TABLE IF NOT EXISTS result_reports
(id INT NOT NULL AUTO_INCREMENT,
REQUEST_ID VARCHAR(500) NOT NULL,
DOCKER_ID VARCHAR(500) NOT NULL,
GPU_MODEL VARCHAR(500) NOT NULL,
FRAMEWORK VARCHAR(500) NOT NULL,
TOPOLOGY VARCHAR(500) NOT NULL,
BATCH_SIZE INT NOT NULL,
SOURCE VARCHAR(500) NOT NULL,
ITERATION INT NOT NULL,
SCORE DOUBLE NOT NULL,
IMAGES_PRE_SEC DOUBLE NOT NULL,
PRIMARY KEY (id));"""
#execute initalizing the user account table command
self.create_table(create_accounts_table_cmd, cursor)
# execute initalizing the docker_images command
self.create_table(create_docker_images_table_cmd, cursor)
# execute initalizing the request_reports command
self.create_table(create_request_reports_table_cmd, cursor)
# execute initalizing the result_reports command
self.create_table(create_result_report_table_cmd, cursor)
cursor.close()
def create_account(self, user, password, mail):
cursor = self.cursor()
try:
inserted_sql = '''INSERT INTO accounts
(USER, PASSWORD, MAIL, HAVE_LOGINED)
VALUES("%s", "%s", "%s", False);''' % \
(user, password, mail)
farmer_log.debug(inserted_sql)
cursor.execute(inserted_sql)
self.connection.commit()
output = cursor.fetchall()
farmer_log.info(output)
except Exception as e:
self.connection.rollback()
farmer_log.error("inert_account_info:" + e.message)
finally:
cursor.close()
def exists_mail(self, mail):
result = False
cursor = self.cursor()
try:
select_sql = """select id from accounts where
MAIL = "%s";""" % mail
farmer_log.info(select_sql)
cursor.execute(select_sql)
self.connection.commit()
if 1 == cursor.rowcount:
result = True
elif 0 == cursor.rowcount:
result = False
else:
farmer_log.error("The account have exists more than one.user[%s]" % user)
except Exception as e:
self.connection.rollback()
farmer_log.error("exists_account error [%s]" % e.message)
return result
def exists_user(self, user):
result = False
cursor = self.cursor()
try:
select_sql = """select id from accounts where
USER = "%s";""" % user
farmer_log.info(select_sql)
cursor.execute(select_sql)
self.connection.commit()
if 1 == cursor.rowcount:
result = True
elif 0 == cursor.rowcount:
result = False
else:
farmer_log.error("The account have exists more than one.user[%s]" % user)
except Exception as e:
self.connection.rollback()
farmer_log.error("exists_account error [%s]" % e.message)
return result
def account_login(self, mail, password):
result = (-1, "")
cursor = self.cursor()
try:
login_sql = """select id, MAIL from accounts
where MAIL = "%s" AND PASSWORD = "%s";""" % (mail, password)
farmer_log.info(login_sql)
rowcount = cursor.execute(login_sql)
self.connection.commit()
if rowcount == 1:
output = cursor.fetchone()
result = (int(output[0]), str(output[1]))
except Exception as e:
self.connection.rollback()
farmer_log.error("account_login error [%s]" % e.message)
return result
def account_logout(self, mail, password):
cursor = self.cursor()
try:
login_sql = """UPDATE accounts
SET HAVE_LOGINED = 0
where MAIL = "%s" AND PASSWORD = "%s";""" % (mail, password)
farmer_log.info(login_sql)
cursor.execute(login_sql)
self.connection.commit()
except Exception as e:
self.connection.rollback()
farmer_log.error("account_logout error [%s]" % e.message)
def create_table(self, sql_command, cursor):
farmer_log.info("init table : [%s]" % sql_command)
cursor.execute(sql_command)
farmer_log.info(cursor.fetchall())
self.connection.commit()
def has_image_in_db_by_rep_tag(self, repository, tag):
result = False
cursor = self.cursor()
try:
seach_image = "SELECT repository, tag FROM docker_images WHERE REPOSITORY = '%s' AND TAG = '%s';" % (repository, tag)
farmer_log.debug(seach_image)
cursor.execute(seach_image)
farmer_log.debug(cursor.rowcount)
if (-1 != cursor.rowcount) and (0 != cursor.rowcount):
result = True
output = cursor.fetchall()
farmer_log.info(output)
except Exception as e:
farmer_log.error("has_image_in_db_by_rep_tag:" + e.message)
self.connection.rollback()
finally:
cursor.close()
return result
def inert_item_in_docker_images_table(self, repository, tag, cuda_version, cuda_version_str, cudnn_version, cudnn_version_str, have_tensorflow, have_caffe):
cursor = self.cursor()
try:
inserted_sql = 'INSERT INTO docker_images (REPOSITORY, TAG,\
CUDA_VERSION, CUDA_VERSION_STRING, CUDNN_VERSION,\
CUDNN_VERSION_STRING, TENSORFLOW, CAFFE) \
VALUES("%s", "%s", %f, "%s", %f, "%s", %d, %d);' % \
(repository, tag, cuda_version, cuda_version_str, cudnn_version,\
cudnn_version_str, have_tensorflow, have_caffe)
farmer_log.debug(inserted_sql)
cursor.execute(inserted_sql)
self.connection.commit()
output = cursor.fetchall()
farmer_log.info(output)
except Exception as e:
self.connection.rollback()
farmer_log.error("inert_item_in_docker_images_table:" + e.message)
finally:
cursor.close()
def inert_item_in_request_reports(self, resquest_id, docker_id, gpu_model, \
mail_addr, framework, topology, \
batch_size, iteration):
cursor = self.cursor()
try:
inserted_sql = 'INSERT INTO request_reports\
(REQUEST_ID, DOCKER_ID, GPU_MODEL, MAIL_ADDRESS, FRAMEWORK, TOPOLOGY, BATCH_SIZE, ITERATION) \
VALUES("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s");' % \
(resquest_id, docker_id, gpu_model, mail_addr, framework, topology, batch_size, iteration)
farmer_log.debug(inserted_sql)
cursor.execute(inserted_sql)
self.connection.commit()
output = cursor.fetchall()
farmer_log.info(output)
except Exception as e:
self.connection.rollback()
farmer_log.error("inert_item_in_request_reports:" + e.message)
finally:
cursor.close()
def inert_item_in_result_reports(self, resquest_id, docker_id, gpu_model, \
framework, topology, batch_size, source, \
iteration, score, images_pre_sec):
cursor = self.cursor()
try:
inserted_sql = 'INSERT INTO result_reports\
(REQUEST_ID, DOCKER_ID, GPU_MODEL, FRAMEWORK, TOPOLOGY, BATCH_SIZE, SOURCE, ITERATION, SCORE, IMAGES_PRE_SEC) \
VALUES("%s", "%s", "%s", "%s", "%s", %d, "%s", %d, %f, %f);' % \
(resquest_id, docker_id, gpu_model, framework, topology, batch_size, source, iteration, score, images_pre_sec)
cursor.execute(inserted_sql)
self.connection.commit()
output = cursor.fetchall()
farmer_log.info(output)
except Exception as e:
self.connection.rollback()
farmer_log.error("inert_item_in_result_report:" + e.message)
finally:
cursor.close()
def get_request_reports(self, start_index, count):
header = ["REQUEST_ID", "DOCKER_ID", "GPU_MODEL", "MAIL_ADDRESS", "FRAMEWORK", "TOPOLOGY", "BATCH_SIZE", "ITERATION", "REQUEST_TIME"]
result = DataMediator(header)
cursor = self.cursor()
try:
seach_image = "SELECT %s FROM request_reports order by REQUEST_TIME desc limit %d, %d;" % (", ".join(header), start_index, count)
farmer_log.debug(seach_image)
cursor.execute(seach_image)
self.connection.commit()
rowcount = cursor.rowcount
farmer_log.debug("The result row count is %d" % rowcount)
for i in range(rowcount):
output = cursor.fetchone()
result.append((output[0], \
output[1], \
output[2], \
output[3], \
output[4], \
output[5], \
output[6], \
output[7], \
output[8]))
farmer_log.info(output)
except Exception as e:
farmer_log.error("get_request_reports:" + e.message)
self.connection.rollback()
finally:
cursor.close()
return result
def get_result_by_request_id(self, request_id):
header = ("REQUEST_ID", "DOCKER_ID", "GPU_MODEL", "FRAMEWORK", "TOPOLOGY", "BATCH_SIZE", "SOURCE", "ITERATION", "SCORE", "IMAGES_PRE_SEC", "MAIL_ADDRESS")
result = DataMediator(header)
cursor = self.cursor()
email = ""
try:
search_email = "SELECT MAIL_ADDRESS FROM request_reports WHERE REQUEST_ID = '%s';" % (request_id)
cursor.execute(search_email)
self.connection.commit()
if cursor.rowcount == 1:
email = cursor.fetchone()
else:
farmer_log.error("The request email have wrong number. The rowcount[%d]" % cursor.rowcount)
farmer_log.error(cursor.fetchall())
search_image = "SELECT \
REQUEST_ID,\
DOCKER_ID,\
GPU_MODEL,\
FRAMEWORK,\
TOPOLOGY,\
BATCH_SIZE,\
SOURCE,\
ITERATION,\
SCORE,\
IMAGES_PRE_SEC FROM result_reports WHERE REQUEST_ID = '%s';" % request_id
farmer_log.debug(search_image)
cursor.execute(search_image)
self.connection.commit()
rowcount = cursor.rowcount
farmer_log.debug("The result row count is %d" % rowcount)
for i in range(rowcount):
output = cursor.fetchone()
farmer_log.info(output)
result.append((output[0], \
output[1], \
output[2], \
output[3], \
output[4], \
output[5], \
output[6], \
output[7], \
"%.2f" % output[8], \
"%.2f" % output[9], \
email))
farmer_log.info(output)
except Exception as e:
farmer_log.error("get_result_by_request_id:" + e.message)
self.connection.rollback()
finally:
cursor.close()
return result
def has_image_in_db_by_cuda_cuddn(self, cuda_string, cuddn_string):
result = False
cursor = self.cursor()
try:
seach_image = "SELECT repository, tag FROM docker_images WHERE CUDA_VERSION_STRING = '%s' AND CUDNN_VERSION_STRING = '%s';" % (cuda_string, cuddn_string)
farmer_log.debug(seach_image)
cursor.execute(seach_image)
farmer_log.debug(cursor.rowcount)
if (-1 != cursor.rowcount) and (0 != cursor.rowcount):
result = True
output = cursor.fetchall()
farmer_log.info(output)
except Exception as e:
self.connection.rollback()
farmer_log.error("has_image_in_db_by_cuda_cuddn" + e.message)
finally:
cursor.close()
return result
def get_detailed_info_from_db(self, repository, tag):
row = None
cursor = self.cursor()
try:
select_sql = "SELECT * FROM docker_images WHERE REPOSITORY = '%s' AND TAG = '%s';" % (repository, tag)
farmer_log.info(select_sql)
cursor.execute(select_sql)
numrows = int(cursor.rowcount)
if numrows == 1:
row = cursor.fetchone()
else:
farmer_log.error("the result numrows[%d] which is not 1." % numrows)
output = cursor.fetchall()
farmer_log.info(output)
except Exception as e:
farmer_log.error("get_detailed_info_from_db" + e.message)
finally:
cursor.close()
return row
if __name__ == "__main__":
wrapper = Mysql_wrapper("localhost", "root", "RACQ4F6c")
wrapper.init_database()
wrapper.inert_item_in_docker_images_table("nvidia/cuda", "7.5-cudnn5.1rc-devel-ubuntu14.04-caffe-pcs", 7.5,\
"cuda_7.5.18", 5.0, "cudnn-7.5-linux-x64-v5.0-rc", False, True)
# wrapper.inert_item_in_result_reports(1, 1, "GPU_MODULE", "[email protected]", "framework", "topology", 0, 100)
print wrapper.get_detailed_info_from_db("nvidia/cuda", "7.5-cudnn5.1rc-devel-ubuntu14.04-caffe-pcs")
#print wrapper.has_image_in_db_by_cuda_cuddn("cuda_7.5.18", "cudnn-7.5-linux-x64-v5.0-rc")
##print wrapper.has_image_in_db_by_cuda_cuddn("cuda_7.5.18", "cudnn-7.5-linux-x64-v5.0-rc")