Merge pull request #627 from alphagov/update-job-page-query-2

Returns the outcome statistics for the job on the API call.
This commit is contained in:
minglis
2016-08-25 12:07:20 +01:00
committed by GitHub
5 changed files with 197 additions and 22 deletions

View File

@@ -1,9 +1,24 @@
from datetime import date, timedelta
from sqlalchemy import desc, cast, Date as sql_date
from app import db
from app.dao import days_ago
from app.models import Job
from app.models import Job, NotificationHistory
from app.statsd_decorators import statsd
from sqlalchemy import func, asc
@statsd(namespace="dao")
def dao_get_notification_outcomes_for_job(service_id, job_id):
query = db.session.query(
func.count(NotificationHistory.status).label('count'),
NotificationHistory.status.label('status')
)
return query \
.filter(NotificationHistory.service_id == service_id) \
.filter(NotificationHistory.job_id == job_id)\
.group_by(NotificationHistory.status) \
.order_by(asc(NotificationHistory.status)) \
.all()
def dao_get_job_by_service_id_and_job_id(service_id, job_id):

View File

@@ -8,7 +8,8 @@ from flask import (
from app.dao.jobs_dao import (
dao_create_job,
dao_get_job_by_service_id_and_job_id,
dao_get_jobs_by_service_id
dao_get_jobs_by_service_id,
dao_get_notification_outcomes_for_job
)
from app.dao.services_dao import (
@@ -42,7 +43,11 @@ register_errors(job)
@job.route('/<job_id>', methods=['GET'])
def get_job_by_service_and_job_id(service_id, job_id):
job = dao_get_job_by_service_id_and_job_id(service_id, job_id)
statistics = dao_get_notification_outcomes_for_job(service_id, job_id)
data = job_schema.dump(job).data
data['statistics'] = [{'status': statistic[1], 'count': statistic[0]} for statistic in statistics]
return jsonify(data=data)

View File

@@ -210,7 +210,11 @@ class JobSchema(BaseSchema):
class Meta:
model = models.Job
exclude = ('notifications',)
exclude = (
'notifications',
'notifications_sent',
'notifications_delivered',
'notifications_failed')
strict = True