From d0faa0e933f68f08d937b1d70f1eabbe0a94c8d2 Mon Sep 17 00:00:00 2001
From: stedif <stefano.difrischia@inaf.it>
Date: Fri, 25 Feb 2022 13:21:50 +0100
Subject: [PATCH] L2SS-404: fix bugs after ticket review

---
 .../test/toolkit/test_archiver_util.py        | 10 +++----
 .../tangostationcontrol/toolkit/archiver.py   | 30 +++++++++----------
 .../toolkit/archiver_config/lofar2.json       | 14 ++++-----
 .../toolkit/archiver_util.py                  | 26 ++++++++--------
 4 files changed, 40 insertions(+), 40 deletions(-)

diff --git a/tangostationcontrol/tangostationcontrol/test/toolkit/test_archiver_util.py b/tangostationcontrol/tangostationcontrol/test/toolkit/test_archiver_util.py
index ab77f4b20..9936046cf 100644
--- a/tangostationcontrol/tangostationcontrol/test/toolkit/test_archiver_util.py
+++ b/tangostationcontrol/tangostationcontrol/test/toolkit/test_archiver_util.py
@@ -33,10 +33,10 @@ class TestArchiverUtil(base.TestCase):
     def test_get_parameters_from_attribute(self):
         """Test if the attribute archiving parameters are correctly retrieved from the JSON config file"""
         self.assertIsNotNone(self.config_dict)
-        suffixes = self.config_dict.get('_global_suffixes')
+        suffixes = self.config_dict['_global_suffixes']
         archive_period, event_period, abs_change, rel_change = get_parameters_from_attribute(self.device_name, self.attribute_name, 
                                                                     suffixes, self.config_dict)
