Beispiel #1
0
def delete_experiment(uuid, project_id):
    """Delete an experiment in our database and in the object storage.

    Args:
        uuid (str): the experiment uuid to look for in our database.
        project_id (str): the project uuid.

    Returns:
        The deletion result.
    """
    raise_if_project_does_not_exist(project_id)

    experiment = Experiment.query.get(uuid)

    if experiment is None:
        raise NOTFOUND

    # remove compare results
    CompareResult.query.filter(CompareResult.experiment_id == uuid).delete()
    # remove operators
    Operator.query.filter(Operator.experiment_id == uuid).delete()

    db_session.delete(experiment)
    db_session.commit()

    fix_positions(project_id=experiment.project_id)

    prefix = join("experiments", uuid)
    remove_objects(prefix=prefix)

    return {"message": "Experiment deleted"}
Beispiel #2
0
def delete_operator(uuid, project_id, experiment_id):
    """Delete an operator in our database.
    Args:
        uuid (str): the operator uuid to look for in our database.
        project_id (str): the project uuid.
        experiment_id (str): the experiment uuid.
    Returns:
        The deletion result.
    """
    raise_if_project_does_not_exist(project_id)
    raise_if_experiment_does_not_exist(experiment_id)

    operator = Operator.query.get(uuid)

    if operator is None:
        raise NotFound("The specified operator does not exist")

    # check if other operators contains the operator being deleted
    # in dependencies and remove this operator from dependencies
    operators = db_session.query(Operator) \
        .filter_by(experiment_id=experiment_id) \
        .filter(Operator.uuid != uuid)\
        .all()
    for op in operators:
        if uuid in op.dependencies:
            dependencies = op.dependencies.remove(uuid)
            if dependencies is None:
                dependencies = []
            update_operator(op.uuid, project_id, experiment_id, dependencies=dependencies)

    db_session.delete(operator)
    db_session.commit()

    return {"message": "Operator deleted"}
Beispiel #3
0
def update_compare_result(uuid, project_id, **kwargs):
    """Updates a compare result in our database.
    Args:
        uuid (str): the a compare result uuid to look for in our database.
        project_id (str): the project uuid.
        **kwargs: arbitrary keyword arguments.
    Returns:
        The compare result info.
    """
    raise_if_project_does_not_exist(project_id)

    compare_result = CompareResult.query.get(uuid)

    if compare_result is None:
        raise NOT_FOUND

    experiment_id = kwargs.get("experiment_id", None)
    if experiment_id:
        raise_if_experiment_does_not_exist(experiment_id)

    data = {"updated_at": datetime.utcnow()}
    data.update(kwargs)

    try:
        db_session.query(CompareResult).filter_by(uuid=uuid).update(data)
        db_session.commit()
    except (InvalidRequestError, ProgrammingError) as e:
        raise BadRequest(str(e))

    return compare_result.as_dict()
Beispiel #4
0
def create_operator(project_id, experiment_id, task_id=None,
                    parameters=None, dependencies=None, position_x=None,
                    position_y=None, **kwargs):
    """
    Creates a new operator in our database.
    The new operator is added to the end of the operator list.
    Args:
        project_id (str): the project uuid.
        experiment_id (str): the experiment uuid.
        task_id (str): the task uuid.
        parameters (dict): the parameters dict.
        dependencies (list): the dependencies array.
        position_x (float): position x.
        position_y (float): position y.
    Returns:
        The operator info.
    """
    raise_if_project_does_not_exist(project_id)
    raise_if_experiment_does_not_exist(experiment_id)

    if not isinstance(task_id, str):
        raise BadRequest("taskId is required")

    try:
        raise_if_task_does_not_exist(task_id)
    except NotFound as e:
        raise BadRequest(e.description)

    if parameters is None:
        parameters = {}

    raise_if_parameters_are_invalid(parameters)

    if dependencies is None:
        dependencies = []

    raise_if_dependencies_are_invalid(project_id, experiment_id, dependencies)

    operator = Operator(uuid=uuid_alpha(),
                        experiment_id=experiment_id,
                        task_id=task_id,
                        dependencies=dependencies,
                        parameters=parameters,
                        position_x=position_x,
                        position_y=position_y)
    db_session.add(operator)
    db_session.commit()

    check_status(operator)

    return operator.as_dict()
