mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-03-01 06:40:54 -05:00
Currently requests to the API made from the admin app are going from PaaS admin app to the nginx router ELB, which then routes them back to the api app on PaaS. This makes sense for external requests, but for requests made from the admin app we could skip nginx and go directly to the api PaaS host, which should reduce load on the nginx instances and potentially reduce latency of the api requests. API apps on PaaS are checking the X-Custom-Forwarder header (which is set by nginx on proxy_pass requests) to only allow requests going through the proxy. This adds the custom header to the API client requests, so that they can pass that header check without going through nginx.
99 lines
3.6 KiB
Python
99 lines
3.6 KiB
Python
from collections import defaultdict
|
|
|
|
from app.notify_client import NotifyAdminAPIClient, _attach_current_user
|
|
|
|
|
|
class JobApiClient(NotifyAdminAPIClient):
|
|
|
|
JOB_STATUSES = {
|
|
'scheduled',
|
|
'pending',
|
|
'in progress',
|
|
'finished',
|
|
'cancelled',
|
|
'sending limits exceeded',
|
|
'ready to send',
|
|
'sent to dvla'
|
|
}
|
|
|
|
def __init__(self):
|
|
super().__init__("a" * 73, "b")
|
|
|
|
@staticmethod
|
|
def __convert_statistics(job):
|
|
results = defaultdict(int)
|
|
for outcome in job['statistics']:
|
|
if outcome['status'] in ['failed', 'technical-failure', 'temporary-failure', 'permanent-failure']:
|
|
results['failed'] += outcome['count']
|
|
if outcome['status'] in ['sending', 'pending', 'created']:
|
|
results['sending'] += outcome['count']
|
|
if outcome['status'] in ['delivered', 'sent']:
|
|
results['delivered'] += outcome['count']
|
|
results['requested'] += outcome['count']
|
|
return results
|
|
|
|
def get_job(self, service_id, job_id):
|
|
params = {}
|
|
job = self.get(url='/service/{}/job/{}'.format(service_id, job_id), params=params)
|
|
stats = self.__convert_statistics(job['data'])
|
|
job['data']['notifications_sent'] = stats['delivered'] + stats['failed']
|
|
job['data']['notifications_delivered'] = stats['delivered']
|
|
job['data']['notifications_failed'] = stats['failed']
|
|
job['data']['notifications_requested'] = stats['requested']
|
|
|
|
return job
|
|
|
|
def get_jobs(self, service_id, limit_days=None, statuses=None, page=1):
|
|
params = {'page': page}
|
|
if limit_days is not None:
|
|
params['limit_days'] = limit_days
|
|
if statuses is not None:
|
|
params['statuses'] = ','.join(statuses)
|
|
|
|
jobs = self.get(url='/service/{}/job'.format(service_id), params=params)
|
|
for job in jobs['data']:
|
|
stats = self.__convert_statistics(job)
|
|
job['notifications_sent'] = stats['delivered'] + stats['failed']
|
|
job['notifications_delivered'] = stats['delivered']
|
|
job['notifications_failed'] = stats['failed']
|
|
job['notifications_requested'] = stats['requested']
|
|
|
|
return jobs
|
|
|
|
def create_job(self, job_id, service_id, template_id, original_file_name, notification_count, scheduled_for=None):
|
|
data = {
|
|
"id": job_id,
|
|
"template": template_id,
|
|
"original_file_name": original_file_name,
|
|
"notification_count": notification_count
|
|
}
|
|
|
|
if scheduled_for:
|
|
data.update({'scheduled_for': scheduled_for})
|
|
|
|
data = _attach_current_user(data)
|
|
job = self.post(url='/service/{}/job'.format(service_id), data=data)
|
|
|
|
stats = self.__convert_statistics(job['data'])
|
|
job['data']['notifications_sent'] = stats['delivered'] + stats['failed']
|
|
job['data']['notifications_delivered'] = stats['delivered']
|
|
job['data']['notifications_failed'] = stats['failed']
|
|
job['data']['notifications_requested'] = stats['requested']
|
|
|
|
return job
|
|
|
|
def cancel_job(self, service_id, job_id):
|
|
|
|
job = self.post(
|
|
url='/service/{}/job/{}/cancel'.format(service_id, job_id),
|
|
data={}
|
|
)
|
|
|
|
stats = self.__convert_statistics(job['data'])
|
|
job['data']['notifications_sent'] = stats['delivered'] + stats['failed']
|
|
job['data']['notifications_delivered'] = stats['delivered']
|
|
job['data']['notifications_failed'] = stats['failed']
|
|
job['data']['notifications_requested'] = stats['requested']
|
|
|
|
return job
|