mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-23 11:29:50 -05:00
Merge pull request #204 from alphagov/add-timestamp-to-template-stats
Change sort order for templates
This commit is contained in:
@@ -67,11 +67,9 @@ def dao_get_template_statistics_for_service(service_id, limit_days=None):
|
||||
desc(TemplateStatistics.day)).limit(1).first()
|
||||
if latest_stat:
|
||||
last_date_to_fetch = latest_stat.day - timedelta(days=limit_days)
|
||||
else:
|
||||
last_date_to_fetch = date.today() - timedelta(days=limit_days)
|
||||
filter.append(TemplateStatistics.day > last_date_to_fetch)
|
||||
filter.append(TemplateStatistics.day > last_date_to_fetch)
|
||||
return TemplateStatistics.query.filter(*filter).order_by(
|
||||
desc(TemplateStatistics.day)).join(Template).order_by(func.lower(Template.name)).all()
|
||||
desc(TemplateStatistics.updated_at)).all()
|
||||
|
||||
|
||||
@transactional
|
||||
@@ -102,7 +100,7 @@ def dao_create_notification(notification, notification_type):
|
||||
day=date.today(),
|
||||
service_id=notification.service_id,
|
||||
template_id=notification.template_id
|
||||
).update({'usage_count': TemplateStatistics.usage_count + 1})
|
||||
).update({'usage_count': TemplateStatistics.usage_count + 1, 'updated_at': datetime.utcnow()})
|
||||
|
||||
if update_count == 0:
|
||||
template_stats = TemplateStatistics(template_id=notification.template_id,
|
||||
|
||||
@@ -362,3 +362,9 @@ class TemplateStatistics(db.Model):
|
||||
template = db.relationship('Template')
|
||||
usage_count = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=1)
|
||||
day = db.Column(db.Date, index=True, nullable=False, unique=False, default=datetime.date.today)
|
||||
updated_at = db.Column(
|
||||
db.DateTime,
|
||||
index=False,
|
||||
unique=False,
|
||||
nullable=False,
|
||||
default=datetime.datetime.utcnow)
|
||||
|
||||
32
migrations/versions/0045_template_stats_update_timestamp.py
Normal file
32
migrations/versions/0045_template_stats_update_timestamp.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""empty message
|
||||
|
||||
Revision ID: 0045_template_stats_update_time
|
||||
Revises: 0044_add_template_stats
|
||||
Create Date: 2016-04-05 14:32:45.165755
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '0045_template_stats_update_time'
|
||||
down_revision = '0044_add_template_stats'
|
||||
|
||||
import datetime
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.sql import table, column
|
||||
|
||||
|
||||
def upgrade():
|
||||
### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('template_statistics', sa.Column('updated_at', sa.DateTime(), nullable=True))
|
||||
updated_at = table('template_statistics', column('updated_at'))
|
||||
op.execute(updated_at.update().values(updated_at=datetime.datetime.utcnow()))
|
||||
op.alter_column('template_statistics', 'updated_at', nullable=False)
|
||||
### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_column('template_statistics', 'updated_at')
|
||||
### end Alembic commands ###
|
||||
@@ -1016,39 +1016,3 @@ def test_get_template_stats_for_service_returns_results_from_first_day_with_data
|
||||
def test_get_template_stats_for_service_with_limit_if_no_records_returns_empty_list(sample_template):
|
||||
template_stats = dao_get_template_statistics_for_service(sample_template.service.id, limit_days=7)
|
||||
assert len(template_stats) == 0
|
||||
|
||||
|
||||
def test_get_template_stats_for_service_for_day_returns_stats_in_template_name_order(sample_service, sample_job):
|
||||
|
||||
from app.dao.templates_dao import dao_create_template
|
||||
from app.models import Template
|
||||
|
||||
template_stats = dao_get_template_statistics_for_service(sample_service.id)
|
||||
assert len(template_stats) == 0
|
||||
|
||||
template_names = ['Aardvark', 'ant', 'zebra', 'walrus', 'Donkey', 'Komodo dragon']
|
||||
for name in template_names:
|
||||
data = {
|
||||
'name': name,
|
||||
'template_type': 'sms',
|
||||
'content': 'blah',
|
||||
'service': sample_service
|
||||
}
|
||||
template = Template(**data)
|
||||
dao_create_template(template)
|
||||
|
||||
notification_data = {
|
||||
'to': '+44709123456',
|
||||
'job_id': sample_job.id,
|
||||
'service': template.service,
|
||||
'service_id': template.service.id,
|
||||
'template': template,
|
||||
'template_id': template.id,
|
||||
'created_at': datetime.utcnow()
|
||||
}
|
||||
notification = Notification(**notification_data)
|
||||
dao_create_notification(notification, template.template_type)
|
||||
template_stats = dao_get_template_statistics_for_service(template.service.id)
|
||||
|
||||
for i, name in enumerate(sorted(template_names, key=str.lower)):
|
||||
assert template_stats[i].template.name == name
|
||||
|
||||
Reference in New Issue
Block a user