From e85d7a38b07740c9b53b141104bde190f778bd14 Mon Sep 17 00:00:00 2001
From: Mattia Mancini <mancini@astron.nl>
Date: Mon, 27 Aug 2018 11:20:57 +0000
Subject: [PATCH] Story OSB-11: major fixes

---
 .../monitoringdb/models/element_error.py      |  3 +-
 .../serializers/component_error.py            |  5 ++-
 .../monitoringdb/serializers/element_error.py |  5 ++-
 .../monitoringdb/serializers/station_tests.py |  2 +-
 .../tests/t_stationtest_parser.py             | 44 +++++++++++++++++++
 .../test_multiple_tileerror_and_rcuerror.csv  |  1 +
 6 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/LCU/Maintenance/DBInterface/monitoringdb/models/element_error.py b/LCU/Maintenance/DBInterface/monitoringdb/models/element_error.py
index 6a481c3b28b..5200e709481 100644
--- a/LCU/Maintenance/DBInterface/monitoringdb/models/element_error.py
+++ b/LCU/Maintenance/DBInterface/monitoringdb/models/element_error.py
@@ -10,7 +10,6 @@ class ElementError(models.Model):
     component_error = models.ForeignKey(ComponentError,
                                         on_delete=models.DO_NOTHING,
                                         related_name='failing_elements')
-    station_test = models.ForeignKey(StationTest, on_delete=models.DO_NOTHING)
 
     type = models.CharField(max_length=50)
-    details = models.ForeignKey(ErrorDetails, on_delete=models.CASCADE)
\ No newline at end of file
+    details = models.ForeignKey(ErrorDetails, on_delete=models.CASCADE, related_name='element_error_details')
\ No newline at end of file
diff --git a/LCU/Maintenance/DBInterface/monitoringdb/serializers/component_error.py b/LCU/Maintenance/DBInterface/monitoringdb/serializers/component_error.py
index 42e0b9c0109..9e4129617a0 100644
--- a/LCU/Maintenance/DBInterface/monitoringdb/serializers/component_error.py
+++ b/LCU/Maintenance/DBInterface/monitoringdb/serializers/component_error.py
@@ -1,10 +1,13 @@
-from rest_framework.serializers import PrimaryKeyRelatedField
+from rest_framework.serializers import PrimaryKeyRelatedField, CharField, IntegerField
 from ..models.component_error import ComponentError
 from .utils import NotNullModelSerializer
 
 
 class ComponentErrorSerializer(NotNullModelSerializer):
     failing_elements = PrimaryKeyRelatedField(many=True, read_only=True)
+    component_id = IntegerField(source='component.component_id')
+    component_type = CharField(source='component.type')
+
     class Meta:
         model = ComponentError
         fields = '__all__'
diff --git a/LCU/Maintenance/DBInterface/monitoringdb/serializers/element_error.py b/LCU/Maintenance/DBInterface/monitoringdb/serializers/element_error.py
index 0819e4922cf..787277e8245 100644
--- a/LCU/Maintenance/DBInterface/monitoringdb/serializers/element_error.py
+++ b/LCU/Maintenance/DBInterface/monitoringdb/serializers/element_error.py
@@ -1,9 +1,10 @@
 from rest_framework.serializers import ModelSerializer
 from ..models.element_error import ElementError
-
+from .error_details import ErrorDetailsSerializer
 
 class ElementErrorSerializer(ModelSerializer):
+
     class Meta:
         model = ElementError
         fields = '__all__'
-        depth = 2
\ No newline at end of file
+        depth = 1
\ No newline at end of file
diff --git a/LCU/Maintenance/DBInterface/monitoringdb/serializers/station_tests.py b/LCU/Maintenance/DBInterface/monitoringdb/serializers/station_tests.py
index 4a2b5c895c0..6b5246a94f4 100644
--- a/LCU/Maintenance/DBInterface/monitoringdb/serializers/station_tests.py
+++ b/LCU/Maintenance/DBInterface/monitoringdb/serializers/station_tests.py
@@ -7,4 +7,4 @@ class StationTestSerializer(ModelSerializer):
     class Meta:
         model = StationTest
         fields = '__all__'
-        depth = 1
\ No newline at end of file
+        depth = 2
\ No newline at end of file
diff --git a/LCU/Maintenance/DBInterface/monitoringdb/tests/t_stationtest_parser.py b/LCU/Maintenance/DBInterface/monitoringdb/tests/t_stationtest_parser.py
index 6c4a49200b6..4703842114d 100644
--- a/LCU/Maintenance/DBInterface/monitoringdb/tests/t_stationtest_parser.py
+++ b/LCU/Maintenance/DBInterface/monitoringdb/tests/t_stationtest_parser.py
@@ -59,5 +59,49 @@ class TestStationTestsParser(unittest.TestCase):
             for result in hba_result:
                 self.assertIsNot(len(result['tile_errors']), 0)
 
