示例#1
0
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)
示例#2
0
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)
示例#3
0
 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
示例#4
0
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)
示例#5
0
 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()
示例#6
0
 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)
示例#7
0
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)
示例#8
0
        except PostgresError, e:
            pgcursor.close()
            postgres.rollback()
            return False, "Database query error! %s" % str(e)

        if exists:
            return False, "Username already exists!"

        SQL = """INSERT INTO qiita_users (qiita_username, qiita_password)
            VALUES (%s, %s)"""
        try:
            # THIS IS THE ONLY PLACE THAT SHOULD MODIFY THE DB IN THIS CODE!
            # ALL OTHERS GO THROUGH THE MIDDLEWARE!!!!!
            # THIS PROBABLY SHOULD BE MIDDLEWARE TOO!
            pgcursor.execute(SQL, (username, password))
            postgres.commit()
            pgcursor.close()
        except PostgresError, e:
            pgcursor.close()
            postgres.rollback()
            return False, "Database set error! %s" % str(e)

        return True, ""


class AuthLoginHandler(BaseHandler):
    '''Login Page'''
    def get(self):
        try:
            error_message = self.get_argument("error")
        # Tornado can raise an Exception directly, not a defined type