<commit_before># -*- coding: utf-8 -*-
"""
The analysis_run module contains functions related to the management of AnalysisRun objects.
"""
import os
import datetime
import logging

from tracker.util import connection
from sqlalchemy import and_

AnalysisRun = connection.Base.classes.analysis_run

RUN_STATUS_READY = 0
RUN_STATUS_SCHEDULED = 1
RUN_STATUS_IN_PROGRESS = 2
RUN_STATUS_COMPLETED = 3
RUN_STATUS_ERROR = 4

RUN_STATUS_READY_STRING = "ready"
RUN_STATUS_SCHEDULED_STRING = "scheduled"
RUN_STATUS_IN_PROGRESS_STRING = "in-progress"
RUN_STATUS_COMPLETED_STRING = "completed"
RUN_STATUS_ERROR_STRING = "error"

run_status_list = [RUN_STATUS_READY_STRING, 
                   RUN_STATUS_SCHEDULED_STRING, 
                   RUN_STATUS_IN_PROGRESS_STRING, 
                   RUN_STATUS_COMPLETED_STRING, 
                   RUN_STATUS_ERROR_STRING]
run_status_dict = {RUN_STATUS_READY_STRING: RUN_STATUS_READY, 
                   RUN_STATUS_SCHEDULED_STRING: RUN_STATUS_SCHEDULED, 
                   RUN_STATUS_IN_PROGRESS_STRING: RUN_STATUS_IN_PROGRESS, 
                   RUN_STATUS_COMPLETED_STRING:RUN_STATUS_COMPLETED, 
                   RUN_STATUS_ERROR_STRING:RUN_STATUS_ERROR_STRING}

logger = logging.getLogger()


def get_run_status_from_string(run_status_string):
    run_status = run_status_dict.get(run_status_string)
    if run_status != None:
        return run_status
    else:
        raise ValueError("Invalid run status string - {}".format(run_status_string))


def create_analysis_run(analysis_id, config_id, workflow_id):
    """
    Create an AnalysisRun and store in the database.

    Args:
        analysis_id (int): Id of the corresponding analysis
        config_id (uuid): Id of the corresponding Configuration
        workflow_id (int): Id of the corresponding workflow


    Returns:
        my_analysis_run (AnalysisRun): The newly created analysis run.
    """
    session = connection.Session()
    
    my_analysis_run = AnalysisRun()
    my_analysis_run.analysis_id = analysis_id
    my_analysis_run.workflow_id = workflow_id
    my_analysis_run.config_id = config_id
    my_analysis_run.run_status = RUN_STATUS_READY

    now = datetime.datetime.now()
    my_analysis_run.last_updated_date = now
    my_analysis_run.created_date = now

    session.add(my_analysis_run)
    session.commit()
    session.close()
    
    return my_analysis_run


def set_configuration_for_analysis_run(analysis_run_id, config_id):
    """
    Set the configuration for analysis run with ID analysis_run_id.

    Args:
        analysis_run_id (id): ID of analysis run
        config_id (uuid): Id of the corresponding Configuration

    Returns:
        my_analysis_run (AnalysisRun): The updated analysis run.
    """
    session = connection.Session()
    
    my_analysis_run = session.query(AnalysisRun).filter(
        AnalysisRun.analysis_run_id == analysis_run_id).first()

    my_analysis_run.config_id = config_id

    now = datetime.datetime.now()
    my_analysis_run.last_updated_date = now

    session.commit()
    session.close()
    
    return my_analysis_run


def get_analysis_run_by_id(analysis_run_id):
    """
    Get the analysis run with ID analysis_run_id.

    Args:
        analysis_run_id (id): ID of analysis run
    
    Returns:
        my_analysis_run (AnalysisRun): The analysis run that has id analysis_run_id.
    """
    session = connection.Session()
    
    my_analysis_run = session.query(AnalysisRun).filter(
        AnalysisRun.analysis_run_id == analysis_run_id).first()

    session.close()
    return my_analysis_run

