Merge pull request #2984 from alphagov/add-stats-endpoint-for-scheduled-jobs

Add an endpoint for stats about scheduled jobs
This commit is contained in:
Chris Hill-Scott
2020-09-29 10:24:09 +01:00
committed by GitHub
3 changed files with 81 additions and 1 deletions

View File

@@ -84,6 +84,18 @@ def dao_get_jobs_by_service_id(
.paginate(page=page, per_page=page_size)
def dao_get_scheduled_job_stats(
service_id,
):
return db.session.query(
func.count(Job.id),
func.min(Job.scheduled_for),
).filter(
Job.service_id == service_id,
Job.job_status == JOB_STATUS_SCHEDULED,
).one()
def dao_get_job_by_id(job_id):
return Job.query.filter_by(id=job_id).one()

View File

@@ -1,4 +1,5 @@
import dateutil
import pytz
from flask import (
Blueprint,
jsonify,
@@ -14,6 +15,7 @@ from app.dao.jobs_dao import (
dao_get_jobs_by_service_id,
dao_get_future_scheduled_job_by_id_and_service_id,
dao_get_notification_outcomes_for_job,
dao_get_scheduled_job_stats,
dao_cancel_letter_job,
can_letter_job_be_cancelled
)
@@ -188,6 +190,18 @@ def create_job(service_id):
return jsonify(data=job_json), 201
@job_blueprint.route('/scheduled-job-stats', methods=['GET'])
def get_scheduled_job_stats(service_id):
count, soonest_scheduled_for = dao_get_scheduled_job_stats(service_id)
return jsonify(
count=count,
soonest_scheduled_for=(
soonest_scheduled_for.replace(tzinfo=pytz.UTC).isoformat()
if soonest_scheduled_for else None
),
), 200
def get_paginated_jobs(
service_id,
*,