-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
217 lines (156 loc) · 6.89 KB
/
server.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from flask import (
Flask, session, redirect, render_template, request, flash, jsonify, send_file, abort
)
import time
from datetime import datetime
import pandas as pd
# Defined in models.py
from models import ModelMgr
# ========================================================
# Flask App
# ========================================================
#app = Flask(__name__, template_folder="./templates", static_folder="./static", static_url_path="")
app = Flask(__name__)
app.secret_key = b'example'
models = ModelMgr()
jpg_map = {
'coaster' : 'https://images.unsplash.com/photo-1555982105-d25af4182e4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&h=400&q=80',
'lens' : 'https://images.unsplash.com/photo-1508423134147-addf71308178?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&h=400&q=80',
'chair' : 'https://images.unsplash.com/photo-1449247709967-d4461a6a6103?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&h=400&q=80',
'typewriter' : 'https://images.unsplash.com/reserve/LJIZlzHgQ7WPSh5KVTCB_Typewriter.jpg?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&h=400&q=80',
'flask' : 'https://images.unsplash.com/photo-1467949576168-6ce8e2df4e13?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&h=400&q=80',
'coffee' : 'https://images.unsplash.com/photo-1544787219-7f47ccb76574?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&h=400&q=80',
'top' : 'https://images.unsplash.com/photo-1550837368-6594235de85c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&h=400&q=80',
'dice' : 'https://images.unsplash.com/photo-1551431009-a802eeec77b1?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=400&q=80',
'bedding' : 'https://images.unsplash.com/photo-1422190441165-ec2956dc9ecc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&h=400&q=80',
'clock' : 'https://images.unsplash.com/photo-1533090161767-e6ffed986c88?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjM0MTM2fQ&auto=format&fit=crop&w=400&h=400&q=80',
'book' : 'https://images.unsplash.com/photo-1519327232521-1ea2c736d34d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&h=400&q=80',
}
def init_user_data():
#print('INIT_USER_DATA')
now = datetime.now()
userdata = {
'Administrative' : 0,
'ProductRelated' : 0,
'Informational' : 0,
'Administrative_Duration' : 0,
'ProductRelated_Duration' : 0,
'Informational_Duration' : 0,
'Month' : now.strftime("%b"),
'Weekend' : now.weekday() > 4, # 0 is Monday, 6 is Sunday
}
return userdata
def increment_userdata_attr(session, attr, amount):
try:
#print(f'trying to inc {attr}')
if 'userdata' not in session:
session['userdata'] = init_user_data()
session['userdata'][attr] += amount
session.modified = True # Mark the session as modified
except Exception as e:
print(f"Error incrementing {attr}: {e}")
def start_timer(session, attr):
now = time.time()
if 'time_info' in session:
old_time_info = session['time_info']
elapsed = now - old_time_info['timestamp']
prev_type_dur = old_time_info['page_type'] + '_Duration'
increment_userdata_attr(session, prev_type_dur, elapsed)
#print(f'{attr} TIMER STARTED')
session['time_info'] = {
'timestamp' : now,
'page_type' : attr
}
def refresh_timer(session):
now = time.time()
if 'time_info' in session:
old_time_info = session['time_info']
elapsed = now - old_time_info['timestamp']
prev_type = old_time_info['page_type']
prev_type_dur = prev_type + '_Duration'
increment_userdata_attr(session, prev_type_dur, elapsed)
session['time_info'] = {
'timestamp' : now,
'page_type' : prev_type
}
#######################
## Dynamic Routes ##
#######################
@app.route("/usertable")
def usertable():
#print('USERTABLE hit')
if 'userdata' not in session:
#print('USERTABLE: reset')
session['userdata'] = init_user_data()
return render_template('usertable.html', data=session['userdata'])
@app.route('/product')
def product():
product_name = request.args.get('name', 'Default Product')
product_image_url = jpg_map[product_name]
# modify product tracking variables here
start_timer(session, 'ProductRelated')
increment_userdata_attr(session, 'ProductRelated', 1)
return render_template('product.html', product_name=product_name.title(),
product_image_url=product_image_url)
@app.route('/leftover-fields', methods=['POST'])
def leftovers():
# Extracting form data
leftover_fields = {
'Browser': int(request.form['Browser']),
'OperatingSystems': int(request.form['OperatingSystems']),
'VisitorType': str(request.form['VisitorType']),
'BounceRates': float(request.form['BounceRates']),
'ExitRates': float(request.form['ExitRates']),
'PageValues': float(request.form['PageValues']),
'SpecialDay': float(request.form['SpecialDay']),
}
if 'userdata' not in session:
session['userdata'] = init_user_data()
userdata = session['userdata']
combined_df = models.preproc_userdata(userdata, leftover_fields)
pred = models.gbdt.predict(combined_df)
is_revenue_good = (pred[0] == 1)
return render_template("predict.html", is_revenue_good=is_revenue_good)
@app.route('/reset', methods=['POST'])
def reset():
if request.method == 'POST':
session.clear()
return usertable()
else:
return 'Method not allowed', 405
@app.route('/refresh', methods=['POST'])
def refresh():
if request.method == 'POST':
refresh_timer(session)
return usertable()
else:
return 'Method not allowed', 405
###################################
## Routes that modify counters ##
###################################
@app.route('/sim-shop')
def sim_shop():
start_timer(session, 'ProductRelated')
increment_userdata_attr(session, 'ProductRelated', 1)
return render_template('sim_shop.html')
@app.route("/admin")
def admin():
start_timer(session, 'Administrative')
increment_userdata_attr(session, 'Administrative', 1)
return render_template("admin.html")
@app.route("/about")
def about():
start_timer(session, 'Informational')
increment_userdata_attr(session, 'Informational', 1)
return render_template("about.html")
######################
## Boring Routes ##
######################
@app.route("/")
def index():
return render_template("index.html")
@app.route("/form")
def form_route():
return render_template("form.html")
if __name__ == "__main__":
app.run(port=5001, debug=True)