def set_ready(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_READY.
    Only possible if the current status is not RUN_STATUS_IN_PROGRESS.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update

    Raises:
        ValueError: If the analysis run is not in the correct state.
    """
    if my_run.run_status == RUN_STATUS_IN_PROGRESS:
        msg = "Attempting to put an In Progress run into Ready state, runID: {}".format(my_run.analysis_run_id)
        print msg

        raise ValueError(msg)
    else:

        session = connection.Session()

        my_run.run_status = RUN_STATUS_READY
        now = datetime.datetime.now()
        my_run.last_updated_date = now
        
        session.add(my_run)
        session.commit()
        session.close()

def set_scheduled(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_SCHEDULED.
    Only possible if the current status is RUN_STATUS_READY.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update

    Raises:
        ValueError: If the analysis run is not in the correct state.
    """
    if my_run.run_status != RUN_STATUS_READY:
        msg = "Wrong run status - {}, Only a Ready run can be Scheduled, runID: {}".format(my_run.run_status, my_run.analysis_run_id)
        print msg

        raise ValueError(msg)
    else:

        session = connection.Session()

        my_run.run_status = RUN_STATUS_SCHEDULED
        now = datetime.datetime.now()
        my_run.last_updated_date = now
        
        session.add(my_run)
        session.commit()
        session.close()

def set_in_progress(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_IN_PROGRESS.
    Only possible if the current status is RUN_STATUS_SCHEDULED.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update

    Raises:
        ValueError: If the analysis run is not in the correct state.
    """
    if my_run.run_status != RUN_STATUS_SCHEDULED:
        msg = "Wrong run status - {}, Only a Scheduled run can be put In Progress, runID: {}".format(my_run.run_status, my_run.analysis_run_id)
        print msg

        raise ValueError(msg)
    else:
        session = connection.Session()

        my_run.run_status = RUN_STATUS_IN_PROGRESS
        now = datetime.datetime.now()
        my_run.last_updated_date = now
        my_run.run_start_date = now

        session.add(my_run)
        session.commit()
        session.close()


def set_completed(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_COMPLETED.
    Only possible if the current status is RUN_STATUS_IN_PROGRESS.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update

    Raises:
        ValueError: If the analysis run is not in the correct state.
    """
    if my_run.run_status != RUN_STATUS_IN_PROGRESS:

        raise ValueError("Wrong run status - {}, Only an In Progress run can be Finished, runID: {}".\
                         format(my_run.run_status, my_run.analysis_run_id))
    else:

        session = connection.Session()

        my_run.run_status = RUN_STATUS_COMPLETED

        now = datetime.datetime.now()
        my_run.last_updated_date = now
        my_run.run_end_date = now

        session.add(my_run)
        session.commit()
        session.close()


def set_error(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_ERROR.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update
    """
    session = connection.Session()

    my_run.run_status = RUN_STATUS_ERROR

    now = datetime.datetime.now()
    my_run.last_updated_date = now

    session.add(my_run)
    session.commit()
    session.close()

def get_number_of_runs_with_status(analysis_id, run_status):
    session = connection.Session()
    num_runs = session.query(AnalysisRun).filter(and_(AnalysisRun.analysis_id == analysis_id, AnalysisRun.run_status == run_status)).count()
    session.close()

    return num_runs<commit_msg>Fix typo in run status dictionary.<commit_after># -*- coding: utf-8 -*-
"""
The analysis_run module contains functions related to the management of AnalysisRun objects.
"""
import os
import datetime
import logging

from tracker.util import connection
from sqlalchemy import and_

AnalysisRun = connection.Base.classes.analysis_run

RUN_STATUS_READY = 0
RUN_STATUS_SCHEDULED = 1
RUN_STATUS_IN_PROGRESS = 2
RUN_STATUS_COMPLETED = 3
RUN_STATUS_ERROR = 4

RUN_STATUS_READY_STRING = "ready"
RUN_STATUS_SCHEDULED_STRING = "scheduled"
RUN_STATUS_IN_PROGRESS_STRING = "in-progress"
RUN_STATUS_COMPLETED_STRING = "completed"
RUN_STATUS_ERROR_STRING = "error"

run_status_list = [RUN_STATUS_READY_STRING, 
                   RUN_STATUS_SCHEDULED_STRING, 
                   RUN_STATUS_IN_PROGRESS_STRING, 
                   RUN_STATUS_COMPLETED_STRING, 
                   RUN_STATUS_ERROR_STRING]
run_status_dict = {RUN_STATUS_READY_STRING: RUN_STATUS_READY, 
                   RUN_STATUS_SCHEDULED_STRING: RUN_STATUS_SCHEDULED, 
                   RUN_STATUS_IN_PROGRESS_STRING: RUN_STATUS_IN_PROGRESS, 
                   RUN_STATUS_COMPLETED_STRING:RUN_STATUS_COMPLETED, 
                   RUN_STATUS_ERROR_STRING:RUN_STATUS_ERROR}

logger = logging.getLogger()


def get_run_status_from_string(run_status_string):
    run_status = run_status_dict.get(run_status_string)
    if run_status != None:
        return run_status
    else:
        raise ValueError("Invalid run status string - {}".format(run_status_string))


def create_analysis_run(analysis_id, config_id, workflow_id):
    """
    Create an AnalysisRun and store in the database.

    Args:
        analysis_id (int): Id of the corresponding analysis
        config_id (uuid): Id of the corresponding Configuration
        workflow_id (int): Id of the corresponding workflow


    Returns:
        my_analysis_run (AnalysisRun): The newly created analysis run.
    """
    session = connection.Session()
    
    my_analysis_run = AnalysisRun()
    my_analysis_run.analysis_id = analysis_id
    my_analysis_run.workflow_id = workflow_id
    my_analysis_run.config_id = config_id
    my_analysis_run.run_status = RUN_STATUS_READY

    now = datetime.datetime.now()
    my_analysis_run.last_updated_date = now
    my_analysis_run.created_date = now

    session.add(my_analysis_run)
    session.commit()
    session.close()
    
    return my_analysis_run


def set_configuration_for_analysis_run(analysis_run_id, config_id):
    """
    Set the configuration for analysis run with ID analysis_run_id.

    Args:
        analysis_run_id (id): ID of analysis run
        config_id (uuid): Id of the corresponding Configuration

    Returns:
        my_analysis_run (AnalysisRun): The updated analysis run.
    """
    session = connection.Session()
    
    my_analysis_run = session.query(AnalysisRun).filter(
        AnalysisRun.analysis_run_id == analysis_run_id).first()

    my_analysis_run.config_id = config_id

    now = datetime.datetime.now()
    my_analysis_run.last_updated_date = now

    session.commit()
    session.close()
    
    return my_analysis_run


def get_analysis_run_by_id(analysis_run_id):
    """
    Get the analysis run with ID analysis_run_id.

    Args:
        analysis_run_id (id): ID of analysis run
    
    Returns:
        my_analysis_run (AnalysisRun): The analysis run that has id analysis_run_id.
    """
    session = connection.Session()
    
    my_analysis_run = session.query(AnalysisRun).filter(
        AnalysisRun.analysis_run_id == analysis_run_id).first()

    session.close()
    return my_analysis_run

def set_ready(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_READY.
    Only possible if the current status is not RUN_STATUS_IN_PROGRESS.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update

    Raises:
        ValueError: If the analysis run is not in the correct state.
    """
    if my_run.run_status == RUN_STATUS_IN_PROGRESS:
        msg = "Attempting to put an In Progress run into Ready state, runID: {}".format(my_run.analysis_run_id)
        print msg

        raise ValueError(msg)
    else:

        session = connection.Session()

        my_run.run_status = RUN_STATUS_READY
        now = datetime.datetime.now()
        my_run.last_updated_date = now
        
        session.add(my_run)
        session.commit()
        session.close()

def set_scheduled(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_SCHEDULED.
    Only possible if the current status is RUN_STATUS_READY.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update

    Raises:
        ValueError: If the analysis run is not in the correct state.
    """
    if my_run.run_status != RUN_STATUS_READY:
        msg = "Wrong run status - {}, Only a Ready run can be Scheduled, runID: {}".format(my_run.run_status, my_run.analysis_run_id)
        print msg

        raise ValueError(msg)
    else:

        session = connection.Session()

        my_run.run_status = RUN_STATUS_SCHEDULED
        now = datetime.datetime.now()
        my_run.last_updated_date = now
        
        session.add(my_run)
        session.commit()
        session.close()

def set_in_progress(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_IN_PROGRESS.
    Only possible if the current status is RUN_STATUS_SCHEDULED.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update

    Raises:
        ValueError: If the analysis run is not in the correct state.
    """
    if my_run.run_status != RUN_STATUS_SCHEDULED:
        msg = "Wrong run status - {}, Only a Scheduled run can be put In Progress, runID: {}".format(my_run.run_status, my_run.analysis_run_id)
        print msg

        raise ValueError(msg)
    else:
        session = connection.Session()

        my_run.run_status = RUN_STATUS_IN_PROGRESS
        now = datetime.datetime.now()
        my_run.last_updated_date = now
        my_run.run_start_date = now

        session.add(my_run)
        session.commit()
        session.close()


def set_completed(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_COMPLETED.
    Only possible if the current status is RUN_STATUS_IN_PROGRESS.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update

    Raises:
        ValueError: If the analysis run is not in the correct state.
    """
    if my_run.run_status != RUN_STATUS_IN_PROGRESS:

        raise ValueError("Wrong run status - {}, Only an In Progress run can be Finished, runID: {}".\
                         format(my_run.run_status, my_run.analysis_run_id))
    else:

        session = connection.Session()

        my_run.run_status = RUN_STATUS_COMPLETED

        now = datetime.datetime.now()
        my_run.last_updated_date = now
        my_run.run_end_date = now

        session.add(my_run)
        session.commit()
        session.close()


def set_error(my_run):
    """
    Set the status of a given analysis run to RUN_STATUS_ERROR.

    Args:
        my_run (AnalysisRun): AnalysisRun object to update
    """
    session = connection.Session()

    my_run.run_status = RUN_STATUS_ERROR

    now = datetime.datetime.now()
    my_run.last_updated_date = now

    session.add(my_run)
    session.commit()
    session.close()

def get_number_of_runs_with_status(analysis_id, run_status):
    session = connection.Session()
    num_runs = session.query(AnalysisRun).filter(and_(AnalysisRun.analysis_id == analysis_id, AnalysisRun.run_status == run_status)).count()
    session.close()

    return num_runs
{'fraction_non_alphanumeric': 0.0740375771909079, 'fraction_numerical': 0.0007883326763894363, 'mean_word_length': 2.912361860704189, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4815498', 'n_tokens_mistral': 4761, 'n_tokens_neox': 4578, 'n_words': 1138}