Beispiel #5
0
def create_compare_result(project_id=None):
    """Creates a new compare result in our database.
    Args:
        project_id (str): the project uuid.
    Returns:
        The compare result info.
    """
    raise_if_project_does_not_exist(project_id)

    compare_result = CompareResult(uuid=uuid_alpha(), project_id=project_id)
    db_session.add(compare_result)
    db_session.commit()

    return compare_result.as_dict()
Beispiel #6
0
def list_compare_results(project_id):
    """Lists all compare results under a project.
    Args:
        project_id (str): the project uuid.
    Returns:
        A list of all compare results.
    """
    raise_if_project_does_not_exist(project_id)

    compare_results = db_session.query(CompareResult) \
        .filter_by(project_id=project_id) \
        .order_by(CompareResult.created_at.asc()) \
        .all()

    return [compare_result.as_dict() for compare_result in compare_results]
Beispiel #7
0
def list_experiments(project_id):
    """Lists all experiments under a project.

    Args:
        project_id (str): the project uuid.

    Returns:
        A list of all experiments.
    """
    raise_if_project_does_not_exist(project_id)

    experiments = db_session.query(Experiment) \
        .filter_by(project_id=project_id) \
        .order_by(Experiment.position.asc()) \
        .all()
    return [experiment.as_dict() for experiment in experiments]
Beispiel #8
0
def get_experiment(uuid, project_id):
    """Details an experiment from our database.

    Args:
        uuid (str): the experiment uuid to look for in our database.
        project_id (str): the project uuid.

    Returns:
        The experiment info.
    """
    raise_if_project_does_not_exist(project_id)

    experiment = Experiment.query.get(uuid)

    if experiment is None:
        raise NOTFOUND

    return experiment.as_dict()
Beispiel #9
0
def delete_compare_result(uuid, project_id):
    """Delete a compare result in our database.
    Args:
        uuid (str): the compare result uuid to look for in our database.
        project_id (str): the project uuid.
    Returns:
        The deletion result.
    """
    raise_if_project_does_not_exist(project_id)

    compare_result = CompareResult.query.get(uuid)

    if compare_result is None:
        raise NOT_FOUND

    db_session.delete(compare_result)
    db_session.commit()

    return {"message": "Compare result deleted"}
Beispiel #10
0
def list_operators(project_id, experiment_id):
    """Lists all operators under an experiment.

    Args:
        project_id (str): the project uuid.
        experiment_id (str): the experiment uuid.

    Returns:
        A list of all operator.
    """
    raise_if_project_does_not_exist(project_id)
    raise_if_experiment_does_not_exist(experiment_id)

    operators = db_session.query(Operator) \
        .filter_by(experiment_id=experiment_id) \
        .all()

    response = []
    for operator in operators:
        check_status(operator)
        response.append(operator.as_dict())

    return response
Beispiel #11
0
def update_operator(uuid, project_id, experiment_id, **kwargs):
    """Updates an operator in our database and adjusts the position of others.
    Args:
        uuid (str): the operator uuid to look for in our database.
        project_id (str): the project uuid.
        experiment_id (str): the experiment uuid.
        **kwargs: arbitrary keyword arguments.
    Returns:
        The operator info.
    """
    raise_if_project_does_not_exist(project_id)
    raise_if_experiment_does_not_exist(experiment_id)

    operator = Operator.query.get(uuid)

    if operator is None:
        raise NotFound("The specified operator does not exist")

    raise_if_parameters_are_invalid(kwargs.get("parameters", {}))

    dependencies = kwargs.get("dependencies")
    if dependencies is not None:
        raise_if_dependencies_are_invalid(project_id, experiment_id, dependencies, operator_id=uuid)

    data = {"updated_at": datetime.utcnow()}
    data.update(kwargs)

    try:
        db_session.query(Operator).filter_by(uuid=uuid).update(data)
        db_session.commit()
    except (InvalidRequestError, ProgrammingError) as e:
        raise BadRequest(str(e))

    check_status(operator)

    return operator.as_dict()
