Adjust attribute key names and data types to work with format_statistics

This commit is contained in:
Pea Tyczynska
2018-11-06 13:30:37 +00:00
parent c37c399741
commit c146b86643
3 changed files with 16 additions and 15 deletions

View File

@@ -4,7 +4,7 @@ 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
@@ -113,12 +113,12 @@ def fetch_notification_status_for_service_for_day(bst_day, service_id):
).all()
def fetch_notification_status_for_service_for_today_and_7_previous_days(service_id):
start_date = midnight_n_days_ago(7)
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('notification_status'),
FactNotificationStatus.notification_status.label('status'),
FactNotificationStatus.notification_count.label('count')
).filter(
FactNotificationStatus.service_id == service_id,
@@ -128,7 +128,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(service_
stats_for_today = db.session.query(
Notification.notification_type.cast(db.Text),
Notification.status.label('notification_status'),
Notification.status,
func.count().label('count')
).filter(
Notification.created_at >= get_london_midnight_in_utc(now),
@@ -141,9 +141,9 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(service_
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.notification_status,
func.sum(all_stats_table.c.count).label('count')
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.notification_status,
all_stats_table.c.status,
).all()

View File

@@ -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
@@ -433,7 +434,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)

View File

@@ -200,23 +200,23 @@ def test_fetch_notification_status_for_service_for_today_and_7_previous_days(not
results = sorted(
fetch_notification_status_for_service_for_today_and_7_previous_days(service_1.id),
key=lambda x: (x.notification_type, x.notification_status)
key=lambda x: (x.notification_type, x.status)
)
assert len(results) == 4
assert results[0].notification_type == 'email'
assert results[0].notification_status == 'delivered'
assert results[0].status == 'delivered'
assert results[0].count == 4
assert results[1].notification_type == 'letter'
assert results[1].notification_status == 'delivered'
assert results[1].status == 'delivered'
assert results[1].count == 5
assert results[2].notification_type == 'sms'
assert results[2].notification_status == 'created'
assert results[2].status == 'created'
assert results[2].count == 2
assert results[3].notification_type == 'sms'
assert results[3].notification_status == 'delivered'
assert results[3].status == 'delivered'
assert results[3].count == 19