mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-29 22:11:52 -05:00
@@ -17,7 +17,11 @@ from app.dao.api_key_dao import (
|
||||
save_model_api_key,
|
||||
)
|
||||
from app.dao.dao_utils import dao_rollback, transaction
|
||||
from app.dao.date_util import get_calendar_year
|
||||
from app.dao.date_util import (
|
||||
get_calendar_year,
|
||||
get_month_start_and_end_date_in_utc,
|
||||
get_number_of_days_for_month,
|
||||
)
|
||||
from app.dao.fact_notification_status_dao import (
|
||||
fetch_monthly_template_usage_for_service,
|
||||
fetch_notification_status_for_service_by_month,
|
||||
@@ -64,12 +68,14 @@ from app.dao.services_dao import (
|
||||
dao_fetch_live_services_data,
|
||||
dao_fetch_service_by_id,
|
||||
dao_fetch_stats_for_service_from_day,
|
||||
dao_fetch_stats_for_service_from_day_for_user,
|
||||
dao_fetch_todays_stats_for_all_services,
|
||||
dao_fetch_todays_stats_for_service,
|
||||
dao_remove_user_from_service,
|
||||
dao_resume_service,
|
||||
dao_suspend_service,
|
||||
dao_update_service,
|
||||
fetch_notification_stats_for_service_by_month_by_user,
|
||||
get_services_by_partial_name,
|
||||
)
|
||||
from app.dao.templates_dao import dao_get_template_by_id
|
||||
@@ -223,20 +229,17 @@ def get_service_statistics_for_specific_days(service_id, start, days=1):
|
||||
|
||||
if days == 1:
|
||||
stats = {}
|
||||
stats[start] = {
|
||||
"value": statistics.format_statistics(
|
||||
dao_fetch_stats_for_service_from_day(service_id, start_date)
|
||||
)
|
||||
}
|
||||
stats[start] = statistics.format_statistics(
|
||||
dao_fetch_stats_for_service_from_day(service_id, start_date)
|
||||
)
|
||||
else:
|
||||
stats = {}
|
||||
for d in range(days):
|
||||
new_date = start_date + timedelta(days=d)
|
||||
key = new_date.strftime("%Y-%m-%d")
|
||||
value = statistics.format_statistics(
|
||||
stats[key] = statistics.format_statistics(
|
||||
dao_fetch_stats_for_service_from_day(service_id, new_date)
|
||||
)
|
||||
stats[key] = {"value": value}
|
||||
|
||||
return stats
|
||||
|
||||
@@ -612,6 +615,7 @@ 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()
|
||||
@@ -624,6 +628,72 @@ def get_monthly_notification_stats(service_id):
|
||||
return jsonify(data=data)
|
||||
|
||||
|
||||
@service_blueprint.route(
|
||||
"/<uuid:service_id>/notifications/<uuid:user_id>/monthly", methods=["GET"]
|
||||
)
|
||||
def get_monthly_notification_stats_by_user(service_id, user_id):
|
||||
# check service_id validity
|
||||
dao_fetch_service_by_id(service_id)
|
||||
# user = get_user_by_id(user_id=user_id)
|
||||
|
||||
try:
|
||||
year = int(request.args.get("year", "NaN"))
|
||||
except ValueError:
|
||||
raise InvalidRequest("Year must be a number", status_code=400)
|
||||
|
||||
start_date, end_date = get_calendar_year(year)
|
||||
|
||||
data = statistics.create_empty_monthly_notification_status_stats_dict(year)
|
||||
|
||||
stats = fetch_notification_stats_for_service_by_month_by_user(
|
||||
start_date, end_date, service_id, user_id
|
||||
)
|
||||
|
||||
statistics.add_monthly_notification_status_stats(data, stats)
|
||||
|
||||
now = datetime.utcnow()
|
||||
if 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)
|
||||
|
||||
|
||||
@service_blueprint.route(
|
||||
"/<uuid:service_id>/notifications/<uuid:user_id>/month", methods=["GET"]
|
||||
)
|
||||
def get_single_month_notification_stats_by_user(service_id, user_id):
|
||||
# check service_id validity
|
||||
dao_fetch_service_by_id(service_id)
|
||||
|
||||
try:
|
||||
month = int(request.args.get("month", "NaN"))
|
||||
year = int(request.args.get("year", "NaN"))
|
||||
except ValueError:
|
||||
raise InvalidRequest(
|
||||
"Both a month and year are required as numbers", status_code=400
|
||||
)
|
||||
|
||||
month_year = datetime(year, month, 10, 00, 00, 00)
|
||||
days = get_number_of_days_for_month(year, month)
|
||||
start_date, end_date = get_month_start_and_end_date_in_utc(month_year)
|
||||
|
||||
stats = {}
|
||||
for d in range(days):
|
||||
new_date = start_date + timedelta(days=d)
|
||||
if new_date <= end_date:
|
||||
key = new_date.strftime("%Y-%m-%d")
|
||||
stats[key] = statistics.format_statistics(
|
||||
dao_fetch_stats_for_service_from_day_for_user(
|
||||
service_id, new_date, user_id
|
||||
)
|
||||
)
|
||||
|
||||
return jsonify(stats)
|
||||
|
||||
|
||||
def get_detailed_service(service_id, today_only=False):
|
||||
service = dao_fetch_service_by_id(service_id)
|
||||
|
||||
|
||||
@@ -113,7 +113,6 @@ def create_empty_monthly_notification_status_stats_dict(year):
|
||||
def add_monthly_notification_status_stats(data, stats):
|
||||
for row in stats:
|
||||
month = row.month.strftime("%Y-%m")
|
||||
|
||||
data[month][row.notification_type][row.notification_status] += row.count
|
||||
|
||||
data[month][row.notification_type][StatisticsType.REQUESTED] += row.count
|
||||
return data
|
||||
|
||||
Reference in New Issue
Block a user