def funnel(): try: start_date = request.args.get('start_date') end_date = request.args.get('end_date') if start_date is None or end_date is None: return jsonify_error(400, "start_date and end_date are required") try: start_date = datetime.strptime(start_date, '%Y-%m-%d') end_date = datetime.strptime(end_date, '%Y-%m-%d') except ValueError: return jsonify_error(400, 'start_date and end_date are expected in YYYY-MM-DD format') if start_date > end_date: return jsonify_error(400, 'start_date should be before end_date') resp = {} results = query_db(FUNNEL_QUERY, [start_date, end_date], one=False) for result in results: #bucket = week_bucket(datetime.strptime(result['c'], "%Y-%m-%d").date()) bucket = DATE_TO_WEEK_CACHE.get(result['c']) if bucket is None: bucket = week_bucket(datetime.strptime(result['c'], "%Y-%m-%d").date()) DATE_TO_WEEK_CACHE[result['c']] = bucket counts_dict = resp.setdefault(bucket, {}) workflow_state = result['workflow_state'] if not workflow_state: continue if workflow_state and workflow_state not in counts_dict: counts_dict[workflow_state] = 0 counts_dict[workflow_state] += result['cnt'] #st = e return jsonify(resp) except Exception as e: print e return jsonify_error(500, 'Internal server error')
def create_app(): #Assuming start date as 2014-01-01 and end date as 2014-12-31 start_date = datetime.strptime('2014-01-01', '%Y-%m-%d').date() end_date = datetime.strptime('2014-12-31', '%Y-%m-%d').date() while start_date < end_date: DATE_TO_WEEK_CACHE[str(start_date)] = week_bucket(start_date) start_date += timedelta(days=1) return Flask(__name__)