2020-01-08 12:23:09 +00:00
|
|
|
|
from werkzeug.utils import cached_property
|
|
|
|
|
|
|
2025-07-04 16:37:04 -07:00
|
|
|
|
from app.enums import JobStatus, NotificationStatus
|
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
|
|
|
|
|
|
from app.utils.time import is_less_than_days_ago
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Job(JSONModel):
|
|
|
|
|
|
ALLOWED_PROPERTIES = {
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"id",
|
|
|
|
|
|
"service",
|
|
|
|
|
|
"template_name",
|
|
|
|
|
|
"template_version",
|
|
|
|
|
|
"original_file_name",
|
|
|
|
|
|
"created_at",
|
|
|
|
|
|
"notification_count",
|
|
|
|
|
|
"created_by",
|
|
|
|
|
|
"template_type",
|
|
|
|
|
|
"recipient",
|
2020-01-08 12:23:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
def from_id(cls, job_id, service_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return cls(job_api_client.get_job(service_id, job_id)["data"])
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def status(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return self._dict.get("job_status")
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def cancelled(self):
|
2025-07-04 17:22:21 -07:00
|
|
|
|
return self.status == JobStatus.CANCELLED
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def scheduled(self):
|
2025-07-04 17:22:21 -07:00
|
|
|
|
return self.status == JobStatus.SCHEDULED
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
2020-01-17 10:01:39 +00:00
|
|
|
|
@property
|
|
|
|
|
|
def scheduled_for(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return self._dict.get("scheduled_for")
|
2020-01-17 10:01:39 +00:00
|
|
|
|
|
2020-02-04 15:36:55 +00:00
|
|
|
|
@property
|
|
|
|
|
|
def upload_type(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return self._dict.get("upload_type")
|
2020-02-04 15:36:55 +00:00
|
|
|
|
|
2020-01-16 16:58:26 +00:00
|
|
|
|
@property
|
|
|
|
|
|
def processing_started(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if not self._dict.get("processing_started"):
|
2020-01-16 16:58:26 +00:00
|
|
|
|
return None
|
2023-08-25 09:12:23 -07: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(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
outcome["count"]
|
|
|
|
|
|
for outcome in self._dict["statistics"]
|
|
|
|
|
|
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):
|
2025-07-04 16:37:04 -07:00
|
|
|
|
return self._aggregate_statistics(
|
2025-07-04 17:22:21 -07:00
|
|
|
|
NotificationStatus.DELIVERED, NotificationStatus.SENT
|
2025-07-04 16:37:04 -07:00
|
|
|
|
)
|
2020-01-09 15:54:42 +00:00
|
|
|
|
|
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(
|
2025-07-04 17:22:21 -07:00
|
|
|
|
NotificationStatus.FAILED,
|
|
|
|
|
|
NotificationStatus.TECHNICAL_FAILURE,
|
|
|
|
|
|
NotificationStatus.TEMPORARY_FAILURE,
|
|
|
|
|
|
NotificationStatus.PERMANENT_FAILURE,
|
|
|
|
|
|
NotificationStatus.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):
|
2025-07-04 17:22:21 -07:00
|
|
|
|
return self.status != JobStatus.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):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return self._dict["template"]
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
@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,
|
2023-08-25 09:12:23 -07:00
|
|
|
|
)["data"]
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def percentage_complete(self):
|
|
|
|
|
|
return self.notifications_requested / self.notification_count * 100
|
|
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
|
def all_notifications(self):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return self.get_notifications(set_status_filters({}))["notifications"]
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
def uncancellable_notifications(self):
|
2022-12-05 15:33:44 -05:00
|
|
|
|
# TODO: this is redundant now
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return (n for n in self.all_notifications)
|
2020-01-08 12:23:09 +00:00
|
|
|
|
|
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 (
|
2023-08-25 09:12:23 -07:00
|
|
|
|
self.notifications_failed
|
|
|
|
|
|
/ (self.notifications_failed + self.notifications_delivered)
|
|
|
|
|
|
* 100
|
2020-01-08 14:29:56 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@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(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
self.service,
|
|
|
|
|
|
self.id,
|
|
|
|
|
|
status=status,
|
2020-01-08 12:23:09 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def cancel(self):
|
2022-11-22 22:50:47 -05:00
|
|
|
|
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
|
|
|
|
|
2023-04-12 15:35:14 -04:00
|
|
|
|
def __init__(self, service_id, *, page=None, limit_days=None):
|
2020-05-12 17:31:01 +01:00
|
|
|
|
super().__init__(
|
|
|
|
|
|
service_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
|