-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapollo.py
More file actions
315 lines (247 loc) · 10.7 KB
/
apollo.py
File metadata and controls
315 lines (247 loc) · 10.7 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
import os, sys, string
# import simWS
import MySQLdb
import MySQLdb.cursors
from logger import Log
# ## STB To Do Make this variable from the SimWS file
# from SimulatorService_v2_0_2_types import *
_runTable = "run"
_populationAxisTable = "population_axis"
_simulatedPopulationTable = "simulated_population"
_simulatedPopulationAxisValueTable = "simulated_population_axis_value"
_timeSeriesTable = "time_series"
_softwareIdentificationTable = "software_identification"
class ApolloDB:
def __init__(self, host_="warhol-fred.psc.edu",
user_="apolloext", dbname_="apollo201", password_=None, logger_=None):
self._host = host_
self._user = user_
self._dbname = dbname_
self._password = password_
self._conn = None
self._DictCursor = None
self._RegularCursor = None
self.populationAxis = None
if logger_ is None:
self.logger = Log("./db.test.log")
self.logger.start()
else:
self.logger = logger_
self.stateToPopulationDict = {'S':'susceptible', 'E':'exposed', 'I':'infectious',
'R':'recovered', 'C':'newly exposed', 'V':'received vaccine control measure',
'Av':'received antiviral control measure', 'ScCl':'school that is closed'}
self.stateToDataFileDict = {'S':'susceptible.txt', 'E':'exposed.txt', 'I':'infectious.txt',
'R':'recovered.txt', 'C':'newly_exposed.txt', 'V':'vacc_administered.txt',
'Av':'av_administered.txt'}
def connect(self):
if self._conn is not None:
print "Connection to Apollo Database has already been established"
return
try:
if self._password is None:
self._conn = MySQLdb.connect(host=self._host, user=self._user, db=self._dbname)
else:
self._conn = MySQLdb.connect(host=self._host,
user=self._user,
passwd=self._password,
db=self._dbname)
self._conn.autocommit(True)
self.logger.update('DB_CONNECT_SUCCESS')
except MySQLdb.Error, e:
print "Problem connecting to Apollo database: %d %s" % (e.args[0], e.args[1])
self.logger.update('DB_CONNECT_FAILED')
raise e
self._cursor = self._conn.cursor(MySQLdb.cursors.DictCursor)
self.populationAxis = self._populationAxis()
def close(self):
if self._conn is None:
raise RuntimeError("A connection to the Apollo database"\
"has not been made before close is made")
self._conn.close()
def query(self, SQLString):
if self._conn is None:
raise RuntimeError("A connection to the Apollo database "\
"has not been made before queury is made")
try:
self._cursor.execute(SQLString)
except Exception as e:
self.logger.update('DB_QUERY_FAILED', message="%s,%s" % (str(e), SQLString))
raise
rows = self._cursor.fetchall()
return rows
def insertID(self):
return self._conn.insert_id()
def _populationAxis(self):
populationAxisDict = {}
SQLStatement = "SELECT * from %s" % _populationAxisTable
result = self.query(SQLStatement)
for row in result:
populationAxisDict[row["label"]] = row["id"]
return populationAxisDict
def getRuns(self):
runList = []
SQLStatement = "SELECT * from %s" % _runTable
result = self.query(SQLStatement)
for row in result:
runList.append(row['label'])
return runList
def getTranslatorServiceKey(self):
SQLStatement = 'SELECT * from %s where service_type = "translator"' % (_softwareIdentificationTable)
result = self.query(SQLStatement)
row = result[0]
return row['id']
def getSoftwareIdentificationId(self, name_="SEIR", version_="3.0"):
SQLStatement = 'SELECT id from %s where name = "%s" and version = "%s"' % (_softwareIdentificationTable, name_, version_)
result = self.query(SQLStatement)
if len(result) == 0:
self.logger.update('DB_QUERY_NO_RESULTS', message=SQLStatement)
return -1
return result[0]['id']
def getSoftwareIdentificationForRunId(self, runId):
SQLStatement = 'SELECT t1.name,t1.developer,t1.version from software_identification t1, '\
+ 'run t2 where t1.id = t2.software_id and t2.id = %s' % str(runId)
result = self.query(SQLStatement)
if len(result) == 0:
self.logger.update('DB_QUERY_NO_RESULTS', message=SQLStatement)
return ('unknown', 'Could not find runId: %s in the ApolloDB')
if len(result) > 1:
self.logger.update('DB_QUERY_MORE_THAN_ONE', messsage=SQLStatement)
raise
return (result[0]['name'], result[0]['developer'], result[0]['version'])
def getSimulatorServiceKey(self, developer, name, version):
SQLStatement = 'SELECT * from %s where developer = "%s" and name = "%s" and version = "%s"'\
% (_softwareIdentificationTable, developer, name, version)
result = self.query(SQLStatement)
if len(result) == 0:
self.logger.update('DB_QUERY_NO_RESULTS', message=SQLStatement)
raise
return result[0]['id']
def getRunDataDescriptionId(self, label_, source_software_=3, destination_software_=4, format_="TEXT", type_="SIMULATOR_LOG_FILE"):
try:
SQLStatement = 'SELECT v.run_data_description_id FROM run_data_description_view v WHERE '\
+ 'v.format = "%s" AND v.label = "%s" and v.type = "%s" and v.source_software = %s ' % (format_, label_, type_, str(source_software_))\
+ 'and v.destination_software = %s' % (str(destination_software_))
result = self.query(SQLStatement)
if len(result) > 1:
raise RuntimeError("getRunDataDescription returned more than one entry " + \
"for (%s,%d,%d,%s,%s)" % (label_, source_software_, destination_software_, format_, type_))
return int(result[0]["run_data_description_id"])
except Exception as e:
print str(e)
raise e
def getRunDataContentFromRunIdAndLabel(self, simulationRunId_, label_, source_software_=3,
destination_software_=4, format_="TEXT", type_="SIMULATOR_LOG_FILE"):
try:
runDataDescriptionId = self.getRunDataDescriptionId(label_, source_software_, destination_software_, format_, type_)
# ## First get the content id
SQLStatement = 'SELECT content_id from run_data where run_Id = "%s" and description_Id = "%s"' % (simulationRunId_, runDataDescriptionId)
result = self.query(SQLStatement)
if len(result) > 1:
raise RuntimeError('getRunDataContentFromRunIdAndLabel returned more than one entry '\
+ 'for the content_id for (%s,%s)' % (simulationRunId_, label_))
if len(result) == 0:
return ""
contentId = result[0]['content_id']
# ## Now get the file content
SQLStatement = 'SELECT text_content from run_data_content where id = "%s"' % str(contentId)
result = self.query(SQLStatement)
if len(result) == 0:
return ""
return result[0]['text_content']
except Exception as e:
raise e
def checkMD5HashExistence(self, md5hash):
try:
SQLStatement = 'SELECT * from run_data_content where md5_hash_of_content = "%s"' % (md5hash)
result = self.query(SQLStatement)
if len(result) > 0:
return result[0]['id']
else:
return -1
except Exception as e:
raise e
def getSimulatorIdFromRunId(self, simulationRunId_):
try:
SQLStatement = 'SELECT software_id from run where id = "%s"' % str(simulationRunId_)
result = self.query(SQLStatement)
if len(result) > 1:
raise RuntimeError("getSimulatorIdFromRunId: more than one run with simulationRunId %s" % str(simulationRunId_))
return result[0]["software_id"]
except Exception as e:
raise e
def getSimulationInputFilesForRunId(self, runId, translatorKey, simulatorKey):
SQLStatement = 'SELECT rddv.label, rdc.text_content FROM run_data_content rdc, run_data rd, '\
+ 'run_data_description_view rddv WHERE rd.content_id = rdc.id AND rd.run_id = %s ' % str(runId)\
+ 'AND rddv.run_data_description_id = rd.description_id AND rddv.source_software = %s ' % str(translatorKey)\
+ 'AND rddv.destination_software = %s' % str(simulatorKey)
result = self.query(SQLStatement)
if len(result) == 0:
self.logger.update('DB_QUERY_NO_RESULTS', message=SQLStatement)
return ('unknown', 'Could not find runId: %s in the ApolloDB' % str(runId))
fileDict = {}
for row in result:
fileDict[row['label']] = row['text_content']
return fileDict
def getRunStatus(self, runId):
SQLStatement = 'SELECT t2.status,t1.message from run_status t1, run_status_description t2 '\
+ 'where t1.run_id = %s and t2.id = t1.status_id' % str(runId)
result = self.query(SQLStatement)
if len(result) == 0:
self.logger.update('DB_QUERY_NO_RESULTS', message=SQLStatement)
return ('unknown', 'Could not find runId: %s in the ApolloDB' % str(runId))
if len(result) > 1:
self.logger.update('DB_QUERY_MORE_THAN_ONE', messsage=SQLStatement)
return ('failed', 'More than in entry for runID: %s in the ApolloDB' % str(runId))
return (result[0]['status'], result[0]['message'])
def getAllowedStatus(self):
SQLStatement = "SELECT status from run_status_description"
result = self.query(SQLStatement)
return [x['status'] for x in result]
def setRunStatus(self, runId, status, message=""):
if status not in self.getAllowedStatus():
self.logger.update('DB_STATUS_ERROR', message="Set Status: Status %s not allowed" % str(status))
return -1
thisStatus, thisMes = self.getRunStatus(runId)
if thisStatus == "unknown":
SQLStatement = ' INSERT into run_status (run_id,status_id,message) values '\
+ '("%s",' % str(runId)\
+ '(SELECT id from run_status_description where status = "%s"),' % status \
+ '"%s") ' % message
self.query(SQLStatement)
else:
SQLStatement = 'UPDATE run_status set status_id = '\
+ '(SELECT id from run_status_description where status = "%s" ),' % str(status)\
+ 'message = "%s" where run_id = %s' % (str(message), str(runId))
result = self.query(SQLStatement)
return 0
def main():
apolloDB = ApolloDB(dbname_="test")
print "Testing connection to Apollo database"
apolloDB.connect()
print "Connection to Database successful"
# runList = apolloDB.getRuns()
# print "Keys: Translator %d FRED Simulator %d"%\
# (apolloDB.getTranslatorServiceKey(),\
# apolloDB.getSimulatorServiceKey('UPitt,PSC,CMU','FRED','2.0.1_i'))
# fileDict = apolloDB.getSimulationInputFilesForRunId('74',1,3)
# for fileName,content in fileDict.items():
# print "%s"%fileName
print str(apolloDB.getAllowedStatus())
print str(apolloDB.getRunStatus(2418))
print str(apolloDB.getSoftwareIdentificationForRunId(14))
apolloDB.setRunStatus(2418, "failed", "my Message")
print str(apolloDB.getRunStatus(2418))
apolloDB.setRunStatus(2418, "completed", "Completed at Thu, 10 Jul 2014 14:48:12 -0400")
print str(apolloDB.getRunStatus(14))
print "Closing connection to database"
apolloDB.close()
print "Closing successful"
# print "RunList 0: " + runList[0]
# apolloOut = ApolloSimulatorOutput(runList[0])
# apolloOut.getNewlyInfectedTimeSeriesWithinLocation("42003")
# apolloOut.printSimulatedPopulations()
############
# Main hook
############
if __name__ == "__main__":
main()