Skip to content

Commit 4384489

Browse files
committed
fix timezones to UTC
1 parent d63494f commit 4384489

File tree

12 files changed

+28
-32
lines changed

12 files changed

+28
-32
lines changed

flask_monitoringdashboard/controllers/endpoints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def get_endpoint_overview(session):
3232
:param session: session for the database
3333
:return: A list of properties for each endpoint that is found in the database
3434
"""
35-
week_ago = datetime.datetime.now(datetime.UTC) - datetime.timedelta(days=7)
36-
now_local = to_local_datetime(datetime.datetime.now(datetime.UTC))
35+
week_ago = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=7)
36+
now_local = to_local_datetime(datetime.datetime.now(datetime.timezone.utc))
3737
today_local = now_local.replace(hour=0, minute=0, second=0, microsecond=0)
3838
today_utc = to_utc_datetime(today_local)
3939

flask_monitoringdashboard/core/cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ def update_last_requested_cache(endpoint_name):
6969
Use this instead of updating the last requested to the database.
7070
"""
7171
global memory_cache
72-
memory_cache.get(endpoint_name).set_last_requested(datetime.datetime.now(datetime.UTC))
72+
memory_cache.get(endpoint_name).set_last_requested(datetime.datetime.now(datetime.timezone.utc))
7373

7474

7575
def update_duration_cache(endpoint_name, duration):
7676
"""
7777
Use this together with adding a request to the database.
7878
"""
7979
global memory_cache
80-
memory_cache.get(endpoint_name).set_last_requested(datetime.datetime.now(datetime.UTC))
80+
memory_cache.get(endpoint_name).set_last_requested(datetime.datetime.now(datetime.timezone.utc))
8181
memory_cache.get(endpoint_name).set_duration(duration)
8282

8383

flask_monitoringdashboard/core/database_pruning.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
def prune_database_older_than_weeks(weeks_to_keep, delete_custom_graph_data):
2424
"""Prune the database of Request and optionally CustomGraph data older than the specified number of weeks"""
2525
with session_scope() as session:
26-
date_to_delete_from = datetime.utcnow() - timedelta(weeks=weeks_to_keep)
26+
date_to_delete_from = datetime.now(datetime.timezone.utc) - timedelta(weeks=weeks_to_keep)
2727