-        self.assertEqual(archive_period,int(suffixes[2].get('archive_period')))
-        self.assertEqual(event_period,int(suffixes[2].get('event_period')))
-        self.assertEqual(abs_change,int(suffixes[2].get('abs_change')))
-        self.assertEqual(rel_change,suffixes[2].get('rel_change'))
+        self.assertEqual(archive_period,int(suffixes[2]['archive_period']))
+        self.assertEqual(event_period,int(suffixes[2]['event_period']))
+        self.assertEqual(abs_change,int(suffixes[2]['abs_change']))
+        self.assertEqual(rel_change,suffixes[2]['rel_change'])
diff --git a/tangostationcontrol/tangostationcontrol/toolkit/archiver.py b/tangostationcontrol/tangostationcontrol/toolkit/archiver.py
index 4287290a7..f4bd05928 100644
--- a/tangostationcontrol/tangostationcontrol/toolkit/archiver.py
+++ b/tangostationcontrol/tangostationcontrol/toolkit/archiver.py
@@ -105,26 +105,26 @@ class Archiver():
         Apply the customized strategy defined by the given archiver configuration.
         """
         # Set global development env variables
-        var_dict = config_dict.get('_global_variables')
-        self.dev_polling_time = int(var_dict.get('development_polling_time'))
-        abs_change = var_dict.get('development_archive_abs_change')
-        rel_change = var_dict.get('development_archive_rel_change')
-        self.dev_archive_abs_change = None if abs_change == 'None' else int(abs_change) 
-        self.dev_archive_rel_change = None if rel_change == 'None' else int(rel_change)
-        self.dev_archive_period = int(var_dict.get('development_archive_period')) 
-        self.dev_event_period = int(var_dict.get('development_event_period'))
-        self.dev_strategy = var_dict.get('development_strategy')
-        suffixes = config_dict.get('_global_suffixes')  # suffixes list of attributes that need to be archived
+        var_dict = config_dict['_global_variables']
+        self.dev_polling_time = int(var_dict['development_polling_time'])
+        abs_change = var_dict['development_archive_abs_change']
+        rel_change = var_dict['development_archive_rel_change']
+        self.dev_archive_abs_change = None and int(abs_change)
+        self.dev_archive_rel_change = None and int(rel_change)
+        self.dev_archive_period = int(var_dict['development_archive_period']) 
+        self.dev_event_period = int(var_dict['development_event_period'])
+        self.dev_strategy = var_dict['development_strategy']
+        suffixes = config_dict['_global_suffixes']  # suffixes list of attributes that need to be archived
         # Set devices archiving
-        env_dict = config_dict.get('devices')
+        env_dict = config_dict['devices']
         for device in env_dict:
             try:
-                dev_env = str(env_dict[device].get('environment')) # Get device environment
+                dev_env = str(env_dict[device]['environment']) # Get device environment
                 if dev_env == 'development':    # DEV environment -> all attributes are excluded by default
                     include_node = env_dict[device].get('include',[])
                     include_att_list = []
                     for a in include_node:
-                        include_att_list.append(a.get('attribute'))
+                        include_att_list.append(a['attribute'])
                     # Add attributes with defined suffixes
                     include_att_list.extend(get_attributes_from_suffix(device,suffixes))
                     self.remove_attributes_by_device(device, exclude=include_att_list)
@@ -192,8 +192,8 @@ class Archiver():
             self.cm.write_attribute('SetStrategy', strategy)
             self.cm.write_attribute('SetPollingPeriod', polling_period)
             self.cm.write_attribute('SetPeriodEvent', event_period)
-            self.cm.write_attribute('SetAbsoluteEvent', abs_change)
-            if (not(rel_change is None or rel_change=="None")): self.cm.write_attribute('SetRelativeEvent', rel_change)
+            if not(abs_change is None or abs_change=="None"): self.cm.write_attribute('SetAbsoluteEvent', abs_change)
+            if not(rel_change is None or rel_change=="None"): self.cm.write_attribute('SetRelativeEvent', rel_change)
             self.cm.AttributeAdd()
             logger.info(f"Attribute {attribute_name} added to archiving list!")
         except DevFailed as e:
diff --git a/tangostationcontrol/tangostationcontrol/toolkit/archiver_config/lofar2.json b/tangostationcontrol/tangostationcontrol/toolkit/archiver_config/lofar2.json
index 31d65db98..c519686dd 100644
--- a/tangostationcontrol/tangostationcontrol/toolkit/archiver_config/lofar2.json
+++ b/tangostationcontrol/tangostationcontrol/toolkit/archiver_config/lofar2.json
@@ -2,17 +2,17 @@
     "_global_variables": {
         "development_polling_time": "1000",
         "development_archive_abs_change": "1",
-        "development_archive_rel_change": "None",    
+        "development_archive_rel_change": null,    
         "development_archive_period": "10000",
         "development_event_period": "1000",
         "development_strategy": "RUN"
         
     },
     "_global_suffixes":[
-        {"attribute": "_error_R",   "archive_period": "10000", "event_period": "1000",  "abs_change": "1", "rel_change": "None"},
-        {"attribute": "_good_R",    "archive_period": "10000", "event_period": "1000",  "abs_change": "1", "rel_change": "None"},
-        {"attribute": "_mask_RW",   "archive_period": "10000", "event_period": "1000",  "abs_change": "1", "rel_change": "None"},
-        {"attribute": "_version_R", "archive_period": "60000", "event_period": "10000", "abs_change": "1", "rel_change": "None"}
+        {"attribute": "_error_R",   "archive_period": "10000", "event_period": "1000",  "abs_change": "1", "rel_change": null},
+        {"attribute": "_good_R",    "archive_period": "10000", "event_period": "1000",  "abs_change": "1", "rel_change": null},
+        {"attribute": "_mask_RW",   "archive_period": "10000", "event_period": "1000",  "abs_change": "1", "rel_change": null},
+        {"attribute": "_version_R", "archive_period": "60000", "event_period": "10000", "abs_change": "1", "rel_change": null}
     ],
     "devices": {
         "STAT/RECV/1": {
@@ -33,8 +33,8 @@
                 "FPGA_scrap_RW"
             ],
             "include": [
-                {"attribute":"FPGA_temp_R",             "archive_period": "10000", "event_period": "1000",   "abs_change": "1", "rel_change": "None"},
-                {"attribute":"FPGA_firmware_version_R", "archive_period": "60000", "event_period": "10000",  "abs_change": "1", "rel_change": "None"}
+                {"attribute":"FPGA_temp_R",             "archive_period": "10000", "event_period": "1000",   "abs_change": "1", "rel_change": null},
+                {"attribute":"FPGA_firmware_version_R", "archive_period": "60000", "event_period": "10000",  "abs_change": "1", "rel_change": null}
             ]
         },
         "STAT/SST/1": {
diff --git a/tangostationcontrol/tangostationcontrol/toolkit/archiver_util.py b/tangostationcontrol/tangostationcontrol/toolkit/archiver_util.py
index e13d11106..b01c8c51f 100644
--- a/tangostationcontrol/tangostationcontrol/toolkit/archiver_util.py
+++ b/tangostationcontrol/tangostationcontrol/toolkit/archiver_util.py
@@ -85,9 +85,9 @@ def get_attributes_from_suffix(device_name:str, suffixes:list):
     attribute_list = device.get_attribute_list()
     result = []
     for s in suffixes:
-        att_name = s.get('attribute')
+        att_name = s['attribute']
         # Search suffix substring in the device attribute list
-        result.extend([a for a in attribute_list if att_name.lower() in a.lower()])
+        result.extend([a for a in attribute_list if a.lower().endswith(att_name.lower())])
     return result
 
 def get_parameters_from_attribute(device_name:str, attribute_name:str, suffixes: list, config_dict:dict):
@@ -95,19 +95,19 @@ def get_parameters_from_attribute(device_name:str, attribute_name:str, suffixes:
     Return the archiving parameters defined in the configuration file for a certain attribute
     """
     # Search if the attribute parameters are listed inside the device configuration
-    included_attrs = config_dict.get('devices').get(device_name).get('include')
+    included_attrs = config_dict['devices'][device_name]['include']
     for a in included_attrs:
-        if (attribute_name.lower() == a.get('attribute').lower()):
-            archive_period =    int(a.get('archive_period'))
-            event_period =      int(a.get('event_period')) 
-            abs_change =        int(a.get('abs_change'))
-            rel_change =        a.get('rel_change')
+        if attribute_name.lower() == a['attribute'].lower():
+            archive_period =    int(a['archive_period'])
+            event_period =      int(a['event_period']) 
+            abs_change =        a['abs_change'] and int(a['abs_change'])
+            rel_change =        a['rel_change'] and int(a['rel_change'])
             return archive_period, event_period, abs_change, rel_change
     # Search if the archiving parameters are listed inside the global suffixes attributes
     for a in suffixes:
-        if (a.get('attribute').lower() in attribute_name.lower()):
-            archive_period =    int(a.get('archive_period'))
-            event_period =      int(a.get('event_period'))
-            abs_change =        int(a.get('abs_change'))
-            rel_change =        a.get('rel_change')
+        if attribute_name.lower().endswith(a['attribute'].lower()):
+            archive_period =    int(a['archive_period'])
+            event_period =      int(a['event_period']) 
+            abs_change =        a['abs_change'] and int(a['abs_change'])
+            rel_change =        a['rel_change'] and int(a['rel_change'])
             return archive_period, event_period, abs_change, rel_change
-- 
GitLab