From f4045ff806fb7bca811d10af1b3f056cd331882e Mon Sep 17 00:00:00 2001 From: Ross Whitfield Date: Mon, 23 Sep 2024 12:11:23 +1000 Subject: [PATCH 1/4] Move DATETIME_FORMAT to formats module. This will allow the django template engine to automatically use this format --- src/webmon_app/reporting/formats/__init__.py | 0 src/webmon_app/reporting/formats/en_US/__init__.py | 0 src/webmon_app/reporting/formats/en_US/formats.py | 3 +++ src/webmon_app/reporting/reporting_app/settings/base.py | 6 +++--- 4 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 src/webmon_app/reporting/formats/__init__.py create mode 100644 src/webmon_app/reporting/formats/en_US/__init__.py create mode 100644 src/webmon_app/reporting/formats/en_US/formats.py diff --git a/src/webmon_app/reporting/formats/__init__.py b/src/webmon_app/reporting/formats/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/webmon_app/reporting/formats/en_US/__init__.py b/src/webmon_app/reporting/formats/en_US/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/webmon_app/reporting/formats/en_US/formats.py b/src/webmon_app/reporting/formats/en_US/formats.py new file mode 100644 index 00000000..d2e74c78 --- /dev/null +++ b/src/webmon_app/reporting/formats/en_US/formats.py @@ -0,0 +1,3 @@ +# Formatting for datetime objects. +# Default is "N j, Y, P", changing to make it parsable by python datetime +DATETIME_FORMAT = "M j, Y, P" diff --git a/src/webmon_app/reporting/reporting_app/settings/base.py b/src/webmon_app/reporting/reporting_app/settings/base.py index 43dc68b6..c175566b 100644 --- a/src/webmon_app/reporting/reporting_app/settings/base.py +++ b/src/webmon_app/reporting/reporting_app/settings/base.py @@ -51,6 +51,9 @@ def validate_ldap_settings(server_uri, user_dn_template): # Language code for this installation. All choices can be found here: # http://www.i18nguy.com/unicode/language-identifiers.html LANGUAGE_CODE = "en-us" +FORMAT_MODULE_PATH = [ + "reporting.formats", +] SITE_ID = 1 @@ -376,6 +379,3 @@ def validate_ldap_settings(server_uri, user_dn_template): TEST_REMOTE_PASSWD = "" GRAVATAR_URL = "https://www.gravatar.com/avatar/" - -# Default is "N j, Y, P", changing to make it parsable by python datetime -DATETIME_FORMAT = "M j, Y, P" From f80b4264f070d988eeabd05ffefa6e4245fb6962 Mon Sep 17 00:00:00 2001 From: Ross Whitfield Date: Mon, 23 Sep 2024 12:12:02 +1000 Subject: [PATCH 2/4] Switch from using settings.DATETIME_FORMAT to formats.get_format --- src/webmon_app/reporting/dasmon/view_util.py | 20 +++++++++++--------- src/webmon_app/reporting/dasmon/views.py | 4 ++-- src/webmon_app/reporting/pvmon/view_util.py | 4 ++-- src/webmon_app/reporting/pvmon/views.py | 4 ++-- src/webmon_app/reporting/report/admin.py | 10 ++-------- src/webmon_app/reporting/report/view_util.py | 6 +++--- src/webmon_app/reporting/report/views.py | 6 +++--- 7 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/webmon_app/reporting/dasmon/view_util.py b/src/webmon_app/reporting/dasmon/view_util.py index 789d361d..cac9428b 100644 --- a/src/webmon_app/reporting/dasmon/view_util.py +++ b/src/webmon_app/reporting/dasmon/view_util.py @@ -16,7 +16,7 @@ from reporting.pvmon.models import PVCache, PVStringCache, MonitoredVariable from django.shortcuts import get_object_or_404 from django.urls import reverse -from django.utils import dateformat, timezone +from django.utils import dateformat, timezone, formats from django.contrib.auth.models import Group from django.conf import settings import datetime @@ -84,7 +84,7 @@ def get_cached_variables(instrument_id, monitored_only=False): variable_dict = { "key": str(kvp.key_id), "value": string_value, - "timestamp": df.format(settings.DATETIME_FORMAT), + "timestamp": df.format(formats.get_format("DATETIME_FORMAT")), } key_value_pairs.append(variable_dict) @@ -496,7 +496,7 @@ def workflow_diagnostics(timeout=None): wf_conditions.append( "No heartbeat since %s: %s" % ( - df.format(settings.DATETIME_FORMAT), + df.format(formats.get_format("DATETIME_FORMAT")), _red_message("contact the Neutron Data Sciences Group or Linux Support"), ) ) @@ -653,7 +653,7 @@ def pvstreamer_diagnostics(instrument_id, timeout=None, process="pvstreamer"): "No %s heartbeat since %s: %s" % ( process, - df.format(settings.DATETIME_FORMAT), + df.format(formats.get_format("DATETIME_FORMAT")), _red_message("ask Linux Support or DAS to restart pvsd"), ) ) @@ -747,7 +747,9 @@ def dasmon_diagnostics(instrument_id, timeout=None): if dt > delay_time: slow_amq = True df = dateformat.DateFormat(last_amq_time) - dasmon_conditions.append("No ActiveMQ updates from DASMON since %s" % df.format(settings.DATETIME_FORMAT)) + dasmon_conditions.append( + "No ActiveMQ updates from DASMON since %s" % df.format(formats.get_format("DATETIME_FORMAT")) + ) # Heartbeat try: @@ -760,7 +762,7 @@ def dasmon_diagnostics(instrument_id, timeout=None): dasmon_conditions.append( "No heartbeat since %s: %s" % ( - df.format(settings.DATETIME_FORMAT), + df.format(formats.get_format("DATETIME_FORMAT")), _red_message("ask Linux Support or DAS to restart DASMON"), ) ) @@ -948,7 +950,7 @@ def get_live_runs_update(request, instrument_id, ipts_id, **data_dict): ) expt_dict = { "run": r.run_number, - "timestamp": df.format(settings.DATETIME_FORMAT), + "timestamp": df.format(formats.get_format("DATETIME_FORMAT")), "last_error": status, "run_id": r.id, "reduce_url": (" Date: Mon, 23 Sep 2024 15:09:26 +1000 Subject: [PATCH 3/4] Change datetime format to something more easily parsable --- src/webmon_app/reporting/formats/en_US/formats.py | 2 +- tests/test_PVPageView.py | 11 ++--------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/webmon_app/reporting/formats/en_US/formats.py b/src/webmon_app/reporting/formats/en_US/formats.py index d2e74c78..ddf78588 100644 --- a/src/webmon_app/reporting/formats/en_US/formats.py +++ b/src/webmon_app/reporting/formats/en_US/formats.py @@ -1,3 +1,3 @@ # Formatting for datetime objects. # Default is "N j, Y, P", changing to make it parsable by python datetime -DATETIME_FORMAT = "M j, Y, P" +DATETIME_FORMAT = "M j, Y, g:i A" diff --git a/tests/test_PVPageView.py b/tests/test_PVPageView.py index 30bda433..13935b44 100644 --- a/tests/test_PVPageView.py +++ b/tests/test_PVPageView.py @@ -21,16 +21,9 @@ def check_PV(text, PV): assert is_float(value) # just check that a new PV arrived in the last two minutes - time = re.findall(f'.*', text) + time = re.findall(f'(.*)', text) assert len(time) == 1 - time = datetime.strptime( - time[0] - .replace(f'', "") - .replace("", "") - .replace("a.m.", "AM") - .replace("p.m.", "PM"), - "%b %d, %Y, %I:%M %p", - ) + time = datetime.strptime(time[0], "%b %d, %Y, %I:%M %p") time_delta = datetime.now() - time assert time_delta.total_seconds() < 120 From 1c4e10a75f10bf7818f0f93f76bfe8478e0a238a Mon Sep 17 00:00:00 2001 From: Ross Whitfield Date: Mon, 23 Sep 2024 16:10:41 +1000 Subject: [PATCH 4/4] Use formats.localize helper function --- src/webmon_app/reporting/dasmon/view_util.py | 29 +++++++------------- src/webmon_app/reporting/dasmon/views.py | 5 ++-- src/webmon_app/reporting/pvmon/view_util.py | 5 ++-- src/webmon_app/reporting/pvmon/views.py | 5 ++-- src/webmon_app/reporting/report/view_util.py | 8 ++---- src/webmon_app/reporting/report/views.py | 14 ++++------ 6 files changed, 24 insertions(+), 42 deletions(-) diff --git a/src/webmon_app/reporting/dasmon/view_util.py b/src/webmon_app/reporting/dasmon/view_util.py index cac9428b..0cf5d217 100644 --- a/src/webmon_app/reporting/dasmon/view_util.py +++ b/src/webmon_app/reporting/dasmon/view_util.py @@ -16,7 +16,7 @@ from reporting.pvmon.models import PVCache, PVStringCache, MonitoredVariable from django.shortcuts import get_object_or_404 from django.urls import reverse -from django.utils import dateformat, timezone, formats +from django.utils import timezone, formats from django.contrib.auth.models import Group from django.conf import settings import datetime @@ -68,7 +68,6 @@ def get_cached_variables(instrument_id, monitored_only=False): localtime = timezone.localtime(kvp.timestamp) except: # noqa: E722 localtime = kvp.timestamp - df = dateformat.DateFormat(localtime) # Check whether we have a number try: @@ -84,7 +83,7 @@ def get_cached_variables(instrument_id, monitored_only=False): variable_dict = { "key": str(kvp.key_id), "value": string_value, - "timestamp": df.format(formats.get_format("DATETIME_FORMAT")), + "timestamp": formats.localize(localtime), } key_value_pairs.append(variable_dict) @@ -492,11 +491,11 @@ def workflow_diagnostics(timeout=None): time_diff = timezone.now() - status_time.replace(tzinfo=None) if time_diff > delay_time: dasmon_listener_warning = True - df = dateformat.DateFormat(status_time) + wf_conditions.append( "No heartbeat since %s: %s" % ( - df.format(formats.get_format("DATETIME_FORMAT")), + formats.localize(status_time), _red_message("contact the Neutron Data Sciences Group or Linux Support"), ) ) @@ -648,12 +647,11 @@ def pvstreamer_diagnostics(instrument_id, timeout=None, process="pvstreamer"): timediff = timezone.now() - status_time.replace(tzinfo=None) if timediff > delay_time: dasmon_listener_warning = True - df = dateformat.DateFormat(status_time) pv_conditions.append( "No %s heartbeat since %s: %s" % ( process, - df.format(formats.get_format("DATETIME_FORMAT")), + formats.localize(status_time), _red_message("ask Linux Support or DAS to restart pvsd"), ) ) @@ -746,10 +744,7 @@ def dasmon_diagnostics(instrument_id, timeout=None): dt = timezone.now().replace(tzinfo=None) - last_amq_time.replace(tzinfo=None) if dt > delay_time: slow_amq = True - df = dateformat.DateFormat(last_amq_time) - dasmon_conditions.append( - "No ActiveMQ updates from DASMON since %s" % df.format(formats.get_format("DATETIME_FORMAT")) - ) + dasmon_conditions.append("No ActiveMQ updates from DASMON since %s" % formats.localize(last_amq_time)) # Heartbeat try: @@ -758,11 +753,10 @@ def dasmon_diagnostics(instrument_id, timeout=None): dt = timezone.now().replace(tzinfo=None) - status_time.replace(tzinfo=None) if dt > delay_time: slow_status = True - df = dateformat.DateFormat(status_time) dasmon_conditions.append( "No heartbeat since %s: %s" % ( - df.format(formats.get_format("DATETIME_FORMAT")), + formats.localize(status_time), _red_message("ask Linux Support or DAS to restart DASMON"), ) ) @@ -943,14 +937,13 @@ def get_live_runs_update(request, instrument_id, ipts_id, **data_dict): localtime = timezone.localtime(r.created_on) except: # noqa: E722 localtime = r.created_on - df = dateformat.DateFormat(localtime) reduce_url = reverse( "report:submit_for_reduction", args=[str(r.instrument_id), r.run_number], ) expt_dict = { "run": r.run_number, - "timestamp": df.format(formats.get_format("DATETIME_FORMAT")), + "timestamp": formats.localize(localtime), "last_error": status, "run_id": r.id, "reduce_url": ("