"""Analytics handlers. The analytics handlers provide endpoints to facilitate metric grouping and fetching. Some of those endpoints are currently used as replacements of VAPI, which requires an extra layer of information (vapi envelope). """ from flask import request from oto import response as oto_response from oto.adaptors import flask as oto_flask from owsrequest import flask_request from analytics import api from analytics import config from analytics.api import app from analytics.logic import track_summary from analytics.logic import product_summary from analytics.utils.handlers import parse_date_values from analytics.utils.newrelic import track_custom_parameters from analytics.validation import request_args_validation @api.route( config.HEALTH_CHECK, methods=['GET']) def hello_world(): """Hello World health check.""" return oto_flask.flaskify(oto_response.Response(message='Hello World!')) @app.route('/track-summary/') def get_track_summary(): """Get geographic insights data by region. Request params: from_date (str): start of date range to_date (str): end of date range search_filter (str): 'by_store', 'by_country', 'by_source' isrc (str): an ISRC. territory_code(str): a territory code Returns: flask.Response with a dict of geographic insights data as message. """ grass_validation = flask_request.verify_grass_headers(request) if not grass_validation: return oto_flask.flaskify(grass_validation) account_type, account_id = flask_request.get_grass_headers(request) validation = request_args_validation.validate_request_args(request.args) if not validation: return oto_flask.flaskify(validation) start_date, end_date = parse_date_values(request.args) isrc = request.args.get('isrc') search_filter = request.args.get('search_filter') group_daily = request.args.get('group_daily') == 'true' transaction = request.args.get('transaction') include_growth = request.args.get('include_growth') == 'true' newrelic_parameters = { 'account_id': account_id, 'start_date': start_date, 'end_date': end_date, 'isrc': isrc, 'search_filter': search_filter, 'group_daily': group_daily, 'transaction': transaction, 'include_growth': include_growth } track_custom_parameters(**newrelic_parameters) test = oto_flask.flaskify( track_summary.get_track_summary( start_date, end_date, isrc, search_filter, account_type, account_id, group_daily, transaction, include_growth)) return test @app.route('/product-summary/') def get_product_summary(): """Get geographic insights data by region. Request params: from_date (str): start of date range to_date (str): end of date range search_filter (str): 'by_store', 'by_country', 'by_source' upc (str): a UPC. territory_code(str): a territory code Returns: flask.Response with a dict of geographic insights data as message. """ grass_validation = flask_request.verify_grass_headers(request) if not grass_validation: return oto_flask.flaskify(grass_validation) account_type, account_id = flask_request.get_grass_headers(request) validation = request_args_validation.validate_request_args(request.args) if not validation: return oto_flask.flaskify(validation) start_date, end_date = parse_date_values(request.args) upc = request.args.get('upc') search_filter = request.args.get('search_filter') group_daily = request.args.get('group_daily') == 'true' transaction = request.args.get('transaction') include_growth = request.args.get('include_growth') == 'true' newrelic_parameters = { 'account_id': account_id, 'start_date': start_date, 'end_date': end_date, 'upc': upc, 'search_filter': search_filter, 'group_daily': group_daily, 'transaction': transaction, 'include_growth': include_growth } track_custom_parameters(**newrelic_parameters) test = oto_flask.flaskify( product_summary.get_product_summary( start_date, end_date, upc, search_filter, account_type, account_id, group_daily, transaction, include_growth)) return test