<commit_before>from datetime import datetime

from flask import Blueprint, jsonify, request

from app.dao.date_util import get_financial_year_for_datetime
from app.dao.fact_billing_dao import (
    fetch_billing_details_for_all_services,
    fetch_daily_sms_provider_volumes_for_platform,
    fetch_daily_volumes_for_platform,
    fetch_letter_costs_and_totals_for_all_services,
    fetch_letter_line_items_for_all_services,
    fetch_sms_billing_for_all_services,
    fetch_volumes_by_service,
)
from app.dao.fact_notification_status_dao import (
    fetch_notification_status_totals_for_all_services,
)
from app.errors import InvalidRequest, register_errors
from app.models import UK_POSTAGE_TYPES
from app.platform_stats.platform_stats_schema import platform_stats_request
from app.schema_validation import validate
from app.service.statistics import format_admin_stats
from app.utils import get_london_midnight_in_utc

platform_stats_blueprint = Blueprint('platform_stats', __name__)

register_errors(platform_stats_blueprint)


@platform_stats_blueprint.route('')
def get_platform_stats():
    if request.args:
        validate(request.args, platform_stats_request)

    # If start and end date are not set, we are expecting today's stats.
    today = str(datetime.utcnow().date())

    start_date = datetime.strptime(request.args.get('start_date', today), '%Y-%m-%d').date()
    end_date = datetime.strptime(request.args.get('end_date', today), '%Y-%m-%d').date()
    data = fetch_notification_status_totals_for_all_services(start_date=start_date, end_date=end_date)
    stats = format_admin_stats(data)

    return jsonify(stats)


def validate_date_format(date_to_validate):
    try:
        validated_date = datetime.strptime(date_to_validate, "%Y-%m-%d").date()
    except ValueError:
        raise InvalidRequest(message="Input must be a date in the format: YYYY-MM-DD", status_code=400)
    return validated_date


def validate_date_range_is_within_a_financial_year(start_date, end_date):
    start_date = validate_date_format(start_date)
    end_date = validate_date_format(end_date)
    if end_date < start_date:
        raise InvalidRequest(message="Start date must be before end date", status_code=400)

    start_fy = get_financial_year_for_datetime(get_london_midnight_in_utc(start_date))
    end_fy = get_financial_year_for_datetime(get_london_midnight_in_utc(end_date))

    if start_fy != end_fy:
        raise InvalidRequest(message="Date must be in a single financial year.", status_code=400)

    return start_date, end_date


@platform_stats_blueprint.route('usage-for-all-services')
@platform_stats_blueprint.route('data-for-billing-report')
def get_data_for_billing_report():
    start_date = request.args.get('start_date')
    end_date = request.args.get('end_date')

    start_date, end_date = validate_date_range_is_within_a_financial_year(start_date, end_date)

    sms_costs = fetch_sms_billing_for_all_services(start_date, end_date)
    letter_overview = fetch_letter_costs_and_totals_for_all_services(start_date, end_date)
    letter_breakdown = fetch_letter_line_items_for_all_services(start_date, end_date)

    lb_by_service = [
        (lb.service_id,
         f"{lb.letters_sent} {postage_description(lb.postage)} letters at {format_letter_rate(lb.letter_rate)}")
        for lb in letter_breakdown
    ]
    combined = {}
    for s in sms_costs:
        if float(s.sms_cost) > 0:
            entry = {<commit_msg>remove unused route<commit_after>from datetime import datetime

from flask import Blueprint, jsonify, request

from app.dao.date_util import get_financial_year_for_datetime
from app.dao.fact_billing_dao import (
    fetch_billing_details_for_all_services,
    fetch_daily_sms_provider_volumes_for_platform,
    fetch_daily_volumes_for_platform,
    fetch_letter_costs_and_totals_for_all_services,
    fetch_letter_line_items_for_all_services,
    fetch_sms_billing_for_all_services,
    fetch_volumes_by_service,
)
from app.dao.fact_notification_status_dao import (
    fetch_notification_status_totals_for_all_services,
)
from app.errors import InvalidRequest, register_errors
from app.models import UK_POSTAGE_TYPES
from app.platform_stats.platform_stats_schema import platform_stats_request
from app.schema_validation import validate
from app.service.statistics import format_admin_stats
from app.utils import get_london_midnight_in_utc

platform_stats_blueprint = Blueprint('platform_stats', __name__)

register_errors(platform_stats_blueprint)


@platform_stats_blueprint.route('')
def get_platform_stats():
    if request.args:
        validate(request.args, platform_stats_request)

    # If start and end date are not set, we are expecting today's stats.
    today = str(datetime.utcnow().date())

    start_date = datetime.strptime(request.args.get('start_date', today), '%Y-%m-%d').date()
    end_date = datetime.strptime(request.args.get('end_date', today), '%Y-%m-%d').date()
    data = fetch_notification_status_totals_for_all_services(start_date=start_date, end_date=end_date)
    stats = format_admin_stats(data)

    return jsonify(stats)


def validate_date_format(date_to_validate):
    try:
        validated_date = datetime.strptime(date_to_validate, "%Y-%m-%d").date()
    except ValueError:
        raise InvalidRequest(message="Input must be a date in the format: YYYY-MM-DD", status_code=400)
    return validated_date


def validate_date_range_is_within_a_financial_year(start_date, end_date):
    start_date = validate_date_format(start_date)
    end_date = validate_date_format(end_date)
    if end_date < start_date:
        raise InvalidRequest(message="Start date must be before end date", status_code=400)

    start_fy = get_financial_year_for_datetime(get_london_midnight_in_utc(start_date))
    end_fy = get_financial_year_for_datetime(get_london_midnight_in_utc(end_date))

    if start_fy != end_fy:
        raise InvalidRequest(message="Date must be in a single financial year.", status_code=400)

    return start_date, end_date


@platform_stats_blueprint.route('data-for-billing-report')
def get_data_for_billing_report():
    start_date = request.args.get('start_date')
    end_date = request.args.get('end_date')

    start_date, end_date = validate_date_range_is_within_a_financial_year(start_date, end_date)

    sms_costs = fetch_sms_billing_for_all_services(start_date, end_date)
    letter_overview = fetch_letter_costs_and_totals_for_all_services(start_date, end_date)
    letter_breakdown = fetch_letter_line_items_for_all_services(start_date, end_date)

    lb_by_service = [
        (lb.service_id,
         f"{lb.letters_sent} {postage_description(lb.postage)} letters at {format_letter_rate(lb.letter_rate)}")
        for lb in letter_breakdown
    ]
    combined = {}
    for s in sms_costs:
        if float(s.sms_cost) > 0:
            entry = {
{'fraction_non_alphanumeric': 0.080099212138897, 'fraction_numerical': 0.0029180040852057193, 'mean_word_length': 5.454802259887006, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2613129', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2251, 'n_words': 391}