def make_dbnote(): st = state.State() r = st.get_active() name = r['name'] ready = True if r['ready'] == 1 else False path = r['path'] molcount = r['molcount'] confcount = r['confcount'] basename = os.path.split(path)[1] info = f'{basename} ({name}): ' \ f'{molcount:,} mols, {confcount:,} confs, ready={ready}' count = st.get_queue_count() if ready and count == 0: link = '<a href="/changedb_page">Change Database</a>' else: link = '<span style="color: #ccc;">Change Database</span>' s = f""" <table width="100%"> <tr> <td>{info}</td> <td align="right" style="font-size:80%;">{link}</td> </tr> </table> """.lstrip() return s
def poll(app): d = misc.read_config() while True: st = state.State() for r in st.get_queue(): # wait for active db to be ready while True: active_db = st.get_active() if active_db['ready'] == 1: break time.sleep(d['POLLING_INTERVAL']) dbname = active_db['name'] oid = r['oid'] ext = r['ext'] hitsize = r['hitsize'] st.update_repo(oid, status=1, dbname=dbname) client = d['FASTROCS_CLIENT'] host = d['FASTROCS_HOST'] query = f'instance/{oid}/query.{ext}' hitlist = f'instance/{oid}/hitlist.sdf' command = f'{client} {host} {query} {hitlist} {hitsize}' print(f'[LOG:COMMAND] {command}', flush=True) proc = misc.run_proc(command) for line in misc.read_output(proc): print(line, flush=True) proc.wait() if r['reportable']: # Correct SDF for ROCS_REPORT misc.fix_sdf(hitlist) rocs_report = d['ROCS_REPORT'] report_pdf = f'instance/{oid}/report.pdf' command = f"{rocs_report} -in {hitlist} -refmol {query} -out {report_pdf}" print(f'[LOG:COMMAND] {command}', flush=True) proc = misc.run_proc(command) for line in misc.read_output(proc): print(line, flush=True) proc.wait() timestamp2 = time.time() st.update_repo(oid, timestamp2=timestamp2, status=2) time.sleep(d['POLLING_INTERVAL'])
def upload(self, file, hitsize, remote_addr): oid = uuid.uuid4().hex filename = secure_filename(file.filename) ext = oechem.OEGetFileExtension(filename) os.makedirs(f'instance/{oid}', exist_ok=True) opath = f'instance/{oid}/query.{ext}' file.save(opath) if ext != 'sq' and not misc.check_mol(opath): os.unlink(opath) return st = state.State() print('hitsize:', hitsize) #@@ st.repo_new(oid, filename, hitsize, remote_addr)
def changedb(self, dbname): st = state.State() if 0 < st.get_queue_count(): return active_db = st.get_active() count = active_db['count'] pid = active_db['pid'] st.set_active(dbname) st.update_active_db(count=count+1) if 0 < pid: print('[LOG] killing', pid, flush=True) kill = misc.run_proc(f'kill -9 {pid}') kill.wait() time.sleep(2) # for safety releasing network port etc. self.fastrocs_server_proc = mp.Process(target=fastrocs_server.run, args=(self,)) self.fastrocs_server_proc.start()
def __init__(self): Flask.__init__(self, __name__) self.config.from_mapping(misc.read_config()) os.makedirs('instance', exist_ok=True) self.instance_path = os.path.abspath(self.config['INSTANCE_PATH']) st = state.State() st.initialize(self.config['DATABASES']) name = self.config['DEFAULT_DB'] st.set_active(name) st.update_active_db(count=1) self.fastrocs_server_proc = mp.Process(target=fastrocs_server.run, args=(self,)) self.fastrocs_server_proc.start() self.polling_proc = mp.Process(target=polling.poll, args=(self,)) self.polling_proc.start()
def download(self, klass, oid): st = state.State() r = st.get_one(oid) ext = r['ext'] basename = r['basename'] if klass == 'query': iname = f'{oid}/query.{ext}' oname = f'{basename}.{ext}' elif klass == 'result': iname = f'{oid}/hitlist.sdf' oname = f'{basename}_hitlist.sdf' else: iname = f'{oid}/report.pdf' oname = f'{basename}_report.pdf' if klass == 'query': return send_from_directory(self.instance_path, iname, as_attachment=True, \ attachment_filename=oname) elif klass == 'result': return send_from_directory(self.instance_path, iname, as_attachment=True, \ attachment_filename=oname) else: return send_from_directory(self.instance_path, iname, attachment_filename=oname)
def run(app): FASTROCS_SERVER = misc.read_config()['FASTROCS_SERVER'] st = state.State() active_db = st.get_active() name = active_db['name'] path = active_db['path'] command = f'{FASTROCS_SERVER} {path}' print(f'[LOG:COMMAND] {command}', flush=True) proc = misc.run_proc(command) st.update_active_db(pid=proc.pid) for line in misc.read_output(proc): print(line, flush=True) if line.startswith('Total:') and line.endswith( 'conformers processed.'): confcount = int(line.split()[1]) st.update_db(name, confcount=confcount) if line.endswith('seconds to load database'): loadtime = float(line.split()[0]) st.update_active_db(loadtime=loadtime, ready=1)
def background_thread(): st = state.State() since = time.time() - app.config['MAX_DAYS'] * 60 * 60 * 24 fp_old = '' while True: fp = st.get_footprint(since) if fp != fp_old: print(f'[LOG] list updated {fp}', flush=True) table = messaging.make_table(app.config) db_status = messaging.make_dbnote() args = dict(table=table, db_status=db_status) socketio.emit('listings_changed', args, broadcast=True, namespace='/listings') else: socketio.sleep(app.config['POLLING_INTERVAL']) fp_old = fp
def make_table(config): st = state.State() now = time.time() since = now - config['MAX_DAYS'] * 60 * 60 * 24 s = """ <table> <tr> <td></td> <td><b>Query</b></td> <td><b>Hitsize</b></td> <td><b>Hitlist</b></td> <td><b>ROCS Report</b></td> <td><b>DB</b></td> <td><b>From</b></td> <td><b>Timestamp</b></td> <td><b>OID</b></td> </tr> """.lstrip() for r in st.get_list(since): oid = r['oid'] basename = r['basename'] ext = r['ext'] reportable = r['reportable'] timestamp1 = r['timestamp1'] timestamp2 = r['timestamp2'] hitsize = r['hitsize'] dbname = r['dbname'] rstatus = r['status'] ip = r['ip'] timestamp1 = misc.iso_time(timestamp1, millisec=True) rstatus_mark = ['⊹', '✷', '✓'][rstatus] if rstatus == 1: query_link = f'<a class="bred" href="/download/query/{oid}">{basename}.{ext}</a>' else: query_link = f'<a href="/download/query/{oid}">{basename}.{ext}</a>' result_link = '' report_link = '' tr_begin = '<tr>' if rstatus == 1: tr_begin = '<tr class="bred">' if rstatus == 2: result_link = f'<a href="/download/result/{oid}">Hits</a>' if reportable == 1 and rstatus == 2: report_link = f'<a href="/download/report/{oid}" target="_blank">PDF</a>' s += f""" {tr_begin} <td>{rstatus_mark}</td> <td>{query_link}</td> <td align="right">{hitsize}</td> <td>{result_link}</td> <td>{report_link}</td> <td>{dbname}</td> <td>{ip}</td> <td>{timestamp1}</td> <td><span style="font-family:monospace; font-size:80%;">{oid}</span></td> </tr> """.lstrip() s += """ </table> """.lstrip() return s