From af975b89ec63a3fd7a1700f05a1bcf634af314ec Mon Sep 17 00:00:00 2001 From: venusbb Date: Wed, 21 Mar 2018 14:14:16 +0000 Subject: [PATCH] primary key for ft_billing should use notification_type --- app/celery/reporting_tasks.py | 12 +++++- app/models.py | 4 +- .../versions/0181_billing_primary_key.py | 43 +++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 migrations/versions/0181_billing_primary_key.py diff --git a/app/celery/reporting_tasks.py b/app/celery/reporting_tasks.py index c60afa373..e758332ed 100644 --- a/app/celery/reporting_tasks.py +++ b/app/celery/reporting_tasks.py @@ -13,6 +13,7 @@ from sqlalchemy import func, desc, case from app.dao.dao_utils import transactional from notifications_utils.statsd_decorators import statsd from app import notify_celery +from flask import current_app def get_rate(non_letter_rates, letter_rates, notification_type, date, crown=None, rate_multiplier=None): @@ -76,13 +77,16 @@ def create_nightly_billing(day_start=None): 'day_created' ).all() + updated_records = 0 + inserted_records = 0 + for data in transit_data: update_count = FactBilling.query.filter( FactBilling.bst_date == data.day_created, FactBilling.template_id == data.template_id, FactBilling.provider == data.sent_by, # This could be zero - this is a bug that needs to be fixed. FactBilling.rate_multiplier == data.rate_multiplier, - FactBilling.international == data.international, + FactBilling.notification_type == data.notification_type, ).update( {"notifications_sent": data.notifications_sent, "billable_units": data.billable_units}, @@ -106,3 +110,9 @@ def create_nightly_billing(day_start=None): data.rate_multiplier) ) db.session.add(billing_record) + inserted_records += 1 + + updated_records += update_count + + current_app.logger.info('ft_billing: {} rows updated, {} rows inserted' + .format(updated_records, inserted_records)) diff --git a/app/models.py b/app/models.py index f4cbc82af..81c654b5c 100644 --- a/app/models.py +++ b/app/models.py @@ -1774,10 +1774,10 @@ class FactBilling(db.Model): bst_date = db.Column(db.Date, nullable=False, primary_key=True, index=True) template_id = db.Column(UUID(as_uuid=True), nullable=False, primary_key=True, index=True) service_id = db.Column(UUID(as_uuid=True), nullable=False, index=True) - notification_type = db.Column(db.Text, nullable=True) + notification_type = db.Column(db.Text, nullable=False, primary_key=True) provider = db.Column(db.Text, nullable=True, primary_key=True) rate_multiplier = db.Column(db.Numeric(), nullable=True, primary_key=True) - international = db.Column(db.Boolean, nullable=True, primary_key=True) + international = db.Column(db.Boolean, nullable=False, primary_key=False) rate = db.Column(db.Numeric(), nullable=True) billable_units = db.Column(db.Numeric(), nullable=True) notifications_sent = db.Column(db.Integer(), nullable=True) diff --git a/migrations/versions/0181_billing_primary_key.py b/migrations/versions/0181_billing_primary_key.py new file mode 100644 index 000000000..eec3c4950 --- /dev/null +++ b/migrations/versions/0181_billing_primary_key.py @@ -0,0 +1,43 @@ +""" + +Revision ID: 0181_billing_primary_key +Revises: 0180_another_letter_org +Create Date: 2018-03-21 13:41:26.203712 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +revision = '0181_billing_primary_key' +down_revision = '0180_another_letter_org' + + +def upgrade(): + op.alter_column('ft_billing', 'service_id', + existing_type=postgresql.UUID(), + nullable=False) + op.drop_constraint('ft_billing_pkey', 'ft_billing', type_='primary') + + op.create_primary_key('ft_billing_pkey', 'ft_billing', ['bst_date', + 'template_id', + 'rate_multiplier', + 'provider', + 'notification_type']) + op.create_index(op.f('ix_ft_billing_template_id'), 'ft_billing', ['template_id'], unique=False) + + +def downgrade(): + op.alter_column('ft_billing', 'service_id', + existing_type=postgresql.UUID(), + nullable=True) + + op.drop_constraint('ft_billing_pkey', 'ft_billing', type_='primary') + + op.create_primary_key('ft_billing_pkey', 'ft_billing', ['bst_date', + 'template_id', + 'rate_multiplier', + 'provider', + 'international']) + + op.drop_index(op.f('ix_ft_billing_template_id'), table_name='ft_billing')