From 8d1cae7914b6b50d849d9a3253dd0ce8f8182620 Mon Sep 17 00:00:00 2001 From: Galen Guyer Date: Fri, 13 Nov 2020 20:09:34 -0500 Subject: clean up diff route --- poller/__init__.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/poller/__init__.py b/poller/__init__.py index c6d0b0e..daa4fa8 100644 --- a/poller/__init__.py +++ b/poller/__init__.py @@ -263,27 +263,23 @@ def _api_v0_difference(): @APP.route('/api/v0/diff') def _api_v0_diff(): - query = request.args.get('q') - if query is None: - return make_repsonse(jsonify({'error': 'you must specify either a number or a range'}), 400) - first = -1 - last = -1 - if ',' not in query: + first = request.args.get('first') + last = request.args.get('last') + data = get_all_from_db() + if first is None: + first = 0 + else: try: - first = int(query) - except: - return make_repsonse(jsonify({'error': 'you must specify either a number or a range'}), 400) + first = int(first) + except: + first = 0 + if last is None: + last = len(data) - 1 else: try: - first = int(query.partition(',')[0]) - last = int(query.partition(',')[2]) + last = int(last) except: - return make_repsonse(jsonify({'error': 'you must specify either a number or a range'}), 400) - data = get_all_from_db() - if first < 0 or (last is not -1 and last < first): - return make_response(jsonify({'error': 'both numbers must be > 0 and last > first'}), 400) - if last >= len(data): - return make_response(jsonify({'error': f'last must be less than or equal to {len(data) - 1}'}), 400) + last = len(data) - 1 latest = data[last] prev = data[first] data = { @@ -298,6 +294,7 @@ def _api_v0_diff(): 'isolation_off_campus': latest["isolation_off_campus"] - prev["isolation_off_campus"], 'beds_available': latest["beds_available"] - prev["beds_available"], 'tests_administered': latest["tests_administered"] - prev["tests_administered"], + 'description': f'day {first} to {last}' } return jsonify(data) -- cgit v1.2.3