def fix_positions(project_id, deployment_id=None, new_position=None): """Reorders the deployments in a project when a deployment is updated/deleted. Args: project_id (str): the project uuid. deployment_id (str): the deployment uuid. new_position (int): the position where the experiment is shown. """ other_deployments = db_session.query(Deployment) \ .filter_by(project_id=project_id) \ .filter(Deployment.uuid != deployment_id)\ .order_by(Deployment.position.asc())\ .all() if deployment_id is not None: deployment = Deployment.query.get(deployment_id) other_deployments.insert(new_position, deployment) for index, deployment in enumerate(other_deployments): data = {"position": index} is_last = (index == len(other_deployments) - 1) # if deployment_id WAS NOT informed, then set the higher position as is_active=True if deployment_id is None and is_last: data["is_active"] = True # if deployment_id WAS informed, then set experiment.is_active=True elif deployment_id is not None and deployment_id == deployment.uuid: data["is_active"] = True else: data["is_active"] = False db_session.query(Deployment).filter_by( uuid=deployment.uuid).update(data) db_session.commit()
def update_operator(uuid, project_id, deployment_id, **kwargs): """Updates an operator in our database. Args: uuid (str): the operator uuid to look for in our database. project_id (str): the project uuid. deployment_id (str): the deployment uuid. **kwargs: arbitrary keyword arguments. Returns: The operator info. """ raise_if_project_does_not_exist(project_id) raise_if_deployment_does_not_exist(deployment_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", {})) 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)) return operator.as_dict()
def get_dataset_name(experiment_id, operator_id,): """Retrieves a dataset name from experiment. Args: experiment_id(str): the experiment uuid operator_id(str): the operator uuid Returns: Dataset name """ raise_if_experiment_does_not_exist(experiment_id) operator = Operator.query.get(operator_id) if operator is None: raise NotFound("The specified operator does not exist") # get dataset name dataset = operator.parameters.get('dataset') if dataset is None: # try to find dataset name in other operators operators = db_session.query(Operator) \ .filter_by(experiment_id=experiment_id) \ .filter(Operator.uuid != operator_id) \ .all() for operator in operators: dataset = operator.parameters.get('dataset') if dataset: break if dataset is None: raise NotFound() return dataset
def raise_if_deployment_does_not_exist(deployment_id): """Raises an exception if the specified deployment does not exist. Args: deployment_id (str): the expdeploymenteriment uuid. """ exists = db_session.query(Deployment.uuid) \ .filter_by(uuid=deployment_id) \ .scalar() is not None if not exists: raise NotFound("The specified deployment does not exist")
def raise_if_task_does_not_exist(task_id): """Raises an exception if the specified task does not exist. Args: task_id (str): the task uuid. """ exists = db_session.query(Task.uuid) \ .filter_by(uuid=task_id) \ .scalar() is not None if not exists: raise NotFound("The specified task does not exist")
def raise_if_project_does_not_exist(project_id): """Raises an exception if the specified project does not exist. Args: project_id (str): the project uuid. """ exists = db_session.query(Project.uuid) \ .filter_by(uuid=project_id) \ .scalar() is not None if not exists: raise NotFound("The specified project does not exist")
def update_deployment(uuid, project_id, **kwargs): """Updates a deployment in our database and adjusts the position of others. Args: uuid (str): the deployment uuid to look for in our database. project_id (str): the project uuid. **kwargs: arbitrary keyword arguments. Returns: The deployment info. """ raise_if_project_does_not_exist(project_id) deployment = Deployment.query.get(uuid) if deployment is None: raise NOT_FOUND if "name" in kwargs: name = kwargs["name"] if name != deployment.name: check_deployment_name = db_session.query(Deployment)\ .filter(Deployment.project_id == project_id)\ .filter(Deployment.name == name)\ .first() if check_deployment_name: raise BadRequest("a deployment with that name already exists") data = {"updated_at": datetime.utcnow()} data.update(kwargs) try: db_session.query(Deployment).filter_by(uuid=uuid).update(data) db_session.commit() except (InvalidRequestError, ProgrammingError) as e: raise BadRequest(str(e)) fix_positions(project_id=deployment.project_id, deployment_id=deployment.uuid, new_position=deployment.position) return deployment.as_dict()
def list_deployments(project_id): """Lists all deployments under a project. Args: project_id (str): the project uuid. Returns: A list of all deployments. """ raise_if_project_does_not_exist(project_id) deployments = db_session.query(Deployment) \ .filter_by(project_id=project_id) \ .order_by(Deployment.position.asc()) \ .all() return [deployment.as_dict() for deployment in deployments]
def create_deployment(experiment_id=None, is_active=None, name=None, operators=None, position=None, project_id=None, status=None): """Creates a new deployment in our database and adjusts the position of others. Args: experiment_id (str): the experiment uuid. is_active (bool): if deployment is active. name (str): the deployment name. operators (list): the deployment operators. position (int): the deployment position. project_id (str): the project uuid. status (str): the deployment status. Returns: The deployment info. """ raise_if_project_does_not_exist(project_id) raise_if_experiment_does_not_exist(experiment_id) if not isinstance(name, str): raise BadRequest("name is required") if status and status not in VALID_STATUS: raise BadRequest("invalid status") check_deployment_name = db_session.query(Deployment)\ .filter(Deployment.project_id == project_id)\ .filter(Deployment.name == name)\ .first() if check_deployment_name: raise BadRequest("a deployment with that name already exists") deployment = Deployment(uuid=uuid_alpha(), experiment_id=experiment_id, is_active=is_active, name=name, project_id=project_id, status=status) db_session.add(deployment) if operators and len(operators) > 0: for operator in operators: create_operator(deployment_id=deployment.uuid, project_id=project_id, task_id=operator.get('taskId'), parameters=operator.get('parameters'), dependencies=operator.get('dependencies'), position_x=operator.get('positionX'), position_y=operator.get('positionY')) db_session.commit() if position is None: position = sys.maxsize # will add to end of list fix_positions(project_id=project_id, deployment_id=deployment.uuid, new_position=position) return deployment.as_dict()