2828
# Prune Request table and related Outlier entries
2929
requests_to_delete = (

flask_monitoringdashboard/core/telemetry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def initialize_telemetry_session(session):
8787
else:
8888
telemetry_user = session.query(TelemetryUser).one()
8989
telemetry_user.times_initialized += 1
90-
telemetry_user.last_initialized = datetime.datetime.now(datetime.UTC)
90+
telemetry_user.last_initialized = datetime.datetime.now(datetime.timezone.utc)
9191
session.commit()
9292

9393
# reset telemetry if declined in previous session

flask_monitoringdashboard/core/timezone.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ def to_local_datetime(dt):
1010
from flask_monitoringdashboard import config
1111

1212
if dt:
13-
if dt.tzinfo is None:
14-
dt = dt.replace(tzinfo=datetime.timezone.utc)
15-
return dt.astimezone(config.timezone)
13+
return dt + config.timezone.utcoffset(datetime.datetime.now(datetime.timezone.utc))
1614
return None
1715

1816

@@ -25,7 +23,5 @@ def to_utc_datetime(dt):
2523
from flask_monitoringdashboard import config
2624

2725
if dt:
28-
if dt.tzinfo is None:
29-
dt = dt.replace(tzinfo=config.timezone)
30-
return dt.astimezone(datetime.timezone.utc)
26+
return dt - config.timezone.utcoffset(datetime.datetime.now(datetime.timezone.utc))
3127
return None

flask_monitoringdashboard/database/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class TelemetryUser(Base):
7272
times_initialized = Column(Integer, default=1)
7373
"""For checking the amount of times the app was initialized"""
7474

75-
last_initialized = Column(DateTime, default=datetime.datetime.utcnow)
75+
last_initialized = Column(DateTime, default=datetime.datetime.now(datetime.timezone.utc))
7676
"""Check when was the last time user accessed FMD"""
7777

7878
monitoring_consent = Column(Integer, default=1)
@@ -93,7 +93,7 @@ class Endpoint(Base):
9393
monitor_level = Column(Integer, default=config.monitor_level)
9494
"""0 - disabled, 1 - performance, 2 - outliers, 3 - profiler + outliers"""
9595

96-
time_added = Column(DateTime, default=datetime.datetime.utcnow)
96+
time_added = Column(DateTime, default=datetime.datetime.now(datetime.timezone.utc))
9797
"""Time when the endpoint was added."""
9898

9999
version_added = Column(String(100), default=config.version)
@@ -118,7 +118,7 @@ class Request(Base):
118118
duration = Column(Float, nullable=False)
119119
"""Processing time of the request in milliseconds."""
120120

121-
time_requested = Column(DateTime, default=datetime.datetime.utcnow)
121+
time_requested = Column(DateTime, default=datetime.datetime.now(datetime.timezone.utc))
122122
"""Moment when the request was handled."""
123123

124124
version_requested = Column(String(100), default=config.version)
@@ -225,7 +225,7 @@ class CustomGraph(Base):
225225
title = Column(String(250), nullable=False, unique=True)
226226
"""Title of this graph."""
227227

228-
time_added = Column(DateTime, default=datetime.datetime.utcnow)
228+
time_added = Column(DateTime, default=datetime.datetime.now(datetime.timezone.utc))
229229
"""When the graph was first added to the dashboard."""
230230

231231
version_added = Column(String(100), default=config.version)
@@ -244,7 +244,7 @@ class CustomGraphData(Base):
244244
graph = relationship(CustomGraph, backref="data")
245245
"""Graph for which the data is collected."""
246246

247-
time = Column(DateTime, default=datetime.datetime.utcnow)
247+
time = Column(DateTime, default=datetime.datetime.now(datetime.timezone.utc))
248248
"""Moment when the data is collected."""
249249

250250
value = Column(Float)

flask_monitoringdashboard/database/endpoint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def update_last_requested(session, endpoint_name, timestamp=None):
150150
:param endpoint_name: name of the endpoint
151151
:param timestamp: optional timestamp. If not given, timestamp is current time
152152
"""
153-
ts = timestamp if timestamp else datetime.datetime.now(datetime.UTC)
153+
ts = timestamp if timestamp else datetime.datetime.now(datetime.timezone.utc)
154154
session.query(Endpoint).filter(Endpoint.name == endpoint_name).update(
155155
{Endpoint.last_requested: ts}
156156
)

flask_monitoringdashboard/views/reporting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
def get_date(p):
22-
return datetime.utcfromtimestamp(int(request.args.get(p)))
22+
return datetime.fromtimestamp(int(request.args.get(p)),tz = datetime.datetime.now(datetime.timezone.utc))
2323

2424

2525
def make_endpoint_summary(endpoint, requests_criterion, baseline_requests_criterion):

tests/api/test_custom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_custom_graphs(dashboard_user, custom_graph, session):
1717

1818

1919
def test_custom_graph_data(dashboard_user, custom_graph, custom_graph_data):
20-
today = datetime.utcnow()
20+
today = datetime.now(datetime.timezone.utc)
2121
yesterday = today - timedelta(days=1)
2222
response = dashboard_user.get('dashboard/api/custom_graph/{id}/{start}/{end}'.format(
2323
id=custom_graph.graph_id,

tests/api/test_reporting.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ def test_make_report_get(dashboard_user):
1111
assert not response.is_json
1212

1313

14-
@pytest.mark.parametrize('request_1__time_requested', [datetime.utcnow() - timedelta(hours=6)])
14+
@pytest.mark.parametrize('request_1__time_requested', [datetime.now(datetime.timezone.utc) - timedelta(hours=6)])
1515
@pytest.mark.parametrize('request_1__duration', [5000])
1616
@pytest.mark.parametrize('request_1__status_code', [500])
17-
@pytest.mark.parametrize('request_2__time_requested', [datetime.utcnow() - timedelta(days=1, hours=6)])
17+
@pytest.mark.parametrize('request_2__time_requested', [datetime.now(datetime.timezone.utc) - timedelta(days=1, hours=6)])
1818
@pytest.mark.parametrize('request_2__duration', [100])
1919
@pytest.mark.skipif(sys.version_info < (3, ), reason="For some reason, this doesn't work in python 2.7.")
2020
def test_make_report_post_not_significant(dashboard_user, endpoint, request_1, request_2, session):
@@ -23,12 +23,12 @@ def test_make_report_post_not_significant(dashboard_user, endpoint, request_1, r
2323
'dashboard/api/reporting/make_report/intervals',
2424
json={
2525
'interval': {
26-
'from': (datetime.utcnow() - timedelta(days=1) - epoch).total_seconds(),
27-
'to': (datetime.utcnow() - epoch).total_seconds(),
26+
'from': (datetime.now(datetime.timezone.utc) - timedelta(days=1) - epoch).total_seconds(),
27+
'to': (datetime.now(datetime.timezone.utc) - epoch).total_seconds(),
2828
},
2929
'baseline_interval': {
30-
'from': (datetime.utcnow() - timedelta(days=2) - epoch).total_seconds(),
31-
'to': (datetime.utcnow() - timedelta(days=1) - epoch).total_seconds(),
30+
'from': (datetime.now(datetime.timezone.utc) - timedelta(days=2) - epoch).total_seconds(),
31+
'to': (datetime.now(datetime.timezone.utc) - timedelta(days=1) - epoch).total_seconds(),
3232
},
3333
},
3434
)

0 commit comments

Comments
 (0)