2016-09-19 17:24:26 +01:00
|
|
|
from sqlalchemy import func
|
2016-04-21 11:37:38 +01:00
|
|
|
|
2016-07-29 16:39:51 +01:00
|
|
|
from app import db
|
|
|
|
|
from app.models import (
|
|
|
|
|
NotificationHistory,
|
|
|
|
|
SMS_TYPE,
|
|
|
|
|
EMAIL_TYPE,
|
2016-08-01 10:22:22 +01:00
|
|
|
NOTIFICATION_STATUS_TYPES_BILLABLE,
|
|
|
|
|
KEY_TYPE_TEST
|
2016-08-23 12:05:47 +01:00
|
|
|
)
|
2017-01-26 16:40:52 +00:00
|
|
|
from app.dao.notifications_dao import get_financial_year
|
2016-04-28 12:01:27 +01:00
|
|
|
|
|
|
|
|
|
2017-01-26 16:40:52 +00:00
|
|
|
def get_fragment_count(service_id, year=None):
|
2016-08-01 10:22:22 +01:00
|
|
|
shared_filters = [
|
|
|
|
|
NotificationHistory.service_id == service_id,
|
|
|
|
|
NotificationHistory.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE),
|
|
|
|
|
NotificationHistory.key_type != KEY_TYPE_TEST
|
|
|
|
|
]
|
|
|
|
|
|
2017-01-26 16:40:52 +00:00
|
|
|
if year:
|
|
|
|
|
shared_filters.append(NotificationHistory.created_at.between(
|
|
|
|
|
*get_financial_year(year)
|
|
|
|
|
))
|
|
|
|
|
|
2016-07-29 16:39:51 +01:00
|
|
|
sms_count = db.session.query(
|
2016-08-03 16:26:11 +01:00
|
|
|
func.sum(NotificationHistory.billable_units)
|
2016-07-29 16:39:51 +01:00
|
|
|
).filter(
|
|
|
|
|
NotificationHistory.notification_type == SMS_TYPE,
|
2016-08-01 10:22:22 +01:00
|
|
|
*shared_filters
|
2016-07-29 16:39:51 +01:00
|
|
|
)
|
2016-08-01 10:22:22 +01:00
|
|
|
|
2016-07-29 16:39:51 +01:00
|
|
|
email_count = db.session.query(
|
|
|
|
|
func.count(NotificationHistory.id)
|
|
|
|
|
).filter(
|
|
|
|
|
NotificationHistory.notification_type == EMAIL_TYPE,
|
2016-08-01 10:22:22 +01:00
|
|
|
*shared_filters
|
2016-07-29 16:39:51 +01:00
|
|
|
)
|
|
|
|
|
return {
|
|
|
|
|
'sms_count': int(sms_count.scalar() or 0),
|
|
|
|
|
'email_count': email_count.scalar() or 0
|
|
|
|
|
}
|