mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
Merge branch 'master' into add_proxy_header_check
This commit is contained in:
@@ -14,7 +14,8 @@ from notifications_utils.template import (
|
||||
)
|
||||
from requests import (
|
||||
HTTPError,
|
||||
request
|
||||
request,
|
||||
RequestException
|
||||
)
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from app import (
|
||||
@@ -492,25 +493,24 @@ def send_inbound_sms_to_service(self, inbound_sms_id, service_id):
|
||||
"date_received": inbound_sms.provider_date.strftime(DATETIME_FORMAT)
|
||||
}
|
||||
|
||||
response = request(
|
||||
method="POST",
|
||||
url=inbound_api.url,
|
||||
data=json.dumps(data),
|
||||
headers={
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer {}'.format(inbound_api.bearer_token)
|
||||
},
|
||||
timeout=60
|
||||
)
|
||||
current_app.logger.info('send_inbound_sms_to_service sending {} to {}, response {}'.format(
|
||||
inbound_sms_id,
|
||||
inbound_api.url,
|
||||
response.status_code
|
||||
))
|
||||
|
||||
try:
|
||||
response = request(
|
||||
method="POST",
|
||||
url=inbound_api.url,
|
||||
data=json.dumps(data),
|
||||
headers={
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer {}'.format(inbound_api.bearer_token)
|
||||
},
|
||||
timeout=60
|
||||
)
|
||||
current_app.logger.info('send_inbound_sms_to_service sending {} to {}, response {}'.format(
|
||||
inbound_sms_id,
|
||||
inbound_api.url,
|
||||
response.status_code
|
||||
))
|
||||
response.raise_for_status()
|
||||
except HTTPError as e:
|
||||
except RequestException as e:
|
||||
current_app.logger.warning(
|
||||
"send_inbound_sms_to_service request failed for service_id: {} and url: {}. exc: {}".format(
|
||||
service_id,
|
||||
@@ -518,7 +518,7 @@ def send_inbound_sms_to_service(self, inbound_sms_id, service_id):
|
||||
e
|
||||
)
|
||||
)
|
||||
if e.response.status_code >= 500:
|
||||
if not isinstance(e, HTTPError) or e.response.status_code >= 500:
|
||||
try:
|
||||
self.retry(queue=QueueNames.RETRY,
|
||||
exc='Unable to send_inbound_sms_to_service for service_id: {} and url: {}. \n{}'.format(
|
||||
|
||||
@@ -245,7 +245,7 @@ class Config(object):
|
||||
'options': {'queue': QueueNames.PERIODIC}
|
||||
},
|
||||
'daily-stats-template-usage-by-month': {
|
||||
'task': 'daily-stats-template_usage_by_month',
|
||||
'task': 'daily-stats-template-usage-by-month',
|
||||
'schedule': crontab(hour=0, minute=50),
|
||||
'options': {'queue': QueueNames.PERIODIC}
|
||||
}
|
||||
|
||||
@@ -554,6 +554,7 @@ def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year)
|
||||
for result in results:
|
||||
stat = type("", (), {})()
|
||||
stat.template_id = result.template_id
|
||||
stat.template_type = result.template_type
|
||||
stat.name = str(result.name)
|
||||
stat.month = result.month
|
||||
stat.year = result.year
|
||||
@@ -567,16 +568,18 @@ def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year)
|
||||
today_results = db.session.query(
|
||||
Notification.template_id,
|
||||
Template.name,
|
||||
Template.template_type,
|
||||
extract('month', month).label('month'),
|
||||
extract('year', year).label('year'),
|
||||
func.count().label('count')
|
||||
).join(
|
||||
Template, Notification.template_id == Template.id
|
||||
Template, Notification.template_id == Template.id,
|
||||
).filter(
|
||||
Notification.created_at >= start_date
|
||||
).group_by(
|
||||
Notification.template_id,
|
||||
Template.name,
|
||||
Template.template_type,
|
||||
month,
|
||||
year
|
||||
).order_by(
|
||||
@@ -586,13 +589,15 @@ def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year)
|
||||
for today_result in today_results:
|
||||
add_to_stats = True
|
||||
for stat in stats:
|
||||
if today_result.template_id == stat.template_id:
|
||||
if today_result.template_id == stat.template_id and today_result.month == stat.month \
|
||||
and today_result.year == stat.year:
|
||||
stat.count = stat.count + today_result.count
|
||||
add_to_stats = False
|
||||
|
||||
if add_to_stats:
|
||||
new_stat = type("", (), {})()
|
||||
new_stat.template_id = today_result.template_id
|
||||
new_stat.template_type = today_result.template_type
|
||||
new_stat.name = today_result.name
|
||||
new_stat.month = today_result.month
|
||||
new_stat.year = today_result.year
|
||||
|
||||
@@ -34,6 +34,7 @@ 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
|
||||
|
||||
@@ -539,6 +539,7 @@ def get_monthly_template_usage(service_id):
|
||||
{
|
||||
'template_id': str(i.template_id),
|
||||
'name': i.name,
|
||||
'type': i.template_type,
|
||||
'month': i.month,
|
||||
'year': i.year,
|
||||
'count': i.count
|
||||
|
||||
Reference in New Issue
Block a user