diff --git a/hera_mc/mc_session.py b/hera_mc/mc_session.py index 8e99e8527..7ecb2f514 100644 --- a/hera_mc/mc_session.py +++ b/hera_mc/mc_session.py @@ -4618,7 +4618,7 @@ def add_antenna_status_from_corrcm(self, ant_status_dict=None, def add_ant_metric(self, obsid, ant, pol, metric, val): """ - Add a new antenna metric to the M&C database. + Add a new antenna metric or update an existing metric. Parameters ---------- @@ -4636,7 +4636,8 @@ def add_ant_metric(self, obsid, ant, pol, metric, val): """ db_time = self.get_current_db_time() - self.add(AntMetrics.create(obsid, ant, pol, metric, db_time, val)) + obj_list = [AntMetrics.create(obsid, ant, pol, metric, db_time, val)] + self._insert_ignoring_duplicates(AntMetrics, obj_list, update=True) def get_ant_metric(self, ant=None, pol=None, metric=None, starttime=None, stoptime=None): @@ -4681,7 +4682,7 @@ def get_ant_metric(self, ant=None, pol=None, metric=None, starttime=None, def add_array_metric(self, obsid, metric, val): """ - Add a new array metric to the M&C database. + Add a new array metric or update an existing metric. Parameters ---------- @@ -4695,7 +4696,8 @@ def add_array_metric(self, obsid, metric, val): """ db_time = self.get_current_db_time() - self.add(ArrayMetrics.create(obsid, metric, db_time, val)) + obj_list = [ArrayMetrics.create(obsid, metric, db_time, val)] + self._insert_ignoring_duplicates(ArrayMetrics, obj_list, update=True) def get_array_metric(self, metric=None, starttime=None, stoptime=None): """ diff --git a/hera_mc/tests/test_qm.py b/hera_mc/tests/test_qm.py index 601f59d06..a4de9394e 100644 --- a/hera_mc/tests/test_qm.py +++ b/hera_mc/tests/test_qm.py @@ -44,12 +44,22 @@ def test_AntMetrics(mcsession, pol_x, pol_y): # now the tests test_session.add_ant_metric(obsid, 0, pol_x, 'test', 4.5) + test_session.commit() r = test_session.get_ant_metric(metric='test') assert len(r) == 1 assert r[0].antpol == (0, pol_x) assert r[0].metric == 'test' assert r[0].val == 4.5 + # test that updating works + test_session.add_ant_metric(obsid, 0, pol_x, 'test', 4.3) + test_session.commit() + r = test_session.get_ant_metric(metric='test') + assert len(r) == 1 + assert r[0].antpol == (0, pol_x) + assert r[0].metric == 'test' + assert r[0].val == 4.3 + # Test more exciting queries test_session.add_ant_metric(obsid, 0, pol_y, 'test', 2.5) test_session.add_ant_metric(obsid, 3, pol_x, 'test', 2.5) @@ -152,10 +162,18 @@ def test_ArrayMetrics(mcsession): # now the tests test_session.add_array_metric(obsid, 'test', 6.2) + test_session.commit() r = test_session.get_array_metric() assert r[0].metric == 'test' assert r[0].val == 6.2 + # test that updating works + test_session.add_array_metric(obsid, 'test', 6.5) + test_session.commit() + r = test_session.get_array_metric() + assert r[0].metric == 'test' + assert r[0].val == 6.5 + # Test more exciting queries test_session.add_array_metric(obsid + 10, 'test', 2.5) test_session.add_array_metric(obsid - 10, 'test', 2.5)