mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Merge pull request #2211 from alphagov/fix_notification_statistics_on_dashboard
Fix notification statistics on dashboard
This commit is contained in:
@@ -4,11 +4,11 @@ from flask import current_app
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.dialects.postgresql import insert
|
||||
from sqlalchemy.sql.expression import literal
|
||||
from sqlalchemy.types import DateTime
|
||||
from sqlalchemy.types import DateTime, Integer
|
||||
|
||||
from app import db
|
||||
from app.models import Notification, NotificationHistory, FactNotificationStatus, KEY_TYPE_TEST
|
||||
from app.utils import convert_bst_to_utc, get_london_midnight_in_utc
|
||||
from app.utils import convert_bst_to_utc, get_london_midnight_in_utc, midnight_n_days_ago
|
||||
|
||||
|
||||
def fetch_notification_status_for_day(process_day, service_id=None):
|
||||
@@ -111,3 +111,39 @@ def fetch_notification_status_for_service_for_day(bst_day, service_id):
|
||||
Notification.notification_type,
|
||||
Notification.status
|
||||
).all()
|
||||
|
||||
|
||||
def fetch_notification_status_for_service_for_today_and_7_previous_days(service_id, limit_days=7):
|
||||
start_date = midnight_n_days_ago(limit_days)
|
||||
now = datetime.utcnow()
|
||||
stats_for_7_days = db.session.query(
|
||||
FactNotificationStatus.notification_type.label('notification_type'),
|
||||
FactNotificationStatus.notification_status.label('status'),
|
||||
FactNotificationStatus.notification_count.label('count')
|
||||
).filter(
|
||||
FactNotificationStatus.service_id == service_id,
|
||||
FactNotificationStatus.bst_date >= start_date,
|
||||
FactNotificationStatus.key_type != KEY_TYPE_TEST
|
||||
)
|
||||
|
||||
stats_for_today = db.session.query(
|
||||
Notification.notification_type.cast(db.Text),
|
||||
Notification.status,
|
||||
func.count().label('count')
|
||||
).filter(
|
||||
Notification.created_at >= get_london_midnight_in_utc(now),
|
||||
Notification.service_id == service_id,
|
||||
Notification.key_type != KEY_TYPE_TEST
|
||||
).group_by(
|
||||
Notification.notification_type,
|
||||
Notification.status
|
||||
)
|
||||
all_stats_table = stats_for_7_days.union_all(stats_for_today).subquery()
|
||||
return db.session.query(
|
||||
all_stats_table.c.notification_type,
|
||||
all_stats_table.c.status,
|
||||
func.cast(func.sum(all_stats_table.c.count), Integer).label('count'),
|
||||
).group_by(
|
||||
all_stats_table.c.notification_type,
|
||||
all_stats_table.c.status,
|
||||
).all()
|
||||
|
||||
@@ -20,7 +20,8 @@ from app.dao.api_key_dao import (
|
||||
expire_api_key)
|
||||
from app.dao.fact_notification_status_dao import (
|
||||
fetch_notification_status_for_service_by_month,
|
||||
fetch_notification_status_for_service_for_day
|
||||
fetch_notification_status_for_service_for_day,
|
||||
fetch_notification_status_for_service_for_today_and_7_previous_days
|
||||
)
|
||||
from app.dao.inbound_numbers_dao import dao_allocate_number_for_service
|
||||
from app.dao.organisation_dao import dao_get_organisation_by_service_id
|
||||
@@ -47,7 +48,6 @@ from app.dao.services_dao import (
|
||||
dao_fetch_all_services_by_user,
|
||||
dao_fetch_monthly_historical_usage_by_template_for_service,
|
||||
dao_fetch_service_by_id,
|
||||
dao_fetch_stats_for_service,
|
||||
dao_fetch_todays_stats_for_service,
|
||||
dao_fetch_todays_stats_for_all_services,
|
||||
dao_resume_service,
|
||||
@@ -433,7 +433,7 @@ def get_service_statistics(service_id, today_only, limit_days=7):
|
||||
if today_only:
|
||||
stats = dao_fetch_todays_stats_for_service(service_id)
|
||||
else:
|
||||
stats = dao_fetch_stats_for_service(service_id, limit_days=limit_days)
|
||||
stats = fetch_notification_status_for_service_for_today_and_7_previous_days(service_id, limit_days=limit_days)
|
||||
|
||||
return statistics.format_statistics(stats)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user