forked from mgckind/desaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
431 lines (392 loc) · 15.1 KB
/
api.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
import Settings
import sqlite3 as lite
import tornado.web
import tornado.websocket
import json
import os
import datetime
import easyaccess as ea
import MySQLdb as mydb
from celery import Celery
import yaml
import jira_ticket
from smtp import email_utils
class BaseHandler(tornado.web.RequestHandler):
def get_current_user(self):
return self.get_secure_cookie("usera")
def humantime(s):
if s < 60:
return "%d seconds" % s
else:
mins = s/60
secs = s % 60
if mins < 60:
return "%d minutes and %d seconds" % (mins, secs)
else:
hours = mins/60
mins = mins % 60
if hours < 24:
return "%d hours and %d minutes" % (hours, mins)
else:
days = hours/24
hours = hours % 24
return "%d days and %d hours" % (days, hours)
class MyExamplesHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
query0 = """--
-- Example Query --
-- This query selects 0.001% of the data
SELECT RA, DEC, MAG_AUTO_G, TILENAME from DR1_MAIN sample(0.001)
"""
query0ra = """--
-- Example Query --
-- This query selects the first 1000 rows from a RA/DEC region
SELECT ALPHAWIN_J2000 RAP,DELTAWIN_J2000 DECP, MAG_AUTO_G, TILENAME
FROM DR1_MAIN
WHERE
RA BETWEEN 40.0 and 41.0 and
DEC BETWEEN -41 and -40 and
ROWNUM < 1001
"""
query1 = """--
-- Example Query --
-- This query creates a Helpix map of number of starts
-- and their mean magnitude on a resolution of NSIDE = 1024
-- using NEST Schema
SELECT
count(main.MAG_AUTO_I) COUNT,
avg(main.MAG_AUTO_I) COUNT,
main.HPIX_1024
FROM DR1_MAIN main
WHERE
main.WAVG_SPREAD_MODEL_I + 3.0*main.WAVG_SPREADERR_MODEL_I < 0.005 and
main.WAVG_SPREAD_MODEL_I > -1 and
main.IMAFLAGS_ISO_I = 0 and
main.MAG_AUTO_I < 21
GROUP BY main.HPIX_1024
"""
query2 = """--
-- Example Query --
-- This query selects stars around the center of glubular cluster M2
SELECT
COADD_OBJECT_ID,RA,DEC,
MAG_AUTO_G G,
MAG_AUTO_R R,
WAVG_MAG_PSF_G G_PSF,
WAVG_MAG_PSF_R R_PSF
FROM DR1_MAIN
WHERE
RA between 323.36-0.12 and 323.36+0.12 and
DEC between -0.82-0.12 and -0.82+0.12 and
WAVG_SPREAD_MODEL_I + 3.0*WAVG_SPREADERR_MODEL_I < 0.005 and
WAVG_SPREAD_MODEL_I > -1 and
IMAFLAGS_ISO_G = 0 and
IMAFLAGS_ISO_R = 0 and
FLAGS_G < 4 and
FLAGS_R < 4
"""
query3 = """--
-- Example Query --
-- This query selects a sample of bright galaxies
SELECT dr1.RA,dr1.DEC,dr1.COADD_OBJECT_ID
FROM dr1_main sample(0.01) dr1
WHERE
dr1.MAG_AUTO_G < 18 and
dr1.WAVG_SPREAD_MODEL_I + 3.0*dr1.WAVG_SPREADERR_MODEL_I > 0.005 and
dr1.WAVG_SPREAD_MODEL_I + 1.0*dr1.WAVG_SPREADERR_MODEL_I > 0.003 and
dr1.WAVG_SPREAD_MODEL_I - 1.0*dr1.WAVG_SPREADERR_MODEL_I > 0.001 and
dr1.WAVG_SPREAD_MODEL_I > -1 and
dr1.IMAFLAGS_ISO_G = 0 and
dr1.IMAFLAGS_ISO_R = 0 and
dr1.IMAFLAGS_ISO_I = 0 and
dr1.FLAGS_G < 4 and
dr1.FLAGS_R < 4 and
dr1.FLAGS_I < 4 and
dr1.NEPOCHS_G > 0 and
dr1.NEPOCHS_R > 0 and
dr1.NEPOCHS_I > 0
"""
query4 = """--
-- Example Query --
-- This query creates a Helpix map of number of galaxies
-- and their mean magnitude on a resolution of NSIDE = 1024
-- using NEST Schema
SELECT count(dr1.MAG_AUTO_I),avg(dr1.MAG_AUTO_I),dr1.HPIX_1024
FROM DR1_MAIN dr1
where
dr1.WAVG_SPREAD_MODEL_I + 3.0*dr1.WAVG_SPREADERR_MODEL_I > 0.005 and
dr1.WAVG_SPREAD_MODEL_I + 1.0*dr1.WAVG_SPREADERR_MODEL_I > 0.003 and
dr1.WAVG_SPREAD_MODEL_I - 1.0*dr1.WAVG_SPREADERR_MODEL_I > 0.001 and
dr1.WAVG_SPREAD_MODEL_I > -1 and
dr1.IMAFLAGS_ISO_I = 0 and
dr1.MAG_AUTO_I < 23
group by dr1.HPIX_1024
"""
queries = []
queries.append({'desc': 'Sample Basic information', 'query': query0})
queries.append({'desc': 'Limit Basic information by region and number of rows', 'query': query0ra})
queries.append({'desc': 'Create stellar density healpix map', 'query': query1})
queries.append({'desc': 'Select stars from M2 Globular Cluster', 'query': query2})
queries.append({'desc': 'Sample of bright galaxies', 'query': query3})
queries.append({'desc': 'Create galaxy density healpix map', 'query': query4})
jjob = []
jquery = []
for i in range(len(queries)):
jjob.append(queries[i]['desc'])
jquery.append(queries[i]['query'])
out_dict = [dict(job=jjob[i], jquery=jquery[i]) for i in range(len(jjob))]
temp = json.dumps(out_dict, indent=4)
self.write(temp)
class GetTileHandler(BaseHandler):
@tornado.web.authenticated
def post(self):
tilename = self.get_argument('tilename', '')
print(tilename)
loc_user = self.get_secure_cookie("usera").decode('ascii').replace('\"', '')
loc_passw = self.get_secure_cookie("userb").decode('ascii').replace('\"', '')
loc_db = self.get_secure_cookie("userdb").decode('ascii').replace('\"', '')
con = ea.connect(loc_db, user=loc_user, passwd=loc_passw)
query = """select FITS_IMAGE_G, FITS_IMAGE_R, FITS_IMAGE_I, FITS_IMAGE_Z, FITS_IMAGE_Y,
FITS_IMAGE_DET, TIFF_COLOR_IMAGE, FITS_DR1_MAIN, FITS_DR1_MAGNITUDE, FITS_DR1_FLUX
from DR1_TILE_INFO where tilename = '{0}'""".format(tilename)
temp_df = con.query_to_pandas(query)
new = temp_df.transpose().reset_index()
new.columns = ['name', 'path']
con.close()
self.write(new.to_json(orient='records'))
class MyLogsHandler(BaseHandler):
@tornado.web.authenticated
def post(self):
loc_user = self.get_secure_cookie("usera").decode('ascii').replace('\"', '')
jobid = self.get_argument('jobid')
print(loc_user, jobid)
log_path = os.path.join(Settings.WORKDIR, loc_user, jobid, 'log.log')
log = ''
with open(log_path, 'r') as logFile:
for line in logFile:
log += line+'<br>'
temp = json.dumps(log)
self.write(temp)
class MyJobsHandler(BaseHandler):
@tornado.web.authenticated
def delete(self):
user = self.get_argument('username')
jobid = self.get_argument('jobid')
app = Celery()
app.config_from_object('config.celeryconfig')
app.control.revoke(jobid, terminate=True, signal='SIGUSR1')
app.close()
with open('config/mysqlconfig.yaml', 'r') as cfile:
conf = yaml.load(cfile)['mysql']
con = mydb.connect(**conf)
cur = con.cursor()
q0 = "UPDATE Jobs SET status='{0}' where job = '{1}'".format('REVOKE', jobid)
cur.execute(q0)
con.commit()
con.close()
self.finish()
@tornado.web.authenticated
def get(self):
loc_user = self.get_secure_cookie("usera").decode('ascii').replace('\"', '')
with open('config/mysqlconfig.yaml', 'r') as cfile:
conf = yaml.load(cfile)['mysql']
con = mydb.connect(**conf)
cur = con.cursor()
cur.execute("SELECT * from Jobs where user = '{0}' order by time DESC".format(loc_user))
cc = cur.fetchall()
con.close()
cc = list(cc)
jjob = []
jstatus = []
jobtype = []
jtime = []
jname = []
jelapsed = []
jquery = []
jfiles = []
jsizes = []
jfiles_bool = []
jquery_bool = []
jwarning = []
jruntime = []
for i in range(len(cc)):
#dd = datetime.datetime.strptime(cc[i][3], '%Y-%m-%d %H:%M:%S')
dd = cc[i][4]
ctime = dd.strftime('%a %b %d %H:%M:%S %Y')
jjob.append(cc[i][1])
jname.append(cc[i][2])
jstatus.append(cc[i][3])
jobtype.append(cc[i][5])
jtime.append(ctime)
jquery.append(cc[i][6])
jfiles.append(cc[i][7])
jsizes.append(cc[i][8])
jruntime.append(cc[i][9])
if cc[i][7] == '':
jfiles_bool.append(False)
else:
jfiles_bool.append(True)
if cc[i][6] == '':
jquery_bool.append(False)
else:
jquery_bool.append(True)
delt = (datetime.datetime.now()-dd).total_seconds()
jelapsed.append(humantime(delt)+" ago")
jwarning.append(delt >= 3600*24)
if delt >= 3600*24*2:
jfiles_bool.pop()
jfiles_bool.append(False)
jelapsed.pop()
jelapsed.append(humantime(delt)+" ago (Expired)")
out_dict = [dict(job=jjob[i], status=jstatus[i], time=jtime[i], elapsed=jelapsed[i],
jquery=jquery[i], jfiles=jfiles[i], jbool=jfiles_bool[i], user=loc_user,
jsizes=jsizes[i], jname=jname[i], jobtype=jobtype[i],
jquerybool=jquery_bool[i], jwarning=jwarning[i], jruntime=jruntime[i]) for i in range(len(jjob))]
temp = json.dumps(out_dict, indent=4)
self.write(temp)
class MyTablesHandler(BaseHandler):
@tornado.web.authenticated
def post(self):
response = {k: self.get_argument(k) for k in self.request.arguments}
loc_user = self.get_secure_cookie("usera").decode('ascii').replace('\"', '')
loc_passw = self.get_secure_cookie("userb").decode('ascii').replace('\"', '')
loc_db = self.get_secure_cookie("userdb").decode('ascii').replace('\"', '')
con = ea.connect(loc_db, user=loc_user, passwd=loc_passw)
query = """
SELECT t.table_name, s.bytes/1024/1024/1024 SIZE_GBYTES, t.num_rows NROWS
FROM user_segments s, user_tables t
WHERE s.segment_name = t.table_name order by t.table_name
"""
temp_df = con.query_to_pandas(query)
con.close()
self.write(temp_df.to_json(orient='records'))
class MyResponseHandler(BaseHandler):
@tornado.web.authenticated
def post(self):
loc_user = self.get_secure_cookie("usera").decode('ascii').replace('\"', '')
jobid = self.get_argument('jobid')
user_folder = os.path.join(Settings.WORKDIR, loc_user)+'/'
jsonfile = os.path.join(user_folder, jobid+'.json')
try:
with open(jsonfile, 'r') as data_file:
tmp = json.load(data_file)
except:
tmp = ''
self.flush()
self.write(tmp)
self.finish()
class DescTablesHandler(BaseHandler):
@tornado.web.authenticated
def post(self):
response = {k: self.get_argument(k) for k in self.request.arguments}
owner = self.get_argument('owner')
table = self.get_argument('tablename')
loc_user = self.get_secure_cookie("usera").decode('ascii').replace('\"', '')
loc_passw = self.get_secure_cookie("userb").decode('ascii').replace('\"', '')
loc_db = self.get_secure_cookie("userdb").decode('ascii').replace('\"', '')
owner = 'DES_ADMIN'
con = ea.connect(loc_db, user=loc_user, passwd=loc_passw)
query = """
select atc.column_name, atc.data_type,
case atc.data_type
when 'NUMBER' then '(' || atc.data_precision || ',' || atc.data_scale || ')'
when 'VARCHAR2' then atc.CHAR_LENGTH || ' characters'
else atc.data_length || '' end as DATA_FORMAT,
acc.comments
from all_tab_cols atc , all_col_comments acc, all_synonyms ass
where ass.synonym_name = '{table}' and
atc.owner = ass.table_owner and atc.table_name = ass.table_name
and acc.owner = ass.table_owner and acc.table_name = ass.table_name
and acc.column_name = atc.column_name
order by atc.column_name
""".format(owner=owner.upper(), table=table.upper())
temp_df = con.query_to_pandas(query)
con.close()
self.write(temp_df.to_json(orient='records'))
class AllTablesHandler(BaseHandler):
@tornado.web.authenticated
def post(self):
response = {k: self.get_argument(k) for k in self.request.arguments}
loc_user = self.get_secure_cookie("usera").decode('ascii').replace('\"', '')
loc_passw = self.get_secure_cookie("userb").decode('ascii').replace('\"', '')
loc_db = self.get_secure_cookie("userdb").decode('ascii').replace('\"', '')
print(loc_db)
con = ea.connect(loc_db, user=loc_user, passwd=loc_passw)
query = """
SELECT t.synonym_name as table_name, a.num_rows as NROWS
FROM all_synonyms t, all_tables a
where t.table_owner = 'DES_ADMIN' and t.table_name = a.table_name
order by table_name;
"""
temp_df = con.query_to_pandas(query)
con.close()
self.write(temp_df.to_json(orient='records'))
class DeleteHandler(BaseHandler):
@tornado.web.authenticated
def delete(self):
response = {k: self.get_argument(k) for k in self.request.arguments}
loc_user = self.get_secure_cookie("usera").decode('ascii').replace('\"', '')
user_folder = os.path.join(Settings.WORKDIR, loc_user)+'/'
Nd = len(response)
with open('config/mysqlconfig.yaml', 'r') as cfile:
conf = yaml.load(cfile)['mysql']
con = mydb.connect(**conf)
cur = con.cursor()
for j in range(Nd):
jid = response[str(j)]
q = "DELETE from Jobs where job = '%s' and user = '%s'" % (jid, loc_user)
cc = cur.execute(q)
folder = os.path.join(user_folder, jid)
print(folder)
try:
os.system('rm -rf ' + folder)
except:
pass
try:
os.system('rm -f ' + user_folder + jid + '.*')
except:
pass
con.commit()
con.close()
self.set_status(200)
self.flush()
self.finish()
class ChangeHandler(BaseHandler):
@tornado.web.authenticated
def post(self):
user = self.get_argument('username')
jobid = self.get_argument('jobid')
jobname = self.get_argument('jobname')
with open('config/mysqlconfig.yaml', 'r') as cfile:
conf = yaml.load(cfile)['mysql']
con = mydb.connect(**conf)
cur = con.cursor()
q0 = "UPDATE Jobs SET name='{0}' where job = '{1}'".format(jobname, jobid)
cur.execute(q0)
con.commit()
con.close()
self.finish()
class HelpHandler(tornado.web.RequestHandler):
"""
This class is special as it also include a post request to
deal with the form submission
"""
@tornado.web.asynchronous
def post(self):
arguments = { k.lower(): self.get_argument(k) for k in self.request.arguments }
print(arguments)
name = self.get_argument("name", "User")
last = self.get_argument("lastname", "")
email = self.get_argument("email", "")
subject = self.get_argument("subject", "")
question = self.get_argument("question", "")
topic = self.get_argument("topic", "")
topics = topic.replace(',', '\n')
print(name, last, email, topic, question)
valid, ticket = jira_ticket.create_ticket(name, last, email, topics, subject, question)
print(valid)
email_utils.send_thanks(name, email, subject, ticket)
self.set_status(200)
self.flush()
self.finish()