Skip to content

Commit

Permalink
Use formats.localize helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
rosswhitfield committed Sep 23, 2024
1 parent 2eda47b commit 1c4e10a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 42 deletions.
29 changes: 10 additions & 19 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, formats
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(formats.get_format("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(formats.get_format("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(formats.get_format("DATETIME_FORMAT")),
formats.localize(status_time),
_red_message("ask Linux Support or DAS to restart pvsd"),
)
)
Expand Down Expand Up @@ -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:
Expand All @@ -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"),
)
)
Expand Down Expand Up @@ -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": ("<a id='reduce_%s' href='javascript:void(0);'" % r.run_number)
Expand Down Expand Up @@ -1101,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(formats.get_format("DATETIME_FORMAT"))
timestamp = formats.localize(localtime)
except: # noqa: E722
value = "No data available"
timestamp = "-"
Expand Down Expand Up @@ -1259,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(formats.get_format("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, formats
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(formats.get_format("DATETIME_FORMAT")),
"timestamp": formats.localize(localtime),
}
data_dict["variables"].append(recording_status)

Expand Down
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, formats
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(formats.get_format("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, formats
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(formats.get_format("DATETIME_FORMAT")),
"timestamp": formats.localize(localtime),
}
data_dict["variables"].append(recording_status)

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, formats
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(formats.get_format("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(formats.get_format("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, formats
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(formats.get_format("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(formats.get_format("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

0 comments on commit 1c4e10a

Please sign in to comment.