<commit_before>from datetime import datetime

from app import db
from app.dao.dao_utils import transactional
from app.dao.date_util import get_month_start_end_date
from app.dao.notification_usage_dao import get_billing_data_for_month
from app.models import MonthlyBilling, SMS_TYPE, NotificationHistory
from app.statsd_decorators import statsd


def get_service_ids_that_need_sms_billing_populated(start_date, end_date):
    return db.session.query(
        NotificationHistory.service_id
    ).filter(
        NotificationHistory.created_at >= start_date,
        NotificationHistory.created_at <= end_date,
        NotificationHistory.notification_type == SMS_TYPE,
        NotificationHistory.billable_units != 0
    ).distinct().all()


@transactional
def create_or_update_monthly_billing_sms(service_id, billing_month):
    start_date, end_date = get_month_start_end_date(billing_month)
    monthly = get_billing_data_for_month(service_id=service_id, start_date=start_date, end_date=end_date)
    # update monthly
    monthly_totals = _monthly_billing_data_to_json(monthly)
    row = MonthlyBilling.query.filter_by(year=billing_month.year,
                                         month=datetime.strftime(billing_month, "%B"),
                                         notification_type='sms').first()
    if row:
        row.monthly_totals = monthly_totals
    else:
        row = MonthlyBilling(service_id=service_id,
                             notification_type=SMS_TYPE,
                             year=billing_month.year,
                             month=datetime.strftime(billing_month, "%B"),
                             monthly_totals=monthly_totals)
    db.session.add(row)


@statsd(namespace="dao")
def get_monthly_billing_sms(service_id, billing_month):
    monthly = MonthlyBilling.query.filter_by(service_id=service_id,
                                             year=billing_month.year,
                                             month=datetime.strftime(billing_month, "%B"),
                                             notification_type=SMS_TYPE).first()
    return monthly


def _monthly_billing_data_to_json(monthly):
    # total cost must take into account the free allowance.
    # might be a good idea to capture free allowance in this table
    return [{"billing_units": x.billing_units,
             "rate_multiplier": x.rate_multiplier,
             "international": x.international,
             "rate": x.rate,
             "total_cost": (x.billing_units * x.rate_multiplier) * x.rate} for x in monthly]<commit_msg>Use the end date for the month, when we are in BST the first day of the month is an hour behind and in the previous month.<commit_after>from datetime import datetime

from app import db
from app.dao.dao_utils import transactional
from app.dao.date_util import get_month_start_end_date
from app.dao.notification_usage_dao import get_billing_data_for_month
from app.models import MonthlyBilling, SMS_TYPE, NotificationHistory
from app.statsd_decorators import statsd


def get_service_ids_that_need_sms_billing_populated(start_date, end_date):
    return db.session.query(
        NotificationHistory.service_id
    ).filter(
        NotificationHistory.created_at >= start_date,
        NotificationHistory.created_at <= end_date,
        NotificationHistory.notification_type == SMS_TYPE,
        NotificationHistory.billable_units != 0
    ).distinct().all()


@transactional
def create_or_update_monthly_billing_sms(service_id, billing_month):
    start_date, end_date = get_month_start_end_date(billing_month)
    monthly = get_billing_data_for_month(service_id=service_id, start_date=start_date, end_date=end_date)
    # update monthly
    monthly_totals = _monthly_billing_data_to_json(monthly)
    row = MonthlyBilling.query.filter_by(year=billing_month.year,
                                         month=datetime.strftime(end_date, "%B"),
                                         notification_type='sms').first()
    if row:
        row.monthly_totals = monthly_totals
    else:
        row = MonthlyBilling(service_id=service_id,
                             notification_type=SMS_TYPE,
                             year=billing_month.year,
                             month=datetime.strftime(end_date, "%B"),
                             monthly_totals=monthly_totals)
    db.session.add(row)


@statsd(namespace="dao")
def get_monthly_billing_sms(service_id, billing_month):
    monthly = MonthlyBilling.query.filter_by(service_id=service_id,
                                             year=billing_month.year,
                                             month=datetime.strftime(billing_month, "%B"),
                                             notification_type=SMS_TYPE).first()
    return monthly


def _monthly_billing_data_to_json(monthly):
    # total cost must take into account the free allowance.
    # might be a good idea to capture free allowance in this table
    return [{"billing_units": x.billing_units,
             "rate_multiplier": x.rate_multiplier,
             "international": x.international,
             "rate": x.rate,
             "total_cost": (x.billing_units * x.rate_multiplier) * x.rate} for x in monthly]
{'fraction_non_alphanumeric': 0.07366199884903127, 'fraction_numerical': 0.0003836562440053712, 'mean_word_length': 2.646153846153846, 'pattern_counts': {'":': 10, '<': 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': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2737034', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1469, 'n_words': 287}