From 3773fbf649c12ce60081cbc68d7f0b9362074965 Mon Sep 17 00:00:00 2001 From: YolanFery Date: Fri, 20 Dec 2024 11:05:34 +0100 Subject: [PATCH 1/5] Get config from previous version --- hexa/pipelines/models.py | 45 +++++++++--- hexa/pipelines/tests/test_models.py | 104 ++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 8 deletions(-) diff --git a/hexa/pipelines/models.py b/hexa/pipelines/models.py index a0d7d626e..ebbba2a19 100644 --- a/hexa/pipelines/models.py +++ b/hexa/pipelines/models.py @@ -340,6 +340,37 @@ def is_schedulable(self): elif self.type == PipelineType.ZIPFILE: return self.last_version and self.last_version.is_schedulable + def get_config_from_previous_version(self, new_parameters: dict): + """ + Get the config from the previous version of the pipeline considering only common parameters between the new and the previous version. + """ + previous_config_from_common_parameter = {} + if self.last_version: + previous_parameters = self.last_version.parameters + common_parameters = [ + param for param in new_parameters if param in previous_parameters + ] + previous_config_from_common_parameter = { + parameter["code"]: value + for parameter in common_parameters + if ( + value := self.last_version.config.get( + parameter["code"], parameter.get("default") + ) + ) + is not None + } + return { + parameter["code"]: value + for parameter in new_parameters + if ( + value := previous_config_from_common_parameter.get( + parameter["code"], parameter.get("default") + ) + ) + is not None + } + def upload_new_version( self, user: User, @@ -354,14 +385,12 @@ def upload_new_version( if not user.has_perm("pipelines.update_pipeline", self): raise PermissionDenied - if config is None: - # No default configuration has been provided, let's take the default values from the parameters - # In the future, we'll use the one from the last version - config = { - parameter["code"]: parameter["default"] - for parameter in parameters - if parameter.get("default") is not None - } + config = ( + config + if config is not None + else self.get_config_from_previous_version(parameters) + ) + version = PipelineVersion( user=user, pipeline=self, diff --git a/hexa/pipelines/tests/test_models.py b/hexa/pipelines/tests/test_models.py index 1a1e44067..770949641 100644 --- a/hexa/pipelines/tests/test_models.py +++ b/hexa/pipelines/tests/test_models.py @@ -184,3 +184,107 @@ def test_mail_run_recipients_mail_all_recipients(self): mail_run_recipients(run) self.assertEqual(len(mail.outbox), 3) + + def test_get_config_from_previous_version(self): + pipeline = Pipeline.objects.create( + name="Test pipeline", + ) + pipeline.upload_new_version( + user=self.USER_ADMIN, + zipfile=b"", + parameters=[ + { + "choices": None, + "code": "param_1", + "default": None, + "help": None, + "multiple": False, + "name": "Param 1", + "required": True, + "type": "int", + }, + { + "choices": None, + "code": "param_2", + "default": None, + "help": None, + "multiple": False, + "name": "Param 2", + "required": True, + "type": "int", + }, + ], + name="Version 1", + config={"param_1": 43, "param_2": 42}, + ) + self.assertEqual( + {"param_1": 43, "param_2": 42}, + pipeline.last_version.config, + "Initial config", + ) + pipeline.upload_new_version( + user=self.USER_ADMIN, + zipfile=b"", + parameters=[ + { + "choices": None, + "code": "param_1", + "default": None, + "help": None, + "multiple": False, + "name": "Param 1", + "required": True, + "type": "int", + }, + { + "choices": None, + "code": "param_3", + "default": None, + "help": None, + "multiple": False, + "name": "Param 3", + "required": True, + "type": "int", + }, + ], + name="Version 2", + config=None, + ) + self.assertEqual( + {"param_1": 43}, + pipeline.last_version.config, + "Config from previous version with a partial change of parameters", + ) + pipeline.upload_new_version( + user=self.USER_ADMIN, + zipfile=b"", + parameters=[ + { + "choices": None, + "code": "param_1", + "default": 45, + "help": None, + "multiple": False, + "name": "Param 1", + "required": True, + "type": "int", + }, + { + "choices": None, + "code": "param_2", + "default": 46, + "help": None, + "multiple": False, + "name": "Param 2", + "required": True, + "type": "int", + }, + ], + name="Version 3", + config=None, + ) + self.assertEqual( + {"param_1": 45, "param_2": 46}, + pipeline.last_version.config, + "Config from previous version with a change of default values", + ) From b9af3a694302b54a3ec806d272edc914d4a3c702 Mon Sep 17 00:00:00 2001 From: YolanFery Date: Fri, 20 Dec 2024 15:53:16 +0100 Subject: [PATCH 2/5] Syntax --- hexa/pipelines/models.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/hexa/pipelines/models.py b/hexa/pipelines/models.py index ebbba2a19..1c3da7379 100644 --- a/hexa/pipelines/models.py +++ b/hexa/pipelines/models.py @@ -385,11 +385,7 @@ def upload_new_version( if not user.has_perm("pipelines.update_pipeline", self): raise PermissionDenied - config = ( - config - if config is not None - else self.get_config_from_previous_version(parameters) - ) + config = config or self.get_config_from_previous_version(parameters) version = PipelineVersion( user=user, From 75c60899f5da04a580eebf4b444e21b86239835a Mon Sep 17 00:00:00 2001 From: YolanFery Date: Mon, 23 Dec 2024 12:05:10 +0100 Subject: [PATCH 3/5] Renames --- hexa/pipelines/models.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/hexa/pipelines/models.py b/hexa/pipelines/models.py index 1c3da7379..64b2112de 100644 --- a/hexa/pipelines/models.py +++ b/hexa/pipelines/models.py @@ -342,30 +342,31 @@ def is_schedulable(self): def get_config_from_previous_version(self, new_parameters: dict): """ - Get the config from the previous version of the pipeline considering only common parameters between the new and the previous version. + Get the config from the previous version of the pipeline considering only overlapping parameters between the new and the previous version. """ - previous_config_from_common_parameter = {} + previous_config_from_overlapping_parameter = {} if self.last_version: previous_parameters = self.last_version.parameters - common_parameters = [ + overlapping_parameters = [ param for param in new_parameters if param in previous_parameters ] - previous_config_from_common_parameter = { - parameter["code"]: value - for parameter in common_parameters + previous_config_from_overlapping_parameter = { + overlapping_parameter["code"]: value + for overlapping_parameter in overlapping_parameters if ( value := self.last_version.config.get( - parameter["code"], parameter.get("default") + overlapping_parameter["code"], + overlapping_parameter.get("default"), ) ) is not None } return { - parameter["code"]: value - for parameter in new_parameters + new_parameter["code"]: value + for new_parameter in new_parameters if ( - value := previous_config_from_common_parameter.get( - parameter["code"], parameter.get("default") + value := previous_config_from_overlapping_parameter.get( + new_parameter["code"], new_parameter.get("default") ) ) is not None From 5f7573fc55aa29b4122a83953c378d2eab31b045 Mon Sep 17 00:00:00 2001 From: YolanFery Date: Mon, 23 Dec 2024 12:06:50 +0100 Subject: [PATCH 4/5] Renames --- hexa/pipelines/models.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hexa/pipelines/models.py b/hexa/pipelines/models.py index 64b2112de..1d1f2b80c 100644 --- a/hexa/pipelines/models.py +++ b/hexa/pipelines/models.py @@ -348,7 +348,9 @@ def get_config_from_previous_version(self, new_parameters: dict): if self.last_version: previous_parameters = self.last_version.parameters overlapping_parameters = [ - param for param in new_parameters if param in previous_parameters + new_parameter + for new_parameter in new_parameters + if new_parameter in previous_parameters ] previous_config_from_overlapping_parameter = { overlapping_parameter["code"]: value From 47bd59685475e7ac8e8c6dddf783a26080fd7827 Mon Sep 17 00:00:00 2001 From: YolanFery Date: Mon, 23 Dec 2024 12:07:31 +0100 Subject: [PATCH 5/5] Renames --- hexa/pipelines/models.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hexa/pipelines/models.py b/hexa/pipelines/models.py index 1d1f2b80c..aaf7ad507 100644 --- a/hexa/pipelines/models.py +++ b/hexa/pipelines/models.py @@ -344,7 +344,7 @@ def get_config_from_previous_version(self, new_parameters: dict): """ Get the config from the previous version of the pipeline considering only overlapping parameters between the new and the previous version. """ - previous_config_from_overlapping_parameter = {} + previous_config_from_overlapping_parameters = {} if self.last_version: previous_parameters = self.last_version.parameters overlapping_parameters = [ @@ -352,7 +352,7 @@ def get_config_from_previous_version(self, new_parameters: dict): for new_parameter in new_parameters if new_parameter in previous_parameters ] - previous_config_from_overlapping_parameter = { + previous_config_from_overlapping_parameters = { overlapping_parameter["code"]: value for overlapping_parameter in overlapping_parameters if ( @@ -367,7 +367,7 @@ def get_config_from_previous_version(self, new_parameters: dict): new_parameter["code"]: value for new_parameter in new_parameters if ( - value := previous_config_from_overlapping_parameter.get( + value := previous_config_from_overlapping_parameters.get( new_parameter["code"], new_parameter.get("default") ) )