Skip to content

Commit 0741e68

Browse files
committed
fix timezones to UTC
1 parent d63494f commit 0741e68

File tree

11 files changed

+26
-26
lines changed

11 files changed

+26
-26
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/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
)

tests/fixtures/models.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ class Meta:
5858

5959
name = factory.LazyFunction(lambda: str(uuid.uuid4()))
6060
monitor_level = 1
61-
time_added = factory.LazyFunction(lambda: datetime.utcnow() - timedelta(days=1))
61+
time_added = factory.LazyFunction(lambda: datetime.now(datetime.timezone.utc) - timedelta(days=1))
6262
version_added = "1.0"
63-
last_requested = factory.LazyFunction(datetime.utcnow)
63+
last_requested = factory.LazyFunction(datetime.now(datetime.timezone.utc))
6464

6565

6666
class RequestFactory(ModelFactory):
@@ -69,7 +69,7 @@ class Meta:
6969

7070
endpoint = factory.SubFactory(EndpointFactory)
7171
duration = factory.LazyFunction(lambda: random() * 5000)
72-
time_requested = factory.LazyFunction(datetime.utcnow)
72+
time_requested = factory.LazyFunction(datetime.now(datetime.timezone.utc))
7373
version_requested = factory.LazyFunction(lambda: str(uuid.uuid4()))
7474
group_by = None
7575
ip = factory.Faker("ipv4_private")
@@ -115,7 +115,7 @@ class Meta:
115115
model = CustomGraph
116116

117117
title = factory.Faker("name")
118-
time_added = factory.LazyFunction(datetime.utcnow)
118+
time_added = factory.LazyFunction(datetime.now(datetime.timezone.utc))
119119
version_added = factory.LazyFunction(lambda: str(uuid.uuid4()))
120120

121121

@@ -124,7 +124,7 @@ class Meta:
124124
model = CustomGraphData
125125

126126
graph = factory.SubFactory(CustomGraphFactory)
127-
time = factory.LazyFunction(datetime.utcnow)
127+
time = factory.LazyFunction(datetime.now(datetime.timezone.utc))
128128
value = factory.LazyFunction(random)
129129

130130

0 commit comments

Comments
 (0)