def switchboard(user, analysis_data): """Fires off all jobs for a given analysis. INPUTS: user: username of user requesting job analysis_data: MetaAnalysisData object with all information in it. Raises a RuntimeError if there is any error connecting with the DB """ analysis_name = analysis_data.get_analysis() # Insert analysis into the postgres analysis table SQL = """INSERT INTO qiita_analysis (qiita_username, analysis_name, analysis_studies, analysis_metadata, analysis_timestamp) VALUES (%s, %s, %s, %s, 'now') RETURNING analysis_id""" sql_studies_list = "{%s}" % ','.join(analysis_data.get_studies()) sql_metadata_list = "{%s}" % ','.join(analysis_data.get_metadata()) parameters = (user, analysis_name, sql_studies_list, sql_metadata_list) try: pgcursor = postgres.cursor() pgcursor.execute(SQL, parameters) analysis_id = pgcursor.fetchone()[0] postgres.commit() except PostgresError, e: pgcursor.close() postgres.rollback() raise RuntimeError("Can't add meta analysis to table: %s" % e)
def job_handler(user, analysis_id, analysis_name, datatype, job, opts): """ INPUTS: user: username of user owner of the analysis analysis_id: DB id of the analysis analysis_name: name of the analysis datatype: job's datatype job: name of the job to run opts: arguments of the job Raises a RuntimeError if there is any error connecting with the DB """ # Dictionary that maps job name with the actual function that # executes the job job_functions_dict = { 'Alpha_Diversity': alpha_diversity, 'Beta_Diversity': beta_diversity, 'Procrustes': procrustes } # Build job identifier for message handling datatype_job = '%s:%s' % (datatype, job) # Push the job has been started push_notification(user, analysis_name, datatype_job, 'Running') # Run the actual job # This is needed due to the dummy functions # we will need to remove this once we are calling # the real functions opts['datatype'] = datatype success, results = job_functions_dict[job](opts) if success: # Push the job finished successfully push_notification(user, analysis_name, datatype_job, 'Completed', results, done=True) else: # Push the job failed push_notification(user, analysis_name, datatype_job, 'ERROR', done=True) # Mark current job as DONE in the DB and check if the other jobs # of the same analysis are done # Create a tuple with the SQL values # Format: (SQL list of output files, datatype, job run, analysis id) sql_results = ("{%s}" % ','.join(results), datatype, job, analysis_id) # Update job in job table to done and with their results SQL = """UPDATE qiita_job SET job_done = true, job_results = %s WHERE job_datatype = %s AND job_type = %s AND analysis_id = %s""" try: pgcursor = postgres.cursor() pgcursor.execute(SQL, sql_results) postgres.commit() except PostgresError, e: pgcursor.close() postgres.rollback() raise RuntimeError("Can't finish off job: %s" % e)
def post(self, page): analysis = self.get_argument('analysis') user = self.get_current_user() SQL = "SELECT * FROM qiita_job WHERE analysis_id = %s" try: pgcursor = postgres.cursor(cursor_factory=DictCursor) pgcursor.execute(SQL, (analysis,)) analysis_info = pgcursor.fetchall() except PostgresError, e: pgcursor.close() postgres.rollback() raise RuntimeError("Analysis info can not be retrieved: %s" % e)
def get(self, analysis): user = self.get_current_user() SQL = """SELECT analysis_id FROM qiita_analysis WHERE qiita_username = %s AND analysis_name = %s""" try: pgcursor = postgres.cursor(cursor_factory=DictCursor) pgcursor.execute(SQL, (user, analysis)) analysis_id = pgcursor.fetchone()[0] except PostgresError, e: pgcursor.close() postgres.rollback() raise RuntimeError("Analysis info can not be retrieved: %s" % e)
def check_permission(self, username, password): SQL = """SELECT qiita_password from qiita_users WHERE qiita_username = %s""" try: pgcursor = postgres.cursor() pgcursor.execute(SQL, (username,)) dbpass = pgcursor.fetchone()[0] postgres.commit() pgcursor.close() except PostgresError, e: pgcursor.close() postgres.rollback() return False
def delete_job(user, jobid): try: pgcursor = postgres.cursor() pgcursor.execute('DELETE FROM qiita_job WHERE analysis_id = %s', (jobid,)) pgcursor.execute('DELETE FROM qiita_analysis WHERE analysis_id = %s', (jobid,)) postgres.commit() pgcursor.close() except PostgresError, e: pgcursor.close() postgres.rollback() raise RuntimeError("Can't remove metaanalysis from database: %s" % e)
def get(self): username = self.get_current_user() SQL = """SELECT DISTINCT analysis_name, analysis_id FROM qiita_analysis WHERE qiita_username = %s AND analysis_done = true ORDER BY analysis_name""" try: pgcursor = postgres.cursor(cursor_factory=DictCursor) pgcursor.execute(SQL, (username,)) completed_analyses = pgcursor.fetchall() postgres.commit() pgcursor.close() except PostgresError, e: pgcursor.close() postgres.rollback()
def get(self): username = self.get_current_user() SQL = """SELECT analysis_name, analysis_timestamp FROM qiita_analysis WHERE qiita_username = %s AND analysis_done = false""" try: pgcursor = postgres.cursor(cursor_factory=DictCursor) pgcursor.execute(SQL, (username,)) analyses = pgcursor.fetchall() postgres.commit() pgcursor.close() except PostgresError, e: pgcursor.close() postgres.rollback() raise RuntimeError("Job info can not be retrieved: %s" % e)
def create_user(self, username, password): if username == "": return False, "No username given!" if password == sha512("").hexdigest(): return False, "No password given!" # Check to make sure user does not already exist SQL = "SELECT count(1) FROM qiita_users WHERE qiita_username = %s" try: pgcursor = postgres.cursor() pgcursor.execute(SQL, (username,)) exists = pgcursor.fetchone()[0] except PostgresError, e: pgcursor.close() postgres.rollback() return False, "Database query error! %s" % str(e)
def post(self, page): if page == '1': pass elif page == '2': analysisname = self.get_argument('analysisname') #check if metaAnalysis name already exists SQL = """SELECT analysis_id FROM qiita_analysis WHERE qiita_username = %s AND analysis_name = %s""" try: pgcursor = postgres.cursor() pgcursor.execute(SQL, (self.user, analysisname)) analysis_info = pgcursor.fetchall() pgcursor.close() except PostgresError, e: pgcursor.close() postgres.rollback() raise RuntimeError("Name can't be checked: %s" % e) if len(analysis_info) > 0: self.render('meta1.html', user=self.user, error="Analyis name already exists!") metaAnalysis.set_analysis(analysisname) metaAnalysis.set_studies(self.get_arguments('studiesView')) if metaAnalysis.get_studies() == []: raise ValueError('Need at least one study to analyze.') metaAnalysis.set_metadata(self.get_arguments('metadataUse')) if metaAnalysis.get_metadata() == []: raise ValueError('Need at least one metadata selected.') metaAnalysis.set_datatypes(self.get_arguments('datatypeView')) if metaAnalysis.get_datatypes() == []: raise ValueError('Need at least one datatype selected.') #add the combined datatype if more than one selected if len(metaAnalysis.get_datatypes()) > 1: metaAnalysis.add_datatype("Combined") self.render('meta2.html', user=self.user, datatypes=metaAnalysis.get_datatypes(), single=SINGLE, combined=COMBINED)
def finish_analysis(user, analysis_id, analysis_name): """Marks current analysis as done in the DB INPUTS: user: username of user owner of the analysis analysis_id: DB id of the analysis analysis_name: name of the analysis Raises a RuntimeError if there is any error connecting with the DB """ # Update analysis to done in analysis table SQL = """UPDATE qiita_analysis SET analysis_done = true WHERE analysis_id = %s""" try: pgcursor = postgres.cursor() pgcursor.execute(SQL, (analysis_id,)) postgres.commit() pgcursor.close() except PostgresError, e: pgcursor.close() postgres.rollback() raise RuntimeError("Can't finish off analysis: %s" % e)