From 2b12d81b6d2abad1aa3af791963272ab030fc1c4 Mon Sep 17 00:00:00 2001 From: Ross Whitfield Date: Mon, 30 Sep 2024 13:30:02 +1000 Subject: [PATCH 1/4] Fix get_cached_variables so it only return newest distinct results --- src/webmon_app/reporting/dasmon/view_util.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/webmon_app/reporting/dasmon/view_util.py b/src/webmon_app/reporting/dasmon/view_util.py index 0cf5d217..f5a2404c 100644 --- a/src/webmon_app/reporting/dasmon/view_util.py +++ b/src/webmon_app/reporting/dasmon/view_util.py @@ -55,7 +55,11 @@ def get_cached_variables(instrument_id, monitored_only=False): :param instrument_id: Instrument object :param monitored_only: if True, only monitored parameters are returned """ - parameter_values = StatusCache.objects.filter(instrument_id=instrument_id).order_by("key_id__name") + parameter_values = ( + StatusCache.objects.filter(instrument_id=instrument_id) + .order_by("key_id__name", "-timestamp") + .distinct("key_id__name") + ) # Variables that are displayed on top top_variables = ["run_number", "proposal_id", "run_title"] key_value_pairs = [] From af4fc621dc8c97ba2838bcb95f901787d85bf4cb Mon Sep 17 00:00:00 2001 From: Ross Whitfield Date: Mon, 30 Sep 2024 15:33:19 +1000 Subject: [PATCH 2/4] Test multiple run_title's, make sure to get only the newest --- .../reporting/tests/test_dasmon/test_view_util.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/webmon_app/reporting/tests/test_dasmon/test_view_util.py b/src/webmon_app/reporting/tests/test_dasmon/test_view_util.py index 409341b7..4270a382 100644 --- a/src/webmon_app/reporting/tests/test_dasmon/test_view_util.py +++ b/src/webmon_app/reporting/tests/test_dasmon/test_view_util.py @@ -41,9 +41,11 @@ def setUpClass(cls): proposal_id.save() run_title = Parameter.objects.create(name="run_title") run_title.save() + + # we add the run_title twice to check that the newest one is returned for p, v in zip( - [para, run_number, cnt_rate, proposal_id, run_title], - ["testValue", 0, 1, 2, "testRunTitle"], + [para, run_number, cnt_rate, proposal_id, run_title, run_title], + ["testValue", 0, 1, 2, "testRunTitle", "testRunTitleNew"], ): StatusVariable.objects.create( instrument_id=inst, @@ -106,6 +108,9 @@ def test_get_cached_variables(self): for d in pairs: if d["key"] == "testParam": assert d["value"] == ref_val["value"] + if d["key"] == "run_title": + # expect run_title==testRunTitleNew because it's newest + assert d["value"] == "testRunTitleNew" for d in pairs_monitoredOnly: if d["key"] == "testParam": assert d["value"] == ref_val["value"] From 3c67d499bbfaeedbebae37b9b0738889b0dd1bed Mon Sep 17 00:00:00 2001 From: Ross Whitfield Date: Mon, 30 Sep 2024 16:08:04 +1000 Subject: [PATCH 3/4] Can't use distinct because sqlite and therefore unittests don't support it --- src/webmon_app/reporting/dasmon/view_util.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/webmon_app/reporting/dasmon/view_util.py b/src/webmon_app/reporting/dasmon/view_util.py index f5a2404c..00a37b07 100644 --- a/src/webmon_app/reporting/dasmon/view_util.py +++ b/src/webmon_app/reporting/dasmon/view_util.py @@ -55,15 +55,19 @@ def get_cached_variables(instrument_id, monitored_only=False): :param instrument_id: Instrument object :param monitored_only: if True, only monitored parameters are returned """ - parameter_values = ( - StatusCache.objects.filter(instrument_id=instrument_id) - .order_by("key_id__name", "-timestamp") - .distinct("key_id__name") - ) + parameter_values = StatusCache.objects.filter(instrument_id=instrument_id).order_by("key_id__name", "-timestamp") # Variables that are displayed on top top_variables = ["run_number", "proposal_id", "run_title"] key_value_pairs = [] + keys_used = set() for kvp in parameter_values: + + if kvp.key_id in keys_used: + # only used the first value for each key, will be ordered newest first + continue + + keys_used.add(kvp.key_id) + if kvp.key_id.monitored or monitored_only is False: # Exclude top variables if monitored_only and str(kvp.key_id) in top_variables: From 7f2dafbdedeb44cb5454fc3c111a1a5538a14436 Mon Sep 17 00:00:00 2001 From: Ross Whitfield Date: Mon, 30 Sep 2024 16:29:53 +1000 Subject: [PATCH 4/4] Update other test --- src/webmon_app/reporting/tests/test_dasmon/test_view_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webmon_app/reporting/tests/test_dasmon/test_view_util.py b/src/webmon_app/reporting/tests/test_dasmon/test_view_util.py index 4270a382..9a8e536f 100644 --- a/src/webmon_app/reporting/tests/test_dasmon/test_view_util.py +++ b/src/webmon_app/reporting/tests/test_dasmon/test_view_util.py @@ -414,7 +414,7 @@ def test_fill_template_values( assert template["run_number"] == "0" assert template["count_rate"] == "-" assert template["proposal_id"] == "2" - assert template["run_title"] == "testRunTitle" + assert template["run_title"] == "testRunTitleNew" def test_get_live_variables(self): from reporting.dasmon.view_util import get_live_variables