Add delivery status endpoint

This commit is contained in:
Adam Shimali
2016-04-29 12:59:36 +01:00
parent 519b912edc
commit b51ee0b4d0
4 changed files with 76 additions and 3 deletions

View File

@@ -85,7 +85,8 @@ def init_app(app):
url_for('status.show_status'),
url_for('notifications.process_ses_response'),
url_for('notifications.process_firetext_response'),
url_for('notifications.process_mmg_response')
url_for('notifications.process_mmg_response'),
url_for('status.show_delivery_status'),
]
if request.path not in no_auth_req:
from app.authentication import auth

View File

@@ -1,8 +1,17 @@
from flask import jsonify
from datetime import (
datetime,
timedelta
)
from flask import Blueprint, request
from flask import (
jsonify,
Blueprint,
request,
current_app
)
from app import db, version
from app.models import Notification
status = Blueprint('status', __name__)
@@ -20,6 +29,34 @@ def show_status():
db_version=get_db_version()), 200
@status.route('/_delivery_status')
def show_delivery_status():
if request.args.get('elb', None):
return jsonify(status="ok"), 200
else:
notifications_alert = current_app.config['NOTIFICATIONS_ALERT']
some_number_of_minutes_ago = datetime.now() - timedelta(minutes=notifications_alert)
notifications = Notification.query.filter(Notification.status == 'sending',
Notification.created_at < some_number_of_minutes_ago).all()
message = "{} notifications in sending state over {} minutes".format(len(notifications), notifications_alert)
if notifications:
return jsonify(
status="error",
message=message,
travis_commit=version.__travis_commit__,
travis_build_number=version.__travis_job_number__,
build_time=version.__time__,
db_version=get_db_version()), 500
return jsonify(
status="ok",
message=message,
travis_commit=version.__travis_commit__,
travis_build_number=version.__travis_job_number__,
build_time=version.__time__,
db_version=get_db_version()), 200
def get_db_version():
try:
query = 'SELECT version_num FROM alembic_version'

View File

@@ -82,6 +82,7 @@ class Config(object):
FIRETEXT_NUMBER = os.getenv('FIRETEXT_NUMBER')
FIRETEXT_API_KEY = os.getenv("FIRETEXT_API_KEY")
CSV_UPLOAD_BUCKET_NAME = 'local-notifications-csv-upload'
NOTIFICATIONS_ALERT = 5 # five mins
class Development(Config):

View File

@@ -0,0 +1,34 @@
import json
from datetime import (
datetime,
timedelta
)
def test_get_delivery_status_all_ok(notify_api, notify_db):
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = '/_delivery_status'
response = client.get(path)
assert response.status_code == 200
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['status'] == 'ok'
assert resp_json['message'] == '0 notifications in sending state over 5 minutes'
def test_get_delivery_status_with_undelivered_notification(notify_api, notify_db, sample_notification):
more_than_five_mins_ago = datetime.now() - timedelta(minutes=10)
sample_notification.created_at = more_than_five_mins_ago
notify_db.session.add(sample_notification)
notify_db.session.commit()
with notify_api.test_request_context():
with notify_api.test_client() as client:
path = '/_delivery_status'
response = client.get(path)
assert response.status_code == 500
resp_json = json.loads(response.get_data(as_text=True))
assert resp_json['status'] == 'error'
assert resp_json['message'] == '1 notifications in sending state over 5 minutes'