diff --git a/src/webmon_app/reporting/dasmon/view_util.py b/src/webmon_app/reporting/dasmon/view_util.py index 0cf5d217..00a37b07 100644 --- a/src/webmon_app/reporting/dasmon/view_util.py +++ b/src/webmon_app/reporting/dasmon/view_util.py @@ -55,11 +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") + 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: 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..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 @@ -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"] @@ -409,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