Skip to content

Commit

Permalink
Merge pull request #184 from neutrons/datetime_format
Browse files Browse the repository at this point in the history
Update how datetime format is set and used
  • Loading branch information
rosswhitfield authored Sep 23, 2024
2 parents 6077aad + 1c4e10a commit 793af07
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 60 deletions.
27 changes: 10 additions & 17 deletions src/webmon_app/reporting/dasmon/view_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 timezone, formats
from django.contrib.auth.models import Group
from django.conf import settings
import datetime
Expand Down Expand Up @@ -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:
Expand All @@ -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(settings.DATETIME_FORMAT),
"timestamp": formats.localize(localtime),
}
key_value_pairs.append(variable_dict)

Expand Down Expand Up @@ -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(settings.DATETIME_FORMAT),
formats.localize(status_time),
_red_message("contact the Neutron Data Sciences Group or Linux Support"),
)
)
Expand Down Expand Up @@ -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(settings.DATETIME_FORMAT),
formats.localize(status_time),
_red_message("ask Linux Support or DAS to restart pvsd"),
)
)
Expand Down Expand Up @@ -746,8 +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(settings.DATETIME_FORMAT))
dasmon_conditions.append("No ActiveMQ updates from DASMON since %s" % formats.localize(last_amq_time))

# Heartbeat
try:
Expand All @@ -756,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(settings.DATETIME_FORMAT),
formats.localize(status_time),
_red_message("ask Linux Support or DAS to restart DASMON"),
)
)
Expand Down Expand Up @@ -941,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(settings.DATETIME_FORMAT),
"timestamp": formats.localize(localtime),
"last_error": status,
"run_id": r.id,
"reduce_url": ("<a id='reduce_%s' href='javascript:void(0);'" % r.run_number)
Expand Down Expand Up @@ -1099,8 +1094,7 @@ def get_signals(instrument_id):
else:
value = "%s" % latest.value
localtime = datetime.datetime.fromtimestamp(latest.update_time).replace(tzinfo=timezone.utc)
df = dateformat.DateFormat(localtime)
timestamp = df.format(settings.DATETIME_FORMAT)
timestamp = formats.localize(localtime)
except: # noqa: E722
value = "No data available"
timestamp = "-"
Expand Down Expand Up @@ -1257,15 +1251,14 @@ def get_latest_updates(instrument_id, message_channel, timeframe=2.0, number_of_
localtime = timezone.localtime(item.timestamp)
except: # noqa: E722
localtime = item.timestamp
df = dateformat.DateFormat(localtime)
try:
ts = timezone.make_naive(item.timestamp, timezone.utc).isoformat()
except: # noqa: E722
ts = item.timestamp.isoformat()
template_data.append(
{
"info": str(item.value),
"time": str(df.format(settings.DATETIME_FORMAT)),
"time": formats.localize(localtime),
"timestamp": ts,
}
)
Expand Down
5 changes: 2 additions & 3 deletions src/webmon_app/reporting/dasmon/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404
from django.urls import reverse
from django.utils import dateformat, timezone
from django.utils import timezone, formats
from django.conf import settings
from django.views.decorators.cache import cache_page, cache_control
from django.views.decorators.vary import vary_on_cookie
Expand Down Expand Up @@ -399,11 +399,10 @@ def get_update(request, instrument):
data_dict["variables"] = view_util.get_cached_variables(instrument_id, monitored_only=False)

localtime = timezone.now()
df = dateformat.DateFormat(localtime)
recording_status = {
"key": "recording_status",
"value": view_util.is_running(instrument_id),
"timestamp": df.format(settings.DATETIME_FORMAT),
"timestamp": formats.localize(localtime),
}
data_dict["variables"].append(recording_status)

Expand Down
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions src/webmon_app/reporting/formats/en_US/formats.py
Original file line number Diff line number Diff line change
@@ -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, g:i A"
5 changes: 2 additions & 3 deletions src/webmon_app/reporting/pvmon/view_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@copyright: 2014 Oak Ridge National Laboratory
"""
from reporting.pvmon.models import PVName, PV, PVCache, PVStringCache
from django.utils import dateformat, timezone
from django.utils import timezone, formats
from django.conf import settings
import datetime
import logging
Expand Down Expand Up @@ -116,7 +116,6 @@ def _process_pvs(queryset):
for kvp in queryset:
if kvp.name.monitored or monitored_only is False:
localtime = datetime.datetime.fromtimestamp(kvp.update_time).replace(tzinfo=timezone.utc)
df = dateformat.DateFormat(localtime)
if isinstance(kvp.value, (int, float)):
string_value = "%g" % kvp.value
else:
Expand All @@ -125,7 +124,7 @@ def _process_pvs(queryset):
item = {
"key": str(kvp.name),
"value": string_value,
"timestamp": df.format(settings.DATETIME_FORMAT),
"timestamp": formats.localize(localtime),
}
key_value_pairs.append(item)

Expand Down
5 changes: 2 additions & 3 deletions src/webmon_app/reporting/pvmon/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404
from django.urls import reverse
from django.utils import dateformat, timezone
from django.utils import timezone, formats
from django.conf import settings
from django.views.decorators.cache import cache_page, cache_control
from django.views.decorators.vary import vary_on_cookie
Expand Down Expand Up @@ -72,11 +72,10 @@ def get_update(request, instrument):

# Update the recording status
localtime = timezone.now()
df = dateformat.DateFormat(localtime)
recording_status = {
"key": "recording_status",
"value": dasmon_view_util.is_running(instrument_id),
"timestamp": df.format(settings.DATETIME_FORMAT),
"timestamp": formats.localize(localtime),
}
data_dict["variables"].append(recording_status)

Expand Down
10 changes: 2 additions & 8 deletions src/webmon_app/reporting/report/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
)
from reporting.report.models import Information, Error, Task
from django.contrib import admin
from django.utils import dateformat, timezone
from django.conf import settings


def reduction_not_needed(modeladmin, request, queryset):
Expand Down Expand Up @@ -77,9 +75,7 @@ class InformationAdmin(admin.ModelAdmin):
search_fields = ["description"]

def time(self, obj):
localtime = timezone.localtime(obj.run_status_id.created_on)
df = dateformat.DateFormat(localtime)
return df.format(settings.DATETIME_FORMAT)
return obj.run_status_id.created_on


class ErrorAdmin(admin.ModelAdmin):
Expand All @@ -89,9 +85,7 @@ class ErrorAdmin(admin.ModelAdmin):
search_fields = ["description"]

def time(self, obj):
localtime = timezone.localtime(obj.run_status_id.created_on)
df = dateformat.DateFormat(localtime)
return df.format(settings.DATETIME_FORMAT)
return obj.run_status_id.created_on


class StatusQueueAdmin(admin.ModelAdmin):
Expand Down
8 changes: 3 additions & 5 deletions src/webmon_app/reporting/report/view_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from django.urls import reverse
from django.http import HttpResponseServerError
from django.shortcuts import get_object_or_404, redirect
from django.utils import dateformat, timezone
from django.utils import timezone, formats
from django.conf import settings
from django.db import connection, transaction
from django.core.cache import cache
Expand Down Expand Up @@ -337,10 +337,9 @@ def get_current_status(instrument_id):

if last_run_id is not None:
localtime = timezone.localtime(last_run_id.created_on)
df = dateformat.DateFormat(localtime)
data_dict["last_run_id"] = last_run_id.id
data_dict["last_run"] = last_run_id.run_number
data_dict["last_run_time"] = df.format(settings.DATETIME_FORMAT)
data_dict["last_run_time"] = formats.localize(localtime)

if last_expt_id is not None:
data_dict["last_expt_id"] = last_expt_id.id
Expand Down Expand Up @@ -414,7 +413,6 @@ def get_run_list_dict(run_list):
try:
for r in run_list:
localtime = timezone.localtime(r.created_on)
df = dateformat.DateFormat(localtime)

run_url = reverse("report:detail", args=[str(r.instrument_id), r.run_number])
reduce_url = reverse("report:submit_for_reduction", args=[str(r.instrument_id), r.run_number])
Expand All @@ -429,7 +427,7 @@ def get_run_list_dict(run_list):
% (r.run_number, reduce_url, r.run_number)
),
"run_id": r.id,
"timestamp": str(df.format(settings.DATETIME_FORMAT)),
"timestamp": formats.localize(localtime),
"status": get_run_status_text(r, use_element_id=True),
}
)
Expand Down
14 changes: 5 additions & 9 deletions src/webmon_app/reporting/report/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.http import HttpResponse, HttpResponseNotFound
from django.shortcuts import render, get_object_or_404
from django.urls import reverse
from django.utils import dateformat, timezone
from django.utils import timezone, formats
from django.views.decorators.cache import cache_page, cache_control
from django.views.decorators.vary import vary_on_cookie
from django.conf import settings
Expand Down Expand Up @@ -334,7 +334,6 @@ def instrument_summary(request, instrument):
expt_list = []
for expt in ipts:
localtime = timezone.localtime(expt.created_on)
df = dateformat.DateFormat(localtime)
expt_list.append(
{
"experiment": str(
Expand All @@ -346,7 +345,7 @@ def instrument_summary(request, instrument):
),
"total": expt.number_of_runs(),
"timestamp": expt.created_on.isoformat(),
"created_on": str(df.format(settings.DATETIME_FORMAT)),
"created_on": formats.localize(localtime),
}
)

Expand Down Expand Up @@ -461,7 +460,6 @@ def live_errors(request, instrument):
error_list = []
for err in error_query:
localtime = timezone.localtime(err.run_status_id.created_on)
df = dateformat.DateFormat(localtime)
error_list.append(
{
"experiment": str(
Expand Down Expand Up @@ -489,7 +487,7 @@ def live_errors(request, instrument):
),
"info": str(err.description),
"timestamp": err.run_status_id.created_on.isoformat(),
"created_on": str(df.format(settings.DATETIME_FORMAT)),
"created_on": formats.localize(localtime),
}
)

Expand Down Expand Up @@ -571,11 +569,10 @@ def get_instrument_update(request, instrument):
for e in expt_list:
if since_expt_id.created_on < e.created_on:
localtime = timezone.localtime(e.created_on)
df = dateformat.DateFormat(localtime)
expt_dict = {
"ipts": e.expt_name.upper(),
"n_runs": e.number_of_runs(),
"created_on": df.format(settings.DATETIME_FORMAT),
"created_on": formats.localize(localtime),
"timestamp": e.created_on.isoformat(),
"ipts_id": e.id,
}
Expand Down Expand Up @@ -622,12 +619,11 @@ def get_error_update(request, instrument):

if last_error_id.run_status_id.created_on < e.run_status_id.created_on:
localtime = timezone.localtime(e.run_status_id.created_on)
df = dateformat.DateFormat(localtime)
err_dict = {
"run": e.run_status_id.run_id.run_number,
"ipts": e.run_status_id.run_id.ipts_id.expt_name,
"description": e.description,
"created_on": df.format(settings.DATETIME_FORMAT),
"created_on": formats.localize(localtime),
"timestamp": e.run_status_id.created_on.isoformat(),
"error_id": e.id,
}
Expand Down
6 changes: 3 additions & 3 deletions src/webmon_app/reporting/reporting_app/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
11 changes: 2 additions & 9 deletions tests/test_PVPageView.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'<td><span id="{PV}_timestamp">.*</span></td>', text)
time = re.findall(f'<td><span id="{PV}_timestamp">(.*)</span></td>', text)
assert len(time) == 1
time = datetime.strptime(
time[0]
.replace(f'<td><span id="{PV}_timestamp">', "")
.replace("</span></td>", "")
.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

Expand Down

0 comments on commit 793af07

Please sign in to comment.