2016-07-29 16:39:51 +01:00
|
|
|
from sqlalchemy import func, cast, Float, case
|
2016-04-21 11:37:38 +01:00
|
|
|
|
2016-07-29 16:39:51 +01:00
|
|
|
from app import db
|
|
|
|
|
from app.models import (
|
|
|
|
|
ProviderStatistics,
|
|
|
|
|
ProviderDetails,
|
|
|
|
|
NotificationHistory,
|
|
|
|
|
SMS_TYPE,
|
|
|
|
|
EMAIL_TYPE,
|
|
|
|
|
NOTIFICATION_STATUS_TYPES_BILLABLE
|
2016-04-28 12:01:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2016-07-29 16:39:51 +01:00
|
|
|
def get_provider_statistics(service, **kwargs):
|
|
|
|
|
query = ProviderStatistics.query.filter_by(service=service)
|
2016-04-28 12:01:27 +01:00
|
|
|
if 'providers' in kwargs:
|
2016-05-06 09:09:47 +01:00
|
|
|
providers = ProviderDetails.query.filter(ProviderDetails.identifier.in_(kwargs['providers'])).all()
|
|
|
|
|
provider_ids = [provider.id for provider in providers]
|
|
|
|
|
query = query.filter(ProviderStatistics.provider_id.in_(provider_ids))
|
2016-04-28 12:01:27 +01:00
|
|
|
return query
|
2016-07-29 16:39:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_fragment_count(service_id):
|
|
|
|
|
sms_count = db.session.query(
|
|
|
|
|
func.sum(
|
|
|
|
|
case(
|
|
|
|
|
[
|
|
|
|
|
(
|
|
|
|
|
NotificationHistory.content_char_count <= 160,
|
|
|
|
|
func.ceil(cast(NotificationHistory.content_char_count, Float) / 153)
|
|
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
else_=1
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
).filter(
|
|
|
|
|
NotificationHistory.service_id == service_id,
|
|
|
|
|
NotificationHistory.notification_type == SMS_TYPE,
|
|
|
|
|
NotificationHistory.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE)
|
|
|
|
|
)
|
|
|
|
|
email_count = db.session.query(
|
|
|
|
|
func.count(NotificationHistory.id)
|
|
|
|
|
).filter(
|
|
|
|
|
NotificationHistory.service_id == service_id,
|
|
|
|
|
NotificationHistory.notification_type == EMAIL_TYPE,
|
|
|
|
|
NotificationHistory.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE)
|
|
|
|
|
)
|
|
|
|
|
return {
|
|
|
|
|
'sms_count': int(sms_count.scalar() or 0),
|
|
|
|
|
'email_count': email_count.scalar() or 0
|
|
|
|
|
}
|