forked from laurawilsonri/1300ABTesting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·90 lines (76 loc) · 2.88 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import sys
import random
import csv
import os
from flask import Flask, render_template
from flask import request
# DO NOT TOUCH ANYTHING IN THIS FILE.
#import datetime
# DO not write pyc files.
sys.dont_write_bytecode = True
# ab_test.csv which could be used for testing locally.
# After hosting in heroku, this file will no more be used.
# As Heroku file system is not persistent and it removes
# any file which is created after hosting.
csv_file_ab = './ab_test.csv'
with open(csv_file_ab, 'w') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
writer.writerow([
'version', 'pageLoadTime', 'clickTime', 'clickHTMLElementId','UniqueSessionID'])
print('version', 'pageLoadTime', 'clickTime', 'clickHTMLElementId','UniqueSession')
# Main Flask app server
app = Flask(__name__) # Simple python Flask server
app.config['DEBUG'] = True
app.static_folder = 'static'
# This is the root endpoint.
@app.route('/', methods=['GET'])
def root():
version = request.args.get('version', default='', type=str)
if version:
page = version + '.html'
return render_template(page)
else:
probability = random.uniform(0, 1)
if probability < 0.5:
return render_template('A.html')
else:
return render_template('B.html')
# data endpoint
@app.route('/data', methods=['GET', 'POST'])
def get_data():
json = request.get_json()
version = json['version']
click_time = json['clickTime']
page_load_time = json['pageLoadTime']
click_obj_id = json['HtmlElementID']
unique_session_id = json['UniqueSession']
# THIS PIECE OF CODE COULD BE USED TO CONVERT TO READ-ABLE TIME
# BUT HAVING IT IN UNIX TIME MAKES IT EASIER TO
# COMPARE WITH EYE_TRACKING.
# convert epoch timestamps to formatted version
# click_time = json['clickTime'] / 1000.0
# if click_time != 0:
# click_time_formatted = datetime.datetime.fromtimestamp(
# click_time).strftime('%Y-%m-%d %H:%M:%S.%f')
# else:
# click_time_formatted = click_time
# page_load_time = json['pageLoadTime'] / 1000.0
# page_load_time_formatted = datetime.datetime.fromtimestamp(
# page_load_time).strftime('%Y-%m-%d %H:%M:%S.%f')
print('AB_TESTING:', version , page_load_time , click_time , click_obj_id, unique_session_id)
# write to csv file
with open(csv_file_ab, 'a') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
writer.writerow([
version, page_load_time, click_time, click_obj_id, unique_session_id])
return "OK"
@app.route('/checkout', methods=['GET'])
def get_cart():
return render_template('checkout.html')
# DO NOT CHANGE ANYTHING HERE
# You might run into issues changing things here
# based on how heroku treats host and port
if __name__ == '__main__':
app.debug = True
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)