2020-09-29 13:17:12 +01:00
|
|
|
|
from datetime import timedelta
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
2020-05-11 10:52:30 +01:00
|
|
|
|
import pytz
|
2020-01-08 12:23:09 +00:00
|
|
|
|
from notifications_utils.letter_timings import (
|
|
|
|
|
|
CANCELLABLE_JOB_LETTER_STATUSES,
|
|
|
|
|
|
get_letter_timings,
|
|
|
|
|
|
letter_can_be_cancelled,
|
|
|
|
|
|
)
|
2020-09-29 13:17:12 +01:00
|
|
|
|
from notifications_utils.timezones import utc_string_to_aware_gmt_datetime
|
2020-01-08 12:23:09 +00:00
|
|
|
|
from werkzeug.utils import cached_property
|
|
|
|
|
|
|
2020-05-12 17:06:23 +01:00
|
|
|
|
from app.models import JSONModel, ModelList, PaginatedModelList
|
2020-01-08 12:23:09 +00:00
|
|
|
|
from app.notify_client.job_api_client import job_api_client
|
|
|
|
|
|
from app.notify_client.notification_api_client import notification_api_client
|
|
|
|
|
|
from app.notify_client.service_api_client import service_api_client
|
2021-06-14 12:40:12 +01:00
|
|
|
|
from app.utils import set_status_filters
|
2021-06-09 13:59:06 +01:00
|
|
|
|
from app.utils.letters import get_letter_printing_statement
|
2021-06-14 12:40:12 +01:00
|
|
|
|
from app.utils.time import is_less_than_days_ago
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Job(JSONModel):
|
|
|
|
|
|
|
|
|
|
|
|
ALLOWED_PROPERTIES = {
|
|
|
|
|
|
'id',
|
|
|
|
|
|
'service',
|
2020-05-12 17:06:23 +01:00
|
|
|
|
'template_name',
|
2020-01-08 12:23:09 +00:00
|
|
|
|
'template_version',
|
|
|
|
|
|
'original_file_name',
|
|
|
|
|
|
'created_at',
|
|
|
|
|
|
'notification_count',
|
|
|
|
|
|
'created_by',
|
2020-02-27 10:13:10 +00:00
|
|
|
|
'template_type',
|
2020-02-27 14:03:03 +00:00
|
|
|
|
'recipient',
|
2020-01-08 12:23:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
def from_id(cls, job_id, service_id):
|
|
|
|
|
|
return cls(job_api_client.get_job(service_id, job_id)['data'])
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def status(self):
|
2020-01-20 15:25:47 +00:00
|
|
|
|
return self._dict.get('job_status')
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def cancelled(self):
|
|
|
|
|
|
return self.status == 'cancelled'
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def scheduled(self):
|
|
|
|
|
|
return self.status == 'scheduled'
|
|
|
|
|
|
|
2020-01-17 10:01:39 +00:00
|
|
|
|
@property
|
|
|
|
|
|
def scheduled_for(self):
|
|
|
|
|
|
return self._dict.get('scheduled_for')
|
|
|
|
|
|
|
2020-02-04 15:36:55 +00:00
|
|
|
|
@property
|
|
|
|
|
|
def upload_type(self):
|
|
|
|
|
|
return self._dict.get('upload_type')
|
|
|
|
|
|
|
2020-02-27 14:03:03 +00:00
|
|
|
|
@property
|
|
|
|
|
|
def pdf_letter(self):
|
|
|
|
|
|
return self.upload_type == 'letter'
|
|
|
|
|
|
|
2020-01-16 16:58:26 +00:00
|
|
|
|
@property
|
|
|
|
|
|
def processing_started(self):
|
|
|
|
|
|
if not self._dict.get('processing_started'):
|
|
|
|
|
|
return None
|
2020-09-28 14:53:42 +01:00
|
|
|
|
return self._dict['processing_started']
|
2020-01-16 16:58:26 +00:00
|
|
|
|
|
2020-01-09 15:54:42 +00:00
|
|
|
|
def _aggregate_statistics(self, *statuses):
|
2020-01-09 15:52:19 +00:00
|
|
|
|
return sum(
|
|
|
|
|
|
outcome['count'] for outcome in self._dict['statistics']
|
2020-01-09 15:54:42 +00:00
|
|
|
|
if not statuses or outcome['status'] in statuses
|
2020-01-09 15:52:19 +00:00
|
|
|
|
)
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
2020-01-09 15:54:42 +00:00
|
|
|
|
@property
|
|
|
|
|
|
def notifications_delivered(self):
|
|
|
|
|
|
return self._aggregate_statistics('delivered', 'sent')
|
|
|
|
|
|
|
2020-01-08 12:23:09 +00:00
|
|
|
|
@property
|
|
|
|
|
|
def notifications_failed(self):
|
2020-01-09 15:54:42 +00:00
|
|
|
|
return self._aggregate_statistics(
|
|
|
|
|
|
'failed', 'technical-failure', 'temporary-failure',
|
|
|
|
|
|
'permanent-failure', 'cancelled',
|
2020-01-09 15:52:19 +00:00
|
|
|
|
)
|
2020-01-08 16:32:08 +00:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def notifications_requested(self):
|
2020-01-09 15:54:42 +00:00
|
|
|
|
return self._aggregate_statistics()
|
2020-01-08 16:32:08 +00:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def notifications_sent(self):
|
|
|
|
|
|
return self.notifications_delivered + self.notifications_failed
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def notifications_sending(self):
|
|
|
|
|
|
if self.scheduled:
|
|
|
|
|
|
return 0
|
2020-01-08 16:32:08 +00:00
|
|
|
|
return self.notification_count - self.notifications_sent
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def notifications_created(self):
|
|
|
|
|
|
return notification_api_client.get_notification_count_for_job_id(
|
|
|
|
|
|
service_id=self.service, job_id=self.id
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def still_processing(self):
|
|
|
|
|
|
return (
|
2022-01-14 12:41:01 +00:00
|
|
|
|
self.status != 'finished' or self.percentage_complete < 100
|
2020-01-08 12:23:09 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
2020-01-08 16:23:43 +00:00
|
|
|
|
@cached_property
|
|
|
|
|
|
def finished_processing(self):
|
2020-01-16 15:40:49 +00:00
|
|
|
|
return self.notification_count == self.notifications_sent
|
2020-01-08 16:23:43 +00:00
|
|
|
|
|
2020-01-16 16:58:26 +00:00
|
|
|
|
@property
|
2020-01-21 14:07:23 +00:00
|
|
|
|
def awaiting_processing_or_recently_processed(self):
|
2020-01-16 16:58:26 +00:00
|
|
|
|
if not self.processing_started:
|
|
|
|
|
|
# Assume that if processing hasn’t started yet then the job
|
|
|
|
|
|
# must have been created recently enough to not have any
|
|
|
|
|
|
# notifications yet
|
|
|
|
|
|
return True
|
2020-09-29 13:17:12 +01:00
|
|
|
|
return is_less_than_days_ago(self.processing_started, 1)
|
2020-01-16 16:58:26 +00:00
|
|
|
|
|
2020-01-08 12:23:09 +00:00
|
|
|
|
@property
|
|
|
|
|
|
def template_id(self):
|
|
|
|
|
|
return self._dict['template']
|
|
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
|
def template(self):
|
|
|
|
|
|
return service_api_client.get_service_template(
|
|
|
|
|
|
service_id=self.service,
|
|
|
|
|
|
template_id=self.template_id,
|
|
|
|
|
|
version=self.template_version,
|
|
|
|
|
|
)['data']
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def percentage_complete(self):
|
|
|
|
|
|
return self.notifications_requested / self.notification_count * 100
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def letter_job_can_be_cancelled(self):
|
|
|
|
|
|
|
|
|
|
|
|
if self.template['template_type'] != 'letter':
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
if any(self.uncancellable_notifications):
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
if not letter_can_be_cancelled(
|
2020-01-21 12:23:30 +00:00
|
|
|
|
'created',
|
|
|
|
|
|
utc_string_to_aware_gmt_datetime(self.created_at).replace(tzinfo=None)
|
2020-01-08 12:23:09 +00:00
|
|
|
|
):
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
2020-05-11 10:52:30 +01:00
|
|
|
|
@property
|
|
|
|
|
|
def letter_printing_statement(self):
|
|
|
|
|
|
if self.upload_type != 'letter_day':
|
|
|
|
|
|
raise TypeError()
|
|
|
|
|
|
return get_letter_printing_statement(
|
|
|
|
|
|
'created',
|
|
|
|
|
|
# We have to make the time just before 5:30pm because a
|
|
|
|
|
|
# letter uploaded at 5:30pm will be printed the next day
|
|
|
|
|
|
(
|
|
|
|
|
|
utc_string_to_aware_gmt_datetime(self.created_at) - timedelta(minutes=1)
|
|
|
|
|
|
).astimezone(pytz.utc).isoformat(),
|
|
|
|
|
|
long_form=False,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2020-01-08 12:23:09 +00:00
|
|
|
|
@cached_property
|
|
|
|
|
|
def all_notifications(self):
|
|
|
|
|
|
return self.get_notifications(set_status_filters({}))['notifications']
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def uncancellable_notifications(self):
|
|
|
|
|
|
return (
|
|
|
|
|
|
n for n in self.all_notifications
|
|
|
|
|
|
if n['status'] not in CANCELLABLE_JOB_LETTER_STATUSES
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
|
def postage(self):
|
|
|
|
|
|
# There might be no notifications if the job has only just been
|
|
|
|
|
|
# created and the tasks haven't run yet
|
|
|
|
|
|
try:
|
|
|
|
|
|
return self.all_notifications[0]['postage']
|
|
|
|
|
|
except IndexError:
|
|
|
|
|
|
return self.template['postage']
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def letter_timings(self):
|
|
|
|
|
|
return get_letter_timings(self.created_at, postage=self.postage)
|
|
|
|
|
|
|
2020-01-08 14:29:56 +00:00
|
|
|
|
@property
|
|
|
|
|
|
def failure_rate(self):
|
|
|
|
|
|
if not self.notifications_delivered:
|
|
|
|
|
|
return 100 if self.notifications_failed else 0
|
|
|
|
|
|
return (
|
|
|
|
|
|
self.notifications_failed / (
|
|
|
|
|
|
self.notifications_failed + self.notifications_delivered
|
|
|
|
|
|
) * 100
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def high_failure_rate(self):
|
|
|
|
|
|
return self.failure_rate > 30
|
|
|
|
|
|
|
2020-01-08 12:23:09 +00:00
|
|
|
|
def get_notifications(self, status):
|
|
|
|
|
|
return notification_api_client.get_notifications_for_service(
|
|
|
|
|
|
self.service, self.id, status=status,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def cancel(self):
|
|
|
|
|
|
if self.template_type == 'letter':
|
|
|
|
|
|
return job_api_client.cancel_letter_job(self.service, self.id)
|
|
|
|
|
|
else:
|
|
|
|
|
|
return job_api_client.cancel_job(self.service, self.id)
|
2020-01-08 14:29:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ImmediateJobs(ModelList):
|
2020-01-16 15:57:51 +00:00
|
|
|
|
client_method = job_api_client.get_immediate_jobs
|
2020-01-08 14:29:56 +00:00
|
|
|
|
model = Job
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ScheduledJobs(ImmediateJobs):
|
2020-01-16 15:57:51 +00:00
|
|
|
|
client_method = job_api_client.get_scheduled_jobs
|
2020-01-08 14:29:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
2020-05-12 17:06:23 +01:00
|
|
|
|
class PaginatedJobs(PaginatedModelList, ImmediateJobs):
|
2020-01-16 15:57:51 +00:00
|
|
|
|
client_method = job_api_client.get_page_of_jobs
|
2020-05-12 17:31:01 +01:00
|
|
|
|
statuses = None
|
2020-01-08 14:29:56 +00:00
|
|
|
|
|
2020-12-01 13:34:31 +00:00
|
|
|
|
def __init__(self, service_id, *, contact_list_id=None, page=None, limit_days=None):
|
2020-05-12 17:31:01 +01:00
|
|
|
|
super().__init__(
|
|
|
|
|
|
service_id,
|
|
|
|
|
|
contact_list_id=contact_list_id,
|
|
|
|
|
|
statuses=self.statuses,
|
|
|
|
|
|
page=page,
|
2020-12-01 13:34:31 +00:00
|
|
|
|
limit_days=limit_days,
|
2020-05-12 17:31:01 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PaginatedJobsAndScheduledJobs(PaginatedJobs):
|
|
|
|
|
|
statuses = job_api_client.NON_CANCELLED_JOB_STATUSES
|
2020-01-08 14:29:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
2020-05-12 17:06:23 +01:00
|
|
|
|
class PaginatedUploads(PaginatedModelList, ImmediateJobs):
|
2020-01-16 15:57:51 +00:00
|
|
|
|
client_method = job_api_client.get_uploads
|