+    def test_element_error_details_parsing(self):
+        preparsed_element_error = ['20180508','HBA','044','E_FAIL',
+                          {'x1':'99.5 140 101.6 99.8 140 102.4'},
+                          {'y1':'99.5 140 101.6 99.8 140 102.4'},
+                          {'ox1': '1'},
+                          {'oy1': '1'},
+                          {'spx1': '1'},
+                          {'spy1': '1'},
+                          {'lnx1': '67.3 2.031'},
+                          {'hnx1': '67.3 2.031'},
+                          {'lny1': '67.3 2.031'},
+                          {'hny1': '67.3 2.031'},
+                          {'jy1': '67.3'},
+                          {'jx1': '67.3'},
+                         ]
+        expected_result = [
+            {'component_error': {'component': {'type': 'HBA', 'component_id': 44},
+                                 'type': 'RF_FAIL'}, 'element': {'element_id': 1},
+             'type': 'RF_FAIL',
+             'details': {'xval_full_delay': 140.0, 'xval_no_delay': 99.5, 'xsb_full_delay': 99.8,
+                         'xsb_no_delay': 101.6, 'xref_full_delay': 102.4, 'xref_no_delay': 140.0,
+                         'yval_full_delay': 140.0, 'yval_no_delay': 99.5, 'ysb_full_delay': 99.8,
+                         'ysb_no_delay': 101.6, 'yref_full_delay': 102.4, 'yref_no_delay': 140.0}},
+            {'component_error': {'component': {'type': 'HBA', 'component_id': 44},
+                                 'type': 'OSCILLATION'}, 'element': {'element_id': 1},
+             'type': 'OSCILLATION', 'details': {'x': True, 'y': True}},
+            {'component_error': {'component': {'type': 'HBA', 'component_id': 44},
+                                 'type': 'SPURIOUS'}, 'element': {'element_id': 1},
+             'type': 'SPURIOUS', 'details': {'x': True, 'y': True}},
+            {'component_error': {'component': {'type': 'HBA', 'component_id': 44},
+                                 'type': 'LOW_NOISE'}, 'element': {'element_id': 1},
+             'type': 'LOW_NOISE',
+             'details': {'xval': 67.3, 'xdiff': 2.031, 'yval': 67.3, 'ydiff': 2.031}},
+            {'component_error': {'component': {'type': 'HBA', 'component_id': 44},
+                                 'type': 'HIGH_NOISE'}, 'element': {'element_id': 1},
+             'type': 'HIGH_NOISE',
+             'details': {'xval': 67.3, 'xdiff': 2.031, 'yval': 67.3, 'ydiff': 2.031}},
+            {'component_error': {'component': {'type': 'HBA', 'component_id': 44},
+                                 'type': 'JITTER'}, 'element': {'element_id': 1}, 'type': 'JITTER',
+             'details': {'ydiff': 67.3, 'xdiff': 67.3}}
+        ]
+        parsed_element_errors = raw_parser.dicts_from_element_error(preparsed_element_error)
+
+        self.assertEqual(parsed_element_errors, expected_result)
 if __name__=='__main__':
     unittest.main()
\ No newline at end of file
diff --git a/LCU/Maintenance/DBInterface/monitoringdb/tests/test_multiple_tileerror_and_rcuerror.csv b/LCU/Maintenance/DBInterface/monitoringdb/tests/test_multiple_tileerror_and_rcuerror.csv
index 9515ee04969..9f80c886fa0 100644
--- a/LCU/Maintenance/DBInterface/monitoringdb/tests/test_multiple_tileerror_and_rcuerror.csv
+++ b/LCU/Maintenance/DBInterface/monitoringdb/tests/test_multiple_tileerror_and_rcuerror.csv
@@ -18,3 +18,4 @@
 20180508,HBA,033,E_FAIL,HNX1=84.8 4.591,HNX2=80.0 10.499,JX8=1.000,HNX9=76.4 12.931,HNX11=80.7 16.661
 20180508,HBA,039,E_FAIL,Y16=98.0 141 101.3 73.8 139 101.5
 20180508,HBA,044,E_FAIL,X15=105.8 143 105.7 65.4 143 106.0,Y15=100.5 143 102.4 65.3 142 102.8
+20180508,HBA,044,E_FAIL,OX1=1,HNY3=70.7 6.709,M5=??,M9=??,M10=??,M13=??,M14=??,OY1=1,OX2=1
-- 
GitLab