2016-04-14 15:09:59 +01:00
|
|
|
import uuid
|
2016-07-22 15:16:24 +01:00
|
|
|
from datetime import date
|
2016-04-14 15:09:59 +01:00
|
|
|
|
2016-07-18 12:03:44 +01:00
|
|
|
from sqlalchemy import asc, func
|
2016-07-15 11:34:59 +01:00
|
|
|
from sqlalchemy.orm import joinedload
|
2016-02-19 15:53:15 +00:00
|
|
|
|
2016-07-18 12:03:44 +01:00
|
|
|
from app import db
|
2016-04-14 15:09:59 +01:00
|
|
|
from app.dao.dao_utils import (
|
|
|
|
|
transactional,
|
2016-04-21 18:10:57 +01:00
|
|
|
version_class
|
2016-04-14 15:09:59 +01:00
|
|
|
)
|
2016-05-06 11:07:11 +01:00
|
|
|
from app.models import (
|
|
|
|
|
NotificationStatistics,
|
|
|
|
|
TemplateStatistics,
|
|
|
|
|
ProviderStatistics,
|
|
|
|
|
VerifyCode,
|
|
|
|
|
ApiKey,
|
|
|
|
|
Template,
|
2016-08-02 16:23:14 +01:00
|
|
|
TemplateHistory,
|
2016-05-06 11:07:11 +01:00
|
|
|
Job,
|
2016-07-11 16:48:32 +01:00
|
|
|
NotificationHistory,
|
2016-05-06 11:07:11 +01:00
|
|
|
Notification,
|
|
|
|
|
Permission,
|
|
|
|
|
User,
|
|
|
|
|
InvitedUser,
|
2016-09-16 13:47:09 +01:00
|
|
|
Service,
|
|
|
|
|
KEY_TYPE_TEST)
|
2016-08-05 10:44:43 +01:00
|
|
|
from app.statsd_decorators import statsd
|
2016-05-06 11:07:11 +01:00
|
|
|
|
2016-02-19 15:53:15 +00:00
|
|
|
|
|
|
|
|
def dao_fetch_all_services():
|
2016-07-15 11:34:59 +01:00
|
|
|
return Service.query.order_by(
|
|
|
|
|
asc(Service.created_at)
|
|
|
|
|
).options(
|
|
|
|
|
joinedload('users')
|
|
|
|
|
).all()
|
2016-02-19 15:53:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_fetch_service_by_id(service_id):
|
2016-07-15 11:34:59 +01:00
|
|
|
return Service.query.filter_by(
|
|
|
|
|
id=service_id
|
|
|
|
|
).options(
|
|
|
|
|
joinedload('users')
|
|
|
|
|
).one()
|
2016-02-19 15:53:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_fetch_all_services_by_user(user_id):
|
2016-07-15 11:34:59 +01:00
|
|
|
return Service.query.filter(
|
|
|
|
|
Service.users.any(id=user_id)
|
|
|
|
|
).order_by(
|
|
|
|
|
asc(Service.created_at)
|
|
|
|
|
).options(
|
|
|
|
|
joinedload('users')
|
|
|
|
|
).all()
|
2016-02-19 15:53:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_fetch_service_by_id_and_user(service_id, user_id):
|
2016-07-15 11:34:59 +01:00
|
|
|
return Service.query.filter(
|
|
|
|
|
Service.users.any(id=user_id),
|
|
|
|
|
Service.id == service_id
|
|
|
|
|
).options(
|
|
|
|
|
joinedload('users')
|
|
|
|
|
).one()
|
2016-02-19 15:53:15 +00:00
|
|
|
|
|
|
|
|
|
2016-04-14 15:09:59 +01:00
|
|
|
@transactional
|
2016-04-21 18:10:57 +01:00
|
|
|
@version_class(Service)
|
2016-02-19 15:53:15 +00:00
|
|
|
def dao_create_service(service, user):
|
2016-04-14 15:09:59 +01:00
|
|
|
from app.dao.permissions_dao import permission_dao
|
|
|
|
|
service.users.append(user)
|
|
|
|
|
permission_dao.add_default_service_permissions_for_user(user, service)
|
|
|
|
|
service.id = uuid.uuid4() # must be set now so version history model can use same id
|
2016-11-08 13:49:47 +00:00
|
|
|
service.active = True
|
2016-05-31 12:49:06 +01:00
|
|
|
service.research_mode = False
|
2016-04-14 15:09:59 +01:00
|
|
|
db.session.add(service)
|
2016-01-07 17:31:17 +00:00
|
|
|
|
|
|
|
|
|
2016-04-14 15:09:59 +01:00
|
|
|
@transactional
|
2016-04-21 18:10:57 +01:00
|
|
|
@version_class(Service)
|
2016-02-19 15:53:15 +00:00
|
|
|
def dao_update_service(service):
|
|
|
|
|
db.session.add(service)
|
2016-01-12 10:39:49 +00:00
|
|
|
|
|
|
|
|
|
2016-03-23 16:30:47 +00:00
|
|
|
def dao_add_user_to_service(service, user, permissions=[]):
|
|
|
|
|
try:
|
|
|
|
|
from app.dao.permissions_dao import permission_dao
|
|
|
|
|
service.users.append(user)
|
|
|
|
|
permission_dao.set_user_service_permission(user, service, permissions, _commit=False)
|
|
|
|
|
db.session.add(service)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
db.session.rollback()
|
|
|
|
|
raise e
|
|
|
|
|
else:
|
|
|
|
|
db.session.commit()
|
2016-02-19 15:53:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def dao_remove_user_from_service(service, user):
|
2016-03-22 13:14:23 +00:00
|
|
|
try:
|
|
|
|
|
from app.dao.permissions_dao import permission_dao
|
|
|
|
|
permission_dao.remove_user_service_permissions(user, service)
|
|
|
|
|
service.users.remove(user)
|
|
|
|
|
db.session.add(service)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
db.session.rollback()
|
|
|
|
|
raise e
|
|
|
|
|
else:
|
|
|
|
|
db.session.commit()
|
2016-05-06 11:07:11 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def delete_service_and_all_associated_db_objects(service):
|
|
|
|
|
|
|
|
|
|
def _delete_commit(query):
|
|
|
|
|
query.delete()
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
_delete_commit(NotificationStatistics.query.filter_by(service=service))
|
|
|
|
|
_delete_commit(TemplateStatistics.query.filter_by(service=service))
|
|
|
|
|
_delete_commit(ProviderStatistics.query.filter_by(service=service))
|
|
|
|
|
_delete_commit(InvitedUser.query.filter_by(service=service))
|
|
|
|
|
_delete_commit(Permission.query.filter_by(service=service))
|
|
|
|
|
_delete_commit(ApiKey.query.filter_by(service=service))
|
2016-05-11 15:52:49 +01:00
|
|
|
_delete_commit(ApiKey.get_history_model().query.filter_by(service_id=service.id))
|
2016-07-11 16:48:32 +01:00
|
|
|
_delete_commit(NotificationHistory.query.filter_by(service=service))
|
2016-05-06 11:07:11 +01:00
|
|
|
_delete_commit(Notification.query.filter_by(service=service))
|
|
|
|
|
_delete_commit(Job.query.filter_by(service=service))
|
|
|
|
|
_delete_commit(Template.query.filter_by(service=service))
|
2016-08-02 16:23:14 +01:00
|
|
|
_delete_commit(TemplateHistory.query.filter_by(service_id=service.id))
|
2016-05-06 11:07:11 +01:00
|
|
|
|
|
|
|
|
verify_codes = VerifyCode.query.join(User).filter(User.id.in_([x.id for x in service.users]))
|
|
|
|
|
list(map(db.session.delete, verify_codes))
|
|
|
|
|
db.session.commit()
|
|
|
|
|
users = [x for x in service.users]
|
|
|
|
|
map(service.users.remove, users)
|
|
|
|
|
[service.users.remove(x) for x in users]
|
2016-05-11 15:52:49 +01:00
|
|
|
_delete_commit(Service.get_history_model().query.filter_by(id=service.id))
|
2016-05-06 11:07:11 +01:00
|
|
|
db.session.delete(service)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
list(map(db.session.delete, users))
|
|
|
|
|
db.session.commit()
|
2016-07-18 12:03:44 +01:00
|
|
|
|
|
|
|
|
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="dao")
|
2016-07-18 12:03:44 +01:00
|
|
|
def dao_fetch_stats_for_service(service_id):
|
2016-07-22 15:16:24 +01:00
|
|
|
return _stats_for_service_query(service_id).all()
|
|
|
|
|
|
|
|
|
|
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="dao")
|
2016-07-22 15:16:24 +01:00
|
|
|
def dao_fetch_todays_stats_for_service(service_id):
|
|
|
|
|
return _stats_for_service_query(service_id).filter(
|
|
|
|
|
func.date(Notification.created_at) == date.today()
|
|
|
|
|
).all()
|
|
|
|
|
|
|
|
|
|
|
2016-10-03 10:57:10 +01:00
|
|
|
def fetch_todays_total_message_count(service_id):
|
|
|
|
|
result = db.session.query(
|
|
|
|
|
func.count(Notification.id).label('count')
|
|
|
|
|
).filter(
|
|
|
|
|
Notification.service_id == service_id,
|
|
|
|
|
Notification.key_type != KEY_TYPE_TEST,
|
|
|
|
|
func.date(Notification.created_at) == date.today()
|
|
|
|
|
).group_by(
|
|
|
|
|
Notification.notification_type,
|
|
|
|
|
Notification.status,
|
|
|
|
|
).first()
|
|
|
|
|
return 0 if result is None else result.count
|
|
|
|
|
|
|
|
|
|
|
2016-07-22 15:16:24 +01:00
|
|
|
def _stats_for_service_query(service_id):
|
2016-07-18 12:03:44 +01:00
|
|
|
return db.session.query(
|
|
|
|
|
Notification.notification_type,
|
|
|
|
|
Notification.status,
|
|
|
|
|
func.count(Notification.id).label('count')
|
|
|
|
|
).filter(
|
2016-09-16 13:47:09 +01:00
|
|
|
Notification.service_id == service_id,
|
|
|
|
|
Notification.key_type != KEY_TYPE_TEST
|
2016-07-18 12:03:44 +01:00
|
|
|
).group_by(
|
|
|
|
|
Notification.notification_type,
|
|
|
|
|
Notification.status,
|
2016-07-22 15:16:24 +01:00
|
|
|
)
|
2016-07-26 11:00:03 +01:00
|
|
|
|
|
|
|
|
|
2016-08-05 10:44:43 +01:00
|
|
|
@statsd(namespace="dao")
|
2016-07-26 11:00:03 +01:00
|
|
|
def dao_fetch_weekly_historical_stats_for_service(service_id):
|
|
|
|
|
monday_of_notification_week = func.date_trunc('week', NotificationHistory.created_at).label('week_start')
|
|
|
|
|
return db.session.query(
|
|
|
|
|
NotificationHistory.notification_type,
|
|
|
|
|
NotificationHistory.status,
|
|
|
|
|
monday_of_notification_week,
|
|
|
|
|
func.count(NotificationHistory.id).label('count')
|
|
|
|
|
).filter(
|
|
|
|
|
NotificationHistory.service_id == service_id
|
|
|
|
|
).group_by(
|
|
|
|
|
NotificationHistory.notification_type,
|
|
|
|
|
NotificationHistory.status,
|
|
|
|
|
monday_of_notification_week
|
|
|
|
|
).order_by(
|
|
|
|
|
asc(monday_of_notification_week), NotificationHistory.status
|
|
|
|
|
).all()
|
2016-08-11 17:24:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@statsd(namespace='dao')
|
|
|
|
|
def dao_fetch_todays_stats_for_all_services():
|
|
|
|
|
return db.session.query(
|
|
|
|
|
Notification.notification_type,
|
|
|
|
|
Notification.status,
|
2016-08-19 16:36:20 +01:00
|
|
|
Notification.service_id,
|
2016-08-11 17:24:44 +01:00
|
|
|
func.count(Notification.id).label('count')
|
|
|
|
|
).select_from(
|
|
|
|
|
Service
|
|
|
|
|
).join(
|
|
|
|
|
Notification
|
|
|
|
|
).filter(
|
|
|
|
|
func.date(Notification.created_at) == date.today()
|
|
|
|
|
).group_by(
|
|
|
|
|
Notification.notification_type,
|
|
|
|
|
Notification.status,
|
|
|
|
|
Notification.service_id
|
2016-08-19 16:36:20 +01:00
|
|
|
).order_by(
|
|
|
|
|
Notification.service_id
|
2016-08-11 17:24:44 +01:00
|
|
|
)
|