Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove date conversion #2398

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions app/celery/process_pinpoint_receipts_tasks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from datetime import datetime
from datetime import datetime, timezone
from typing import Union

from flask import current_app, json
from notifications_utils.statsd_decorators import statsd
from notifications_utils.timezones import convert_utc_to_local_timezone
from sqlalchemy.orm.exc import NoResultFound

from app import annual_limit_client, notify_celery, statsd_client
Expand Down Expand Up @@ -120,7 +119,7 @@ def process_pinpoint_results(self, response):
if not annual_limit_client.was_seeded_today(service_id):
annual_limit_client.set_seeded_at(service_id)
notifications_to_seed = fetch_notification_status_for_service_for_day(
convert_utc_to_local_timezone(datetime.utcnow()),
datetime.now(timezone.utc),
service_id=service_id,
)
annual_limit_client.seed_annual_limit_notifications(
Expand Down
5 changes: 2 additions & 3 deletions app/celery/process_ses_receipts_tasks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from datetime import datetime
from datetime import datetime, timezone

from flask import current_app, json
from notifications_utils.statsd_decorators import statsd
from notifications_utils.timezones import convert_utc_to_local_timezone
from sqlalchemy.orm.exc import NoResultFound

from app import annual_limit_client, bounce_rate_client, notify_celery, statsd_client
Expand Down Expand Up @@ -97,7 +96,7 @@ def process_ses_results(self, response): # noqa: C901
if not annual_limit_client.was_seeded_today(service_id):
annual_limit_client.set_seeded_at(service_id)
notifications_to_seed = fetch_notification_status_for_service_for_day(
convert_utc_to_local_timezone(datetime.utcnow()),
datetime.now(timezone.utc),
service_id=service_id,
)
annual_limit_client.seed_annual_limit_notifications(
Expand Down
5 changes: 2 additions & 3 deletions app/celery/process_sns_receipts_tasks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from datetime import datetime
from datetime import datetime, timezone

from flask import current_app, json
from notifications_utils.statsd_decorators import statsd
from notifications_utils.timezones import convert_utc_to_local_timezone
from sqlalchemy.orm.exc import NoResultFound

from app import annual_limit_client, notify_celery, statsd_client
Expand Down Expand Up @@ -77,7 +76,7 @@ def process_sns_results(self, response):
if not annual_limit_client.was_seeded_today(service_id):
annual_limit_client.set_seeded_at(service_id)
notifications_to_seed = fetch_notification_status_for_service_for_day(
convert_utc_to_local_timezone(datetime.utcnow()),
datetime.now(timezone.utc),
service_id=service_id,
)
annual_limit_client.seed_annual_limit_notifications(
Expand Down
2 changes: 1 addition & 1 deletion app/dao/date_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def utc_midnight_n_days_ago(number_of_days):
"""
Returns utc midnight a number of days ago.
"""
return get_midnight(datetime.utcnow() - timedelta(days=number_of_days))
return get_midnight(datetime.now(timezone.utc) - timedelta(days=number_of_days))


def get_query_date_based_on_retention_period(retention_period):
Expand Down
7 changes: 5 additions & 2 deletions app/dao/fact_notification_status_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ def fetch_notification_stats_for_trial_services():


def fetch_notification_status_for_service_for_day(bst_day, service_id):
# Fetch data from bst_day 00:00:00 to bst_day 23:59:59
# bst_dat is currently in UTC and the return is in UTC
bst_day = bst_day.replace(hour=0, minute=0, second=0)
return (
db.session.query(
# return current month as a datetime so the data has the same shape as the ft_notification_status query
Expand All @@ -256,8 +259,8 @@ def fetch_notification_status_for_service_for_day(bst_day, service_id):
func.count().label("count"),
)
.filter(
Notification.created_at >= get_local_timezone_midnight_in_utc(bst_day),
Notification.created_at < get_local_timezone_midnight_in_utc(bst_day + timedelta(days=1)),
Notification.created_at >= bst_day,
Notification.created_at < bst_day + timedelta(days=1),
Notification.service_id == service_id,
Notification.key_type != KEY_TYPE_TEST,
)
Expand Down
10 changes: 6 additions & 4 deletions app/service/rest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import itertools
from datetime import datetime
from datetime import datetime, timezone

from flask import Blueprint, current_app, jsonify, request
from notifications_utils.clients.redis import (
Expand Down Expand Up @@ -651,10 +651,12 @@ def get_monthly_notification_stats(service_id):
stats = fetch_notification_status_for_service_by_month(start_date, end_date, service_id)
statistics.add_monthly_notification_status_stats(data, stats)

now = datetime.utcnow()
now = datetime.now(timezone.utc)
# end_date doesn't have tzinfo, so we need to remove it from now
end_date_now = now.replace(tzinfo=None)
# TODO FF_ANNUAL_LIMIT removal
if not current_app.config["FF_ANNUAL_LIMIT"] and end_date > now:
todays_deltas = fetch_notification_status_for_service_for_day(convert_utc_to_local_timezone(now), service_id=service_id)
if not current_app.config["FF_ANNUAL_LIMIT"] and end_date > end_date_now:
todays_deltas = fetch_notification_status_for_service_for_day(now, service_id=service_id)
statistics.add_monthly_notification_status_stats(data, todays_deltas)

return jsonify(data=data)
Expand Down
Loading