2016-08-24 16:24:30 +01:00
|
|
|
from datetime import date, timedelta, datetime
|
|
|
|
|
from sqlalchemy import desc, asc, cast, Date as sql_date
|
2016-01-15 11:12:05 +00:00
|
|
|
from app import db
|
2016-05-25 11:13:49 +01:00
|
|
|
from app.dao import days_ago
|
2016-08-23 16:46:58 +01:00
|
|
|
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()
|
2016-01-15 11:12:05 +00:00
|
|
|
|
|
|
|
|
|
2016-02-24 17:12:30 +00:00
|
|
|
def dao_get_job_by_service_id_and_job_id(service_id, job_id):
|
2016-03-11 12:39:55 +00:00
|
|
|
return Job.query.filter_by(service_id=service_id, id=job_id).one()
|
2016-01-15 11:12:05 +00:00
|
|
|
|
|
|
|
|
|
2016-05-24 17:21:04 +01:00
|
|
|
def dao_get_jobs_by_service_id(service_id, limit_days=None):
|
|
|
|
|
query_filter = [Job.service_id == service_id]
|
|
|
|
|
if limit_days is not None:
|
2016-05-25 11:13:49 +01:00
|
|
|
query_filter.append(cast(Job.created_at, sql_date) >= days_ago(limit_days))
|
2016-05-24 17:21:04 +01:00
|
|
|
return Job.query.filter(*query_filter).order_by(desc(Job.created_at)).all()
|
2016-01-15 11:12:05 +00:00
|
|
|
|
|
|
|
|
|
2016-02-24 17:12:30 +00:00
|
|
|
def dao_get_job_by_id(job_id):
|
2016-03-11 15:34:20 +00:00
|
|
|
return Job.query.filter_by(id=job_id).one()
|
2016-02-24 17:12:30 +00:00
|
|
|
|
|
|
|
|
|
2016-08-24 16:24:30 +01:00
|
|
|
def dao_get_scheduled_jobs():
|
|
|
|
|
return Job.query \
|
|
|
|
|
.filter(Job.job_status == 'scheduled', Job.scheduled_for < datetime.utcnow()) \
|
|
|
|
|
.order_by(asc(Job.scheduled_for)) \
|
|
|
|
|
.all()
|
|
|
|
|
|
|
|
|
|
|
2016-02-24 17:12:30 +00:00
|
|
|
def dao_create_job(job):
|
|
|
|
|
db.session.add(job)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_update_job(job):
|
|
|
|
|
db.session.add(job)
|
|
|
|
|
db.session.commit()
|