mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 15:31:15 -05:00
Add endpoint for breakdown of activity by month
This endpoint will eventualy replace the weekly breakdown one. By month for a given financial year is better, because it gives us consistency with the breakdown of financial usage (and eventually consistency with the template usage). The code to do this is a bit convoluted, in order to fill out the counts for months and statuses where we don’t have notifications. This will make the admin side of this easier, because we can rely on there always being numbers available. The admin side will deal with summing the statuses (eg `temporary-failure` > `failed`) because this is presentational. This commit also modifies the usage count to use `.between()` for consistency.
This commit is contained in:
@@ -9,6 +9,7 @@ from app.dao.dao_utils import (
|
||||
transactional,
|
||||
version_class
|
||||
)
|
||||
from app.dao.notifications_dao import get_financial_year
|
||||
from app.models import (
|
||||
NotificationStatistics,
|
||||
TemplateStatistics,
|
||||
@@ -24,7 +25,10 @@ from app.models import (
|
||||
User,
|
||||
InvitedUser,
|
||||
Service,
|
||||
KEY_TYPE_TEST)
|
||||
KEY_TYPE_TEST,
|
||||
NOTIFICATION_STATUS_TYPES,
|
||||
TEMPLATE_TYPES,
|
||||
)
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
|
||||
@@ -238,6 +242,56 @@ def dao_fetch_weekly_historical_stats_for_service(service_id):
|
||||
).all()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_fetch_monthly_historical_stats_for_service(service_id, year):
|
||||
monday_of_notification_week = func.date_trunc('week', NotificationHistory.created_at).label('week_start')
|
||||
start, end = get_financial_year(year)
|
||||
|
||||
month = func.date_trunc(
|
||||
"month",
|
||||
func.timezone(
|
||||
"Europe/London",
|
||||
func.timezone("UTC", NotificationHistory.created_at)
|
||||
)
|
||||
)
|
||||
|
||||
rows = db.session.query(
|
||||
NotificationHistory.notification_type,
|
||||
NotificationHistory.status,
|
||||
month,
|
||||
func.count(NotificationHistory.id).label('count')
|
||||
).filter(
|
||||
NotificationHistory.service_id == service_id,
|
||||
NotificationHistory.created_at.between(*get_financial_year(year)),
|
||||
).group_by(
|
||||
NotificationHistory.notification_type,
|
||||
NotificationHistory.status,
|
||||
month
|
||||
).order_by(
|
||||
month
|
||||
)
|
||||
|
||||
months = {
|
||||
datetime.strftime(date, '%Y-%m'): dict.fromkeys(
|
||||
TEMPLATE_TYPES,
|
||||
dict.fromkeys(
|
||||
NOTIFICATION_STATUS_TYPES,
|
||||
0
|
||||
)
|
||||
)
|
||||
for date in [
|
||||
datetime(year, month, 1) for month in range(4, 13)
|
||||
] + [
|
||||
datetime(year + 1, month, 1) for month in range(1, 4)
|
||||
]
|
||||
}
|
||||
|
||||
for notification_type, status, date, count in rows:
|
||||
months[datetime.strftime(date, "%Y-%m")][notification_type][status] = count
|
||||
|
||||
return months
|
||||
|
||||
|
||||
@statsd(namespace='dao')
|
||||
def dao_fetch_todays_stats_for_all_services(include_from_test_key=True):
|
||||
query = db.session.query(
|
||||
|
||||
Reference in New Issue
Block a user