fix flake 8

This commit is contained in:
Kenneth Kehl
2024-08-30 07:10:00 -07:00
6 changed files with 271 additions and 117 deletions

View File

@@ -1056,7 +1056,7 @@ def add_test_users_to_db(generate, state, admin):
current_app.logger.error("Can only be run in development", exc_info=True)
return
for num in range(1, int(generate) + 1):
for num in range(1, int(generate) + 1): # noqa
def fake_email(name):
first_name, last_name = name.split(maxsplit=1)
@@ -1064,7 +1064,7 @@ def add_test_users_to_db(generate, state, admin):
return f"{username}@test.gsa.gov"
name = fake.name()
user = create_user(
create_user(
name=name,
email=fake_email(name),
state=state,

View File

@@ -1,9 +1,10 @@
from datetime import timedelta
from sqlalchemy import Date, case, func
from sqlalchemy import Date, case, cast, func, select, union_all
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.orm import aliased
from sqlalchemy.sql.expression import extract, literal
from sqlalchemy.types import DateTime, Integer
from sqlalchemy.types import DateTime, Integer, Text
from app import db
from app.dao.dao_utils import autocommit
@@ -14,6 +15,9 @@ from app.models import (
NotificationAllTimeView,
Service,
Template,
TemplateFolder,
User,
template_folder_map,
)
from app.utils import (
get_midnight_in_utc,
@@ -126,36 +130,47 @@ def fetch_notification_status_for_service_for_day(fetch_day, service_id):
def fetch_notification_status_for_service_for_today_and_7_previous_days(
service_id, by_template=False, limit_days=7
):
service_id: str, by_template: bool = False, limit_days: int = 7
) -> list[dict | None]:
start_date = midnight_n_days_ago(limit_days)
now = utc_now()
stats_for_7_days = db.session.query(
FactNotificationStatus.notification_type.cast(db.Text).label(
"notification_type"
),
FactNotificationStatus.notification_status.cast(db.Text).label("status"),
now = get_midnight_in_utc(utc_now())
# Query for the last 7 days
stats_for_7_days = select(
cast(FactNotificationStatus.notification_type, Text).label("notification_type"),
cast(FactNotificationStatus.notification_status, Text).label("status"),
*(
[FactNotificationStatus.template_id.label("template_id")]
[
FactNotificationStatus.template_id.label("template_id"),
FactNotificationStatus.local_date.label("date_used"),
]
if by_template
else []
),
FactNotificationStatus.notification_count.label("count"),
).filter(
).where(
FactNotificationStatus.service_id == service_id,
FactNotificationStatus.local_date >= start_date,
FactNotificationStatus.key_type != KeyType.TEST,
)
# Query for today's stats
stats_for_today = (
db.session.query(
Notification.notification_type.cast(db.Text),
Notification.status.cast(db.Text),
*([Notification.template_id] if by_template else []),
select(
cast(Notification.notification_type, Text),
cast(Notification.status, Text),
*(
[
Notification.template_id,
literal(now).label("date_used"),
]
if by_template
else []
),
func.count().label("count"),
)
.filter(
Notification.created_at >= get_midnight_in_utc(now),
.where(
Notification.created_at >= now,
Notification.service_id == service_id,
Notification.key_type != KeyType.TEST,
)
@@ -166,31 +181,67 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(
)
)
all_stats_table = stats_for_7_days.union_all(stats_for_today).subquery()
# Combine the queries using union_all
all_stats_union = union_all(stats_for_7_days, stats_for_today).subquery()
all_stats_alias = aliased(all_stats_union, name="all_stats")
query = db.session.query(
# Final query with optional template joins
query = select(
*(
[
TemplateFolder.name.label("folder"),
Template.name.label("template_name"),
False, # TODO: this is related to is_precompiled_letter
all_stats_table.c.template_id,
False, # TODO: Handle `is_precompiled_letter`
template_folder_map.c.template_folder_id,
all_stats_alias.c.template_id,
User.name.label("created_by"),
Template.created_by_id,
func.max(all_stats_alias.c.date_used).label(
"last_used"
), # Get the most recent date
]
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"),
all_stats_alias.c.notification_type,
all_stats_alias.c.status,
cast(func.sum(all_stats_alias.c.count), Integer).label("count"),
)
if by_template:
query = query.filter(all_stats_table.c.template_id == Template.id)
query = (
query.join(Template, all_stats_alias.c.template_id == Template.id)
.join(User, Template.created_by_id == User.id)
.outerjoin(
template_folder_map, Template.id == template_folder_map.c.template_id
)
.outerjoin(
TemplateFolder,
TemplateFolder.id == template_folder_map.c.template_folder_id,
)
)
return query.group_by(
*([Template.name, all_stats_table.c.template_id] if by_template else []),
all_stats_table.c.notification_type,
all_stats_table.c.status,
).all()
# Group by all necessary fields except date_used
query = query.group_by(
*(
[
TemplateFolder.name,
Template.name,
all_stats_alias.c.template_id,
User.name,
template_folder_map.c.template_folder_id,
Template.created_by_id,
]
if by_template
else []
),
all_stats_alias.c.notification_type,
all_stats_alias.c.status,
)
# Execute the query using Flask-SQLAlchemy's session
result = db.session.execute(query)
return result.mappings().all()
def fetch_notification_status_totals_for_all_services(start_date, end_date):

View File

@@ -23,7 +23,7 @@ def get_template_statistics_for_service_by_day(service_id):
try:
whole_days = int(whole_days)
except ValueError:
error = "{} is not an integer".format(whole_days)
error = f"{whole_days} is not an integer"
message = {"whole_days": [error]}
raise InvalidRequest(message, status_code=400)
@@ -41,6 +41,11 @@ def get_template_statistics_for_service_by_day(service_id):
"count": row.count,
"template_id": str(row.template_id),
"template_name": row.template_name,
"template_folder_id": row.template_folder_id,
"template_folder": row.folder,
"created_by_id": row.created_by_id,
"created_by": row.created_by,
"last_used": row.last_used,
"template_type": row.notification_type,
"status": row.status,
}

View File

@@ -1,4 +1,5 @@
import json
import os
import uuid
from urllib.parse import urlencode
@@ -53,7 +54,7 @@ from app.user.users_schema import (
post_verify_code_schema,
post_verify_webauthn_schema,
)
from app.utils import url_with_token, utc_now
from app.utils import hilite, url_with_token, utc_now
from notifications_utils.recipients import is_us_phone_number, use_numeric_sender
user_blueprint = Blueprint("user", __name__)
@@ -588,13 +589,27 @@ def get_user_login_gov_user():
return jsonify(data=result)
def debug_not_production(msg):
if os.getenv("NOTIFY_ENVIRONMENT") not in ["production"]:
current_app.logger.info(msg)
@user_blueprint.route("/email", methods=["POST"])
def fetch_user_by_email():
email = email_data_request_schema.load(request.get_json())
fetched_user = get_user_by_email(email["email"])
result = fetched_user.serialize()
return jsonify(data=result)
try:
debug_not_production(
hilite(f"enter fetch_user_by_email with {request.get_json()}")
)
email = email_data_request_schema.load(request.get_json())
debug_not_production(hilite(f"request schema loads {email}"))
fetched_user = get_user_by_email(email["email"])
debug_not_production(hilite(f"fetched user is {fetched_user}"))
result = fetched_user.serialize()
debug_not_production(hilite(f"result is serialized to {result}"))
return jsonify(data=result)
except Exception as e:
debug_not_production(hilite(f"Failed with {e}!!"))
raise e
# TODO: Deprecate this GET endpoint