Beispiel #12
0
def create_experiment(name=None, project_id=None, copy_from=None):
    """Creates a new experiment in our database and adjusts the position of others.

    The new experiment is added to the end of the experiment list.

    Args:
        name (str): the experiment name.
        project_id (str): the project uuid.
        copy_from (str): the copy_from uuid.
    Returns:
        The experiment info.
    """
    raise_if_project_does_not_exist(project_id)

    if not isinstance(name, str):
        raise BadRequest("name is required")

    check_experiment_name = db_session.query(Experiment)\
        .filter(Experiment.project_id == project_id)\
        .filter(Experiment.name == name)\
        .first()
    if check_experiment_name:
        raise BadRequest("an experiment with that name already exists")

    experiment = Experiment(uuid=uuid_alpha(), name=name, project_id=project_id)
    db_session.add(experiment)
    db_session.commit()

    if copy_from:
        try:
            experiment_find = find_by_experiment_id(experiment_id=copy_from)

            source_operators = {}
            for source_operator in experiment_find['operators']:

                source_dependencies = source_operator.dependencies

                kwargs = {
                    "task_id": source_operator.task_id,
                    "parameters": source_operator.parameters,
                    "dependencies": [],
                    "position_x": source_operator.position_x,
                    "position_y": source_operator.position_y
                }
                operator = create_operator(project_id, experiment.uuid, **kwargs)

                source_operators[source_operator.uuid] = {
                    "copy_uuid": operator["uuid"],
                    "dependencies": source_dependencies
                }

            # update dependencies on new operators
            for _, value in source_operators.items():
                dependencies = [source_operators[d]['copy_uuid'] for d in value['dependencies']]
                update_operator(value['copy_uuid'], project_id, experiment.uuid, dependencies=dependencies)

        except NotFound:
            delete_experiment(experiment.uuid, project_id)
            raise BadRequest('Source experiment does not exist')

    fix_positions(project_id=project_id,
                  experiment_id=experiment.uuid,
                  new_position=sys.maxsize)  # will add to end of list

    return experiment.as_dict()
Beispiel #13
0
def update_experiment(uuid, project_id, **kwargs):
    """Updates an experiment in our database and adjusts the position of others.

    Args:
        uuid (str): the experiment uuid to look for in our database.
        project_id (str): the project uuid.
        **kwargs: arbitrary keyword arguments.
    Returns:
        The experiment info.
    """
    raise_if_project_does_not_exist(project_id)

    experiment = Experiment.query.get(uuid)

    if experiment is None:
        raise NOTFOUND

    if "name" in kwargs:
        name = kwargs["name"]
        if name != experiment.name:
            check_experiment_name = db_session.query(Experiment)\
                .filter(Experiment.project_id == project_id)\
                .filter(Experiment.name == name)\
                .first()
            if check_experiment_name:
                raise BadRequest("an experiment with that name already exists")

    # updates operators
    if "template_id" in kwargs:
        template_id = kwargs["template_id"]
        del kwargs["template_id"]
        template = Template.query.get(template_id)

        if template is None:
            raise BadRequest("The specified template does not exist")

        # remove operators
        Operator.query.filter(Operator.experiment_id == uuid).delete()

        # save the last operator id created to create dependency on next operator
        last_operator_id = None
        for task_id in template.tasks:
            operator_id = uuid_alpha()
            dependencies = []
            if last_operator_id is not None:
                dependencies = [last_operator_id]
            objects = [
                Operator(uuid=operator_id,
                         experiment_id=uuid,
                         task_id=task_id,
                         dependencies=dependencies)
            ]
            db_session.bulk_save_objects(objects)
            last_operator_id = operator_id

    data = {"updated_at": datetime.utcnow()}
    data.update(kwargs)

    try:
        db_session.query(Experiment).filter_by(uuid=uuid).update(data)
        db_session.commit()
    except (InvalidRequestError, ProgrammingError) as e:
        raise BadRequest(str(e))

    fix_positions(project_id=experiment.project_id,
                  experiment_id=experiment.uuid,
                  new_position=experiment.position)

    return experiment.as_dict()