def fetch_notification_status_for_service_for_today_and_7_previous_days( service_id, by_template=False, limit_days=7): start_date = midnight_n_days_ago(limit_days) now = datetime.now() stats_for_7_days = db.session.query( FactNotificationStatus.notification_type.label('notification_type'), FactNotificationStatus.notification_status.label('status'), *([FactNotificationStatus.template_id.label('template_id')] if by_template else []), 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, *([Notification.template_id] if by_template else []), func.count().label('count')).filter( Notification.created_at >= get_local_timezone_midnight(now), Notification.service_id == service_id, Notification.key_type != KEY_TYPE_TEST).group_by( Notification.notification_type, *([Notification.template_id] if by_template else []), Notification.status) all_stats_table = stats_for_7_days.union_all(stats_for_today).subquery() query = db.session.query( *([ Template.name.label("template_name"), Template.is_precompiled_letter, all_stats_table.c.template_id ] if by_template else []), all_stats_table.c.notification_type, all_stats_table.c.status, func.cast(func.sum(all_stats_table.c.count), Integer).label('count'), ) if by_template: query = query.filter(all_stats_table.c.template_id == Template.id) return query.group_by( *([ Template.name, Template.is_precompiled_letter, all_stats_table.c.template_id ] if by_template else []), all_stats_table.c.notification_type, all_stats_table.c.status, ).all()
def test_get_local_timezone_midnight_returns_expected_date_for_datetime( date_val: datetime, expected_date: datetime): assert get_local_timezone_midnight(date_val) == expected_date
def test_get_local_timezone_midnight_returns_expected_date_for_date( date_val: date, expected_date: datetime): # based upon the comment above we localize date to a datetime with a time of midnight with tz of utc dt = datetime.combine(date_val, datetime.min.time(), tzinfo=timezone.utc) assert get_local_timezone_midnight(dt) == expected_date
def test_get_local_timezone_midnight_returns_expected_date( date, expected_date): assert get_local_timezone_midnight(date) == expected_date