def edit_configuration( cls, company_id: str, task_id: str, configuration: Sequence[Configuration], replace_configuration: bool, force: bool, ) -> int: with TimingContext("mongo", "edit_configuration"): task = get_task_for_update(company_id=company_id, task_id=task_id, force=force) update_cmds = dict() configuration = { ParameterKeyEscaper.escape(c.name): ConfigurationItem(**c.to_struct()) for c in configuration } if replace_configuration: update_cmds["set__configuration"] = configuration else: for name, value in configuration.items(): update_cmds[ f"set__configuration__{mongoengine_safe(name)}"] = value return update_task(task, update_cmds=update_cmds)
def edit_params( cls, company_id: str, task_id: str, hyperparams: Sequence[HyperParamItem], replace_hyperparams: str, force: bool, ) -> int: with TimingContext("mongo", "edit_hyperparams"): properties_only = cls._normalize_params(hyperparams) task = get_task_for_update( company_id=company_id, task_id=task_id, allow_all_statuses=properties_only, force=force, ) update_cmds = dict() hyperparams = cls._db_dicts_from_list(hyperparams) if replace_hyperparams == ReplaceHyperparams.all: update_cmds["set__hyperparams"] = hyperparams elif replace_hyperparams == ReplaceHyperparams.section: for section, value in hyperparams.items(): update_cmds[ f"set__hyperparams__{mongoengine_safe(section)}"] = value else: for section, section_params in hyperparams.items(): for name, value in section_params.items(): update_cmds[ f"set__hyperparams__{section}__{mongoengine_safe(name)}"] = value return update_task(task, update_cmds=update_cmds, set_last_update=not properties_only)
def add_or_update_artifacts( cls, company_id: str, task_id: str, artifacts: Sequence[ApiArtifact], force: bool, ) -> int: with TimingContext("mongo", "update_artifacts"): task = get_task_for_update( company_id=company_id, task_id=task_id, force=force, ) artifacts = { get_artifact_id(a): Artifact(**a) for a in (api_artifact.to_struct() for api_artifact in artifacts) } update_cmds = { f"set__execution__artifacts__{mongoengine_safe(name)}": value for name, value in artifacts.items() } return update_task(task, update_cmds=update_cmds)
def delete_configuration(cls, company_id: str, task_id: str, configuration: Sequence[str], force: bool) -> int: with TimingContext("mongo", "delete_configuration"): task = get_task_for_update(company_id=company_id, task_id=task_id, force=force) delete_cmds = { f"unset__configuration__{ParameterKeyEscaper.escape(name)}": 1 for name in set(configuration) } return update_task(task, update_cmds=delete_cmds)
def set_requirements(call: APICall, company_id, req_model: SetRequirementsRequest): requirements = req_model.requirements with translate_errors_context(): task = TaskBLL.get_task_with_access( req_model.task, company_id=company_id, only=("status", "script"), requires_write_access=True, ) if not task.script: raise errors.bad_request.MissingTaskFields( "Task has no script field", task=task.id ) res = update_task(task, update_cmds=dict(script__requirements=requirements)) call.result.data_model = UpdateResponse(updated=res) if res: call.result.data_model.fields = {"script.requirements": requirements}
def delete_params( cls, company_id: str, task_id: str, hyperparams: Sequence[HyperParamKey], force: bool, ) -> int: with TimingContext("mongo", "delete_hyperparams"): properties_only = cls._normalize_params(hyperparams) task = get_task_for_update( company_id=company_id, task_id=task_id, allow_all_statuses=properties_only, force=force, ) with_param, without_param = iterutils.partition( hyperparams, key=lambda p: bool(p.name)) sections_to_delete = {p.section for p in without_param} delete_cmds = { f"unset__hyperparams__{ParameterKeyEscaper.escape(section)}": 1 for section in sections_to_delete } for item in with_param: section = ParameterKeyEscaper.escape(item.section) if item.section in sections_to_delete: raise errors.bad_request.FieldsConflict( "Cannot delete section field if the whole section was scheduled for deletion" ) name = ParameterKeyEscaper.escape(item.name) delete_cmds[f"unset__hyperparams__{section}__{name}"] = 1 return update_task(task, update_cmds=delete_cmds, set_last_update=not properties_only)
def delete_artifacts( cls, company_id: str, task_id: str, artifact_ids: Sequence[ArtifactId], force: bool, ) -> int: with TimingContext("mongo", "delete_artifacts"): task = get_task_for_update( company_id=company_id, task_id=task_id, force=force, ) artifact_ids = [ get_artifact_id(a) for a in (artifact_id.to_struct() for artifact_id in artifact_ids) ] delete_cmds = { f"unset__execution__artifacts__{id_}": 1 for id_ in set(artifact_ids) } return update_task(task, update_cmds=delete_cmds)