def get_template_statistics_for_7_days(limit_days, service_id): cache_key = cache_key_for_service_template_counter(service_id) template_stats_by_id = redis_store.get_all_from_hash(cache_key) if not template_stats_by_id: stats = dao_get_template_usage(service_id, limit_days=limit_days) cache_values = dict([(x.template_id, x.count) for x in stats]) if cache_values: redis_store.set_hash_and_expire( cache_key, cache_values, current_app.config['EXPIRE_CACHE_TEN_MINUTES']) else: stats = dao_get_templates_for_cache(template_stats_by_id.items()) return stats
def test_persist_notification_with_optionals(sample_job, sample_api_key, mocker): assert Notification.query.count() == 0 assert NotificationHistory.query.count() == 0 mocked_redis = mocker.patch( 'app.notifications.process_notifications.redis_store.get') mock_service_template_cache = mocker.patch( 'app.notifications.process_notifications.redis_store.get_all_from_hash' ) n_id = uuid.uuid4() created_at = datetime.datetime(2016, 11, 11, 16, 8, 18) persist_notification(template_id=sample_job.template.id, template_version=sample_job.template.version, recipient='+61412345678', service=sample_job.service, personalisation=None, notification_type='sms', api_key_id=sample_api_key.id, key_type=sample_api_key.key_type, created_at=created_at, job_id=sample_job.id, job_row_number=10, client_reference="ref from client", notification_id=n_id, created_by_id=sample_job.created_by_id) assert Notification.query.count() == 1 assert NotificationHistory.query.count() == 1 persisted_notification = Notification.query.all()[0] assert persisted_notification.id == n_id persisted_notification.job_id == sample_job.id assert persisted_notification.job_row_number == 10 assert persisted_notification.created_at == created_at mocked_redis.assert_called_once_with( str(sample_job.service_id) + "-2016-01-01-count") mock_service_template_cache.assert_called_once_with( cache_key_for_service_template_counter(sample_job.service_id)) assert persisted_notification.client_reference == "ref from client" assert persisted_notification.reference is None assert persisted_notification.international is False assert persisted_notification.phone_prefix == '61' assert persisted_notification.rate_multiplier == 1 assert persisted_notification.created_by_id == sample_job.created_by_id assert not persisted_notification.reply_to_text
def persist_notification( *, template_id, template_version, recipient, service, personalisation, notification_type, api_key_id, key_type, created_at=None, job_id=None, job_row_number=None, reference=None, client_reference=None, notification_id=None, simulated=False, created_by_id=None, status=NOTIFICATION_CREATED, reply_to_text=None, status_callback_url=None, status_callback_bearer_token=None, ): notification_created_at = created_at or datetime.utcnow() if not notification_id: notification_id = uuid.uuid4() notification = Notification( id=notification_id, template_id=template_id, template_version=template_version, to=recipient, service_id=service.id, service=service, personalisation=personalisation, notification_type=notification_type, api_key_id=api_key_id, key_type=key_type, created_at=notification_created_at, job_id=job_id, job_row_number=job_row_number, client_reference=client_reference, reference=reference, created_by_id=created_by_id, status=status, reply_to_text=reply_to_text, status_callback_url=status_callback_url, status_callback_bearer_token=status_callback_bearer_token, ) if notification_type == SMS_TYPE: formatted_recipient = validate_and_format_phone_number_and_allow_international( recipient) recipient_info = get_international_phone_info(formatted_recipient) notification.normalised_to = formatted_recipient notification.international = recipient_info.international notification.phone_prefix = recipient_info.country_prefix notification.rate_multiplier = recipient_info.billable_units # We can't use a sender name/ID if the text is sending to an # international number. At the time of writing, this is because Telstra # won't send to an international number unless sending from the number # associated with the subscription. Additionally, Twilio can send from a # sender name/ID, however, it requires configuration and it depends on # the countries in play. if notification.international: notification.reply_to_text = None elif notification_type == EMAIL_TYPE: notification.normalised_to = format_email_address(notification.to) # if simulated create a Notification model to return but do not persist the Notification to the dB if not simulated: dao_create_notification(notification) if key_type != KEY_TYPE_TEST: if redis_store.get(redis.daily_limit_cache_key(service.id)): redis_store.incr(redis.daily_limit_cache_key(service.id)) if redis_store.get_all_from_hash( cache_key_for_service_template_counter(service.id)): redis_store.increment_hash_value( cache_key_for_service_template_counter(service.id), template_id) increment_template_usage_cache(service.id, template_id, notification_created_at) current_app.logger.info("{} {} created at {}".format( notification_type, notification_id, notification_created_at)) return notification
def persist_notification(*, template_id, template_version, recipient, service, personalisation, notification_type, api_key_id, key_type, created_at=None, job_id=None, job_row_number=None, reference=None, client_reference=None, notification_id=None, simulated=False, created_by_id=None, status=NOTIFICATION_CREATED, reply_to_text=None): notification_created_at = created_at or datetime.utcnow() if not notification_id: notification_id = uuid.uuid4() notification = Notification( id=notification_id, template_id=template_id, template_version=template_version, to=recipient, service_id=service.id, service=service, personalisation=personalisation, notification_type=notification_type, api_key_id=api_key_id, key_type=key_type, created_at=notification_created_at, job_id=job_id, job_row_number=job_row_number, client_reference=client_reference, reference=reference, created_by_id=created_by_id, status=status, reply_to_text=reply_to_text, ) if notification_type == SMS_TYPE: formatted_recipient = validate_and_format_phone_number( recipient, international=True) recipient_info = get_international_phone_info(formatted_recipient) notification.normalised_to = formatted_recipient notification.international = recipient_info.international notification.phone_prefix = recipient_info.country_prefix notification.rate_multiplier = recipient_info.billable_units elif notification_type == EMAIL_TYPE: notification.normalised_to = format_email_address(notification.to) # if simulated create a Notification model to return but do not persist the Notification to the dB if not simulated: dao_create_notification(notification) if key_type != KEY_TYPE_TEST: if redis_store.get(redis.daily_limit_cache_key(service.id)): redis_store.incr(redis.daily_limit_cache_key(service.id)) if redis_store.get_all_from_hash( cache_key_for_service_template_counter(service.id)): redis_store.increment_hash_value( cache_key_for_service_template_counter(service.id), template_id) increment_template_usage_cache(service.id, template_id, notification_created_at) current_app.logger.info("{} {} created at {}".format( notification_type, notification_id, notification_created_at)) return notification