mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
are not returned from queries - Updated stats_template_usage_by_month_dao.py to return the results for financial year not calendar, as the report os for FY only and hence only the FY data is required - Updated services_dao.py to ensure double precision values are converted to an int as the 'exact' function returns double precision from the database query, as the admin code requires the value for month to be an int
58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
from sqlalchemy import or_, and_
|
|
|
|
from app import db
|
|
from app.statsd_decorators import statsd
|
|
from app.dao.dao_utils import transactional
|
|
from app.models import StatsTemplateUsageByMonth, Template
|
|
|
|
|
|
@transactional
|
|
@statsd(namespace="dao")
|
|
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)
|
|
|
|
|
|
@statsd(namespace="dao")
|
|
def dao_get_template_usage_stats_by_service(service_id, year):
|
|
return db.session.query(
|
|
StatsTemplateUsageByMonth.template_id,
|
|
Template.name,
|
|
Template.template_type,
|
|
StatsTemplateUsageByMonth.month,
|
|
StatsTemplateUsageByMonth.year,
|
|
StatsTemplateUsageByMonth.count
|
|
).join(
|
|
Template, StatsTemplateUsageByMonth.template_id == Template.id
|
|
).filter(
|
|
Template.service_id == service_id
|
|
).filter(
|
|
or_(
|
|
and_(
|
|
StatsTemplateUsageByMonth.month.in_([4, 5, 6, 7, 8, 9, 10, 11, 12]),
|
|
StatsTemplateUsageByMonth.year == year
|
|
), and_(
|
|
StatsTemplateUsageByMonth.month.in_([1, 2, 3]),
|
|
StatsTemplateUsageByMonth.year == year + 1
|
|
)
|
|
)
|
|
).all()
|