mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 01:41:05 -05:00
Merge branch 'master' into remove-initial-update-sms-sender
This commit is contained in:
@@ -2,7 +2,8 @@ from datetime import (
|
||||
timedelta,
|
||||
datetime
|
||||
)
|
||||
|
||||
from flask import current_app
|
||||
from sqlalchemy import desc
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import transactional
|
||||
@@ -31,6 +32,28 @@ def dao_get_inbound_sms_for_service(service_id, limit=None, user_number=None):
|
||||
return q.all()
|
||||
|
||||
|
||||
def dao_get_paginated_inbound_sms_for_service(
|
||||
service_id,
|
||||
older_than=None,
|
||||
page_size=None
|
||||
):
|
||||
if page_size is None:
|
||||
page_size = current_app.config['PAGE_SIZE']
|
||||
|
||||
filters = [InboundSms.service_id == service_id]
|
||||
|
||||
if older_than:
|
||||
older_than_created_at = db.session.query(
|
||||
InboundSms.created_at).filter(InboundSms.id == older_than).as_scalar()
|
||||
filters.append(InboundSms.created_at < older_than_created_at)
|
||||
|
||||
query = InboundSms.query.filter(*filters)
|
||||
|
||||
return query.order_by(desc(InboundSms.created_at)).paginate(
|
||||
per_page=page_size
|
||||
).items
|
||||
|
||||
|
||||
def dao_count_inbound_sms_for_service(service_id):
|
||||
return InboundSms.query.filter(
|
||||
InboundSms.service_id == service_id
|
||||
|
||||
@@ -26,6 +26,7 @@ def get_service_ids_that_need_billing_populated(start_date, end_date):
|
||||
).distinct().all()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def create_or_update_monthly_billing(service_id, billing_month):
|
||||
start_date, end_date = get_month_start_and_end_date_in_utc(billing_month)
|
||||
_update_monthly_billing(service_id, start_date, end_date, SMS_TYPE)
|
||||
@@ -47,6 +48,7 @@ def _monthly_billing_data_to_json(billing_data):
|
||||
return results
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
@transactional
|
||||
def _update_monthly_billing(service_id, start_date, end_date, notification_type):
|
||||
billing_data = get_billing_data_for_month(
|
||||
|
||||
@@ -105,6 +105,7 @@ def is_between(date, start_date, end_date):
|
||||
return start_date <= date <= end_date
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def billing_data_per_month_query(rate, service_id, start_date, end_date, notification_type):
|
||||
month = get_london_month_from_utc_column(NotificationHistory.created_at)
|
||||
if notification_type == SMS_TYPE:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import uuid
|
||||
from datetime import date, datetime, timedelta
|
||||
from datetime import date, datetime, timedelta, time
|
||||
|
||||
from sqlalchemy import asc, func
|
||||
from sqlalchemy import asc, func, extract
|
||||
from sqlalchemy.orm import joinedload
|
||||
from flask import current_app
|
||||
|
||||
@@ -519,3 +519,25 @@ def dao_fetch_active_users_for_service(service_id):
|
||||
)
|
||||
|
||||
return query.all()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_fetch_monthly_historical_stats_by_template():
|
||||
month = get_london_month_from_utc_column(NotificationHistory.created_at)
|
||||
year = func.date_trunc("year", NotificationHistory.created_at)
|
||||
end_date = datetime.combine(date.today(), time.min)
|
||||
|
||||
return db.session.query(
|
||||
NotificationHistory.template_id,
|
||||
extract('month', month).label('month'),
|
||||
extract('year', year).label('year'),
|
||||
func.count().label('count')
|
||||
).filter(
|
||||
NotificationHistory.created_at < end_date
|
||||
).group_by(
|
||||
NotificationHistory.template_id,
|
||||
month,
|
||||
year
|
||||
).order_by(
|
||||
NotificationHistory.template_id
|
||||
).all()
|
||||
|
||||
24
app/dao/stats_template_usage_by_month_dao.py
Normal file
24
app/dao/stats_template_usage_by_month_dao.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from app import db
|
||||
from app.models import StatsTemplateUsageByMonth
|
||||
|
||||
|
||||
def insert_or_update_stats_for_template(template_id, month, year, count):
|
||||
result = db.session.query(
|
||||
StatsTemplateUsageByMonth
|
||||
).filter(
|
||||
StatsTemplateUsageByMonth.template_id == template_id,
|
||||
StatsTemplateUsageByMonth.month == month,
|
||||
StatsTemplateUsageByMonth.year == year
|
||||
).update(
|
||||
{
|
||||
'count': count
|
||||
}
|
||||
)
|
||||
if result == 0:
|
||||
monthly_stats = StatsTemplateUsageByMonth(
|
||||
template_id=template_id,
|
||||
month=month,
|
||||
year=year,
|
||||
count=count
|
||||
)
|
||||
db.session.add(monthly_stats)
|
||||
Reference in New Issue
Block a user