mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-05 15:08:25 -04:00
Fixed imports
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import uuid
|
import uuid
|
||||||
from datetime import timedelta
|
from datetime import datetime, timedelta
|
||||||
import datetime
|
|
||||||
import pytz
|
import pytz
|
||||||
|
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
@@ -801,9 +800,14 @@ def fetch_notification_stats_for_service_by_month_by_user(
|
|||||||
|
|
||||||
|
|
||||||
def get_specific_days_stats(
|
def get_specific_days_stats(
|
||||||
data, start_date, days=None, end_date=None, total_notifications=None
|
data,
|
||||||
, timezone="UTC"):
|
start_date,
|
||||||
user_timezone = pytz.timezone(timezone if timezone else "UTC")
|
days=None,
|
||||||
|
end_date=None,
|
||||||
|
timezone="UTC",
|
||||||
|
total_notifications=None
|
||||||
|
):
|
||||||
|
user_timezone = pytz.timezone(timezone or "UTC")
|
||||||
|
|
||||||
if days is not None and end_date is not None:
|
if days is not None and end_date is not None:
|
||||||
raise ValueError("Only set days OR set end_date, not both.")
|
raise ValueError("Only set days OR set end_date, not both.")
|
||||||
@@ -812,22 +816,30 @@ def get_specific_days_stats(
|
|||||||
elif end_date is not None:
|
elif end_date is not None:
|
||||||
date_range = list(generate_date_range(start_date, end_date))
|
date_range = list(generate_date_range(start_date, end_date))
|
||||||
else:
|
else:
|
||||||
raise ValueError("Either days or end_date must be set.")
|
raise ValueError("Either 'days' or 'end_date' must be set.")
|
||||||
|
|
||||||
grouped_data = {date: [] for date in gen_range} | {
|
# Group data by local date
|
||||||
day: [row for row in data if row.day.date() == day]
|
grouped_data = {date: [] for date in date_range}
|
||||||
for day in {item.day.date() for item in data}
|
for item in data:
|
||||||
}
|
if not isinstance(item.timestamp, datetime):
|
||||||
|
continue
|
||||||
|
local_datetime = item.timestamp.replace(tzinfo=pytz.utc).astimezone(user_timezone)
|
||||||
|
local_date = local_datetime.date()
|
||||||
|
if local_date in grouped_data:
|
||||||
|
grouped_data[local_date].append(item)
|
||||||
|
|
||||||
stats = {
|
# Build final stats, optionally including total_notifications
|
||||||
day.strftime("%Y-%m-%d"): statistics.format_statistics(
|
stats = {}
|
||||||
grouped_data.get(day, []),
|
for day in date_range:
|
||||||
total_notifications=(
|
formatted_day = day.strftime("%Y-%m-%d")
|
||||||
total_notifications.get(day, 0)
|
day_data = grouped_data[day]
|
||||||
if total_notifications is not None
|
total_for_day = None
|
||||||
else None
|
if total_notifications and day in total_notifications:
|
||||||
),
|
total_for_day = total_notifications[day]
|
||||||
|
|
||||||
|
stats[formatted_day] = statistics.format_statistics(
|
||||||
|
day_data,
|
||||||
|
total_notifications=total_for_day
|
||||||
)
|
)
|
||||||
for day in date_range
|
|
||||||
}
|
|
||||||
return stats
|
return stats
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import itertools
|
import itertools
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime
|
||||||
|
|
||||||
import pytz
|
import pytz
|
||||||
from flask import Blueprint, current_app, jsonify, request
|
from flask import Blueprint, current_app, jsonify, request
|
||||||
@@ -273,11 +273,11 @@ def get_service_statistics_for_specific_days_by_user(
|
|||||||
utc_end_date
|
utc_end_date
|
||||||
) = build_local_and_utc_date_range(start_date_str=start, days=days, timezone=timezone)
|
) = build_local_and_utc_date_range(start_date_str=start, days=days, timezone=timezone)
|
||||||
|
|
||||||
results = dao_fetch_stats_for_service_from_days_for_user(
|
total_notifications, results = dao_fetch_stats_for_service_from_days_for_user(
|
||||||
service_id, start_date, end_date, user_id
|
service_id, utc_start_date, utc_end_date, user_id
|
||||||
)
|
)
|
||||||
|
|
||||||
stats = get_specific_days_stats(results, start_date, days=days)
|
stats = get_specific_days_stats(results, local_start_date, days=days, total_notifications=total_notifications)
|
||||||
|
|
||||||
return stats
|
return stats
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user