rename days_ago to midnight_n_days_ago

also add some more timezone boundary tests and minor code cleanup
This commit is contained in:
Leo Hemsted
2018-04-30 11:50:56 +01:00
parent 310b8eda4c
commit 0efa223fb2
6 changed files with 54 additions and 25 deletions

View File

@@ -10,7 +10,7 @@ from sqlalchemy import (
)
from app import db
from app.utils import days_ago
from app.utils import midnight_n_days_ago
from app.models import (
Job,
JOB_STATUS_PENDING,
@@ -51,7 +51,7 @@ def dao_get_jobs_by_service_id(service_id, limit_days=None, page=1, page_size=50
Job.original_file_name != current_app.config['ONE_OFF_MESSAGE_FILENAME'],
]
if limit_days is not None:
query_filter.append(Job.created_at >= days_ago(limit_days))
query_filter.append(Job.created_at >= midnight_n_days_ago(limit_days))
if statuses is not None and statuses != ['']:
query_filter.append(
Job.job_status.in_(statuses)

View File

@@ -21,7 +21,7 @@ from sqlalchemy.sql import functions
from notifications_utils.international_billing_rates import INTERNATIONAL_BILLING_RATES
from app import db, create_uuid
from app.utils import days_ago
from app.utils import midnight_n_days_ago
from app.errors import InvalidRequest
from app.models import (
Notification,
@@ -248,7 +248,7 @@ def get_notifications_for_service(
filters = [Notification.service_id == service_id]
if limit_days is not None:
filters.append(Notification.created_at >= days_ago(limit_days))
filters.append(Notification.created_at >= midnight_n_days_ago(limit_days))
if older_than is not None:
older_than_created_at = db.session.query(

View File

@@ -39,7 +39,7 @@ def get_template_statistics_for_service_by_day(service_id):
if limit_days < 1 or limit_days > 7:
raise InvalidRequest({'limit_days': ['limit_days must be between 1 and 7']}, status_code=400)
return jsonify(data=get_template_statistics_for_last_n_days(service_id, limit_days))
return jsonify(data=_get_template_statistics_for_last_n_days(service_id, limit_days))
@template_statistics.route('/<template_id>')
@@ -58,7 +58,7 @@ def get_template_statistics_for_template_id(service_id, template_id):
return jsonify(data=data)
def get_template_statistics_for_last_n_days(service_id, limit_days):
def _get_template_statistics_for_last_n_days(service_id, limit_days):
template_stats_by_id = Counter()
for day in last_n_days(limit_days):
@@ -77,6 +77,7 @@ def get_template_statistics_for_last_n_days(service_id, limit_days):
# if there is data in db, but not in redis - lets put it in redis so we don't have to do
# this calc again next time. If there isn't any data, we can't put it in redis.
# Zero length hashes aren't a thing in redis. (There'll only be no data if the service has no templates)
# Nothing is stored if redis is down.
if stats:
redis_store.set_hash_and_expire(
key,

View File

@@ -81,7 +81,7 @@ def cache_key_for_service_template_counter(service_id, limit_days=7):
def cache_key_for_service_template_usage_per_day(service_id, datetime):
"""
You should probably pass a BST datetime into this function
You should pass a BST datetime into this function
"""
return "service-{}-template-usage-{}".format(service_id, datetime.date().isoformat())
@@ -95,7 +95,7 @@ def get_public_notify_type_text(notify_type, plural=False):
return '{}{}'.format(notify_type_text, 's' if plural else '')
def days_ago(number_of_days):
def midnight_n_days_ago(number_of_days):
"""
Returns midnight a number of days ago. Takes care of daylight savings etc.
"""