-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend_api.py
413 lines (371 loc) · 16.9 KB
/
backend_api.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
from flask import Flask, jsonify, request
import mysql.connector
import datetime
import requests
app = Flask(__name__)
# MySQL database connection configuration
db_config = {
'host': '127.0.0.1',
'database': 'dm_project',
'user': 'root',
'password': 'root'
}
# Endpoint to add new users to user table
@app.route('/add_user', methods=['POST'])
def add_user():
try:
connection = mysql.connector.connect(**db_config)
cursor = connection.cursor()
data = request.get_json()
userid = data.get('userid')
username = data.get('username')
useremail = data.get('useremail')
query = "INSERT INTO user (userid, useremail, username) VALUES (%s, %s, %s)"
cursor.execute(query, (userid, useremail, username))
connection.commit()
return jsonify(message='User added successfully')
except Exception as e:
return jsonify(error=str(e))
finally:
cursor.close()
connection.close()
# Endpoint to add new favourites to favourite table
@app.route('/add_favourite', methods=['POST'])
def add_favourite():
try:
connection = mysql.connector.connect(**db_config)
cursor = connection.cursor()
data = request.get_json()
userid = data.get('userid')
firm = data.get('firm')
query = "INSERT INTO favourite (userid, firm) VALUES (%s, %s)"
cursor.execute(query, (userid, firm))
connection.commit()
return jsonify(message='favourite added successfully')
except Exception as e:
return jsonify(error=str(e))
finally:
cursor.close()
connection.close()
# Endpoint to retrieve favourites data from favourites table
@app.route('/get_favourites', methods=['GET'])
def get_favourites():
userid = request.args.get('userid')
if userid is None:
return jsonify(error="Userid parameter is required.")
try:
connection = mysql.connector.connect(**db_config)
cursor = connection.cursor(dictionary=True)
query = "SELECT * FROM favourite WHERE userid = %s"
cursor.execute(query, (userid,))
favouritedata = cursor.fetchall()
return jsonify(favouritedata)
except Exception as e:
return jsonify(error=str(e))
finally:
cursor.close()
connection.close()
# Endpoint to retrieve user data from user table
@app.route('/get_user', methods=['GET'])
def get_user():
userid = request.args.get('userid')
if userid is None:
return jsonify(error="Userid parameter is required.")
try:
connection = mysql.connector.connect(**db_config)
cursor = connection.cursor(dictionary=True)
query = "SELECT * FROM user WHERE userid = %s"
cursor.execute(query, (userid,))
userdata = cursor.fetchall()
return jsonify(userdata)
except Exception as e:
return jsonify(error=str(e))
finally:
cursor.close()
connection.close()
# Endpoint to retrieve all available firm names from firm_sentiment_details table
@app.route('/firm_names', methods=['GET'])
def get_firm_names():
try:
connection = mysql.connector.connect(**db_config)
cursor = connection.cursor()
query = "SELECT DISTINCT firm FROM firm_sentiment_details"
cursor.execute(query)
firm_names = [row[0] for row in cursor.fetchall()]
return jsonify(firm_names)
except Exception as e:
return jsonify(error=str(e))
finally:
cursor.close()
connection.close()
# Endpoint to retrieve firm details based on firm name
@app.route('/firm_details', methods=['GET'])
def get_firm_details():
firm_name = request.args.get('firm_name')
if firm_name is None:
return jsonify(error="Firm name parameter is required.")
try:
connection = mysql.connector.connect(**db_config)
cursor = connection.cursor(dictionary=True)
query = "SELECT * FROM firm_details WHERE firm_name = %s"
cursor.execute(query, (firm_name,))
firms = cursor.fetchall()
return jsonify(firms)
except Exception as e:
return jsonify(error=str(e))
finally:
cursor.close()
connection.close()
# Endpoint to retrieve firm sentiment details based on firm name
@app.route('/firm_sentiment_details', methods=['GET'])
def get_firm_sentiment_details():
firm_name = request.args.get('firm_name')
if firm_name is None:
return jsonify(error="Firm name parameter is required.")
try:
connection = mysql.connector.connect(**db_config)
cursor = connection.cursor(dictionary=True)
query = "SELECT * FROM firm_sentiment_details WHERE firm = %s"
cursor.execute(query, (firm_name,))
firms_sentiment = cursor.fetchall()
return jsonify(firms_sentiment)
except Exception as e:
return jsonify(error=str(e))
finally:
cursor.close()
connection.close()
# Endpoint to retrieve year count based on firm name
@app.route('/year_count', methods=['GET'])
def get_year_count():
firm_name = request.args.get('firm_name')
if firm_name is None:
return jsonify(error="Firm name parameter is required.")
try:
connection = mysql.connector.connect(**db_config)
cursor = connection.cursor(dictionary=True)
query = "SELECT * FROM year_count WHERE firm = %s"
cursor.execute(query, (firm_name,))
year_counts = cursor.fetchall()
return jsonify(year_counts)
except Exception as e:
return jsonify(error=str(e))
finally:
cursor.close()
connection.close()
# Endpoint to retrieve title count based on firm name
@app.route('/title_count', methods=['GET'])
def get_title_count():
firm_name = request.args.get('firm_name')
if firm_name is None:
return jsonify(error="Firm name parameter is required.")
try:
connection = mysql.connector.connect(**db_config)
cursor = connection.cursor(dictionary=True)
query = "SELECT * FROM title_count WHERE firm = %s"
cursor.execute(query, (firm_name,))
title_counts = cursor.fetchall()
return jsonify(title_counts)
except Exception as e:
return jsonify(error=str(e))
finally:
cursor.close()
connection.close()
# Endpoint to retrieve average values for firm details and sentiment, sorted by selected average values in descending order
@app.route('/sorted_firm_averages', methods=['POST'])
def get_sorted_firm_averages():
try:
connection = mysql.connector.connect(**db_config)
cursor = connection.cursor(dictionary=True)
data = request.get_json()
include_work_life_balance = data.get('work_life_balance')
include_culture_values = data.get('culture_values')
include_diversity_inclusion = data.get('diversity_inclusion')
include_career_opp = data.get('career_opp')
include_comp_benefits = data.get('comp_benefits')
include_senior_mgmt = data.get('senior_mgmt')
include_recommend = data.get('recommend')
include_ceo_approv = data.get('ceo_approv')
include_predicted_sentiments = data.get('review_rating')
select_fields = []
if include_work_life_balance:
select_fields.append('work_life_balance')
if include_culture_values:
select_fields.append('culture_values')
if include_diversity_inclusion:
select_fields.append('diversity_inclusion')
if include_career_opp:
select_fields.append('career_opp')
if include_comp_benefits:
select_fields.append('comp_benefits')
if include_senior_mgmt:
select_fields.append('senior_mgmt')
if include_recommend:
select_fields.append('recommend')
if include_ceo_approv:
select_fields.append('ceo_approv')
if include_predicted_sentiments:
query_1 = "SELECT firm, Predicted_sentiments FROM firm_sentiment_details"
cursor.execute(query_1)
firm_sentiment_details = cursor.fetchall()
if len(select_fields) != 0:
if len(select_fields) == 1:
select_statement = select_fields[0]
query_2 = f"SELECT firm_name, {select_statement} FROM firm_details"
else:
select_statement = ", ".join(select_fields)
query_2 = f"SELECT firm_name, {select_statement} FROM firm_details"
cursor.execute(query_2)
firm_details = cursor.fetchall()
firm_data = {}
if len(select_fields) == 0:
if include_predicted_sentiments:
for firm_sentiment in firm_sentiment_details:
firm_name = firm_sentiment['firm']
firm_data[firm_name] = {}
firm_data[firm_name]['predicted_sentiments'] = firm_sentiment['Predicted_sentiments']/2
else:
for firm in firm_details:
firm_name = firm['firm_name']
firm_data[firm_name] = {}
for field in select_fields:
firm_data[firm_name][field] = firm[field]
if include_predicted_sentiments:
for firm_sentiment in firm_sentiment_details:
if firm_sentiment['firm'] == firm_name:
firm_data[firm_name]['predicted_sentiments'] = firm_sentiment['Predicted_sentiments']/2
firm_averages = {}
for firm in firm_data:
firm_averages[firm] = {}
average = 0
for field in firm_data[firm]:
average = average + firm_data[firm][field]
average = average / len(firm_data[firm])
firm_averages[firm] = average
sorted_firm_averages = sorted(firm_averages.items(), key=lambda x: x[1], reverse=True)
return jsonify(sorted_firm_averages)
except Exception as e:
return jsonify(error=str(e))
finally:
cursor.close()
connection.close()
@app.route('/store_firm_review', methods=['POST'])
def store_firm_sentiment():
try:
connection = mysql.connector.connect(**db_config)
cursor = connection.cursor()
data = request.get_json()
firm_name = data.get('firm_name')
job_title = data.get('job_title')
work_life_balance = data.get('work_life_balance')
culture_values = data.get('culture_values')
diversity_inclusion = data.get('diversity_inclusion')
career_opp = data.get('career_opp')
comp_benefits = data.get('comp_benefits')
senior_mgmt = data.get('senior_mgmt')
recommend = data.get('recommend')
ceo_approv = data.get('ceo_approv')
outlook = data.get('outlook')
headline = data.get('headline')
pros = data.get('pros')
cons = data.get('cons')
year = datetime.datetime.now().year
review = headline + " " + pros + " " + cons
sentiment_api_url = 'http://localhost:9090/predict_sentiment'
sentiment_data = {'review': review}
sentiment_response = requests.post(sentiment_api_url, json=sentiment_data)
sentiment_list = sentiment_response.json().get('sentiment')
predicted_sentiment = sentiment_list[0][0]*10
query = "INSERT INTO reviews (firm, job_title, work_life_balance, culture_values, diversity_inclusion, career_opp, comp_benefits, senior_mgmt, recommend, ceo_approv, outlook, headline, pros, cons, year) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
cursor.execute(query, (firm_name, job_title, work_life_balance, culture_values, diversity_inclusion, career_opp, comp_benefits, senior_mgmt, recommend, ceo_approv, outlook, headline, pros, cons, year))
connection.commit()
query_1 = "SELECT * FROM firm_count WHERE firm = %s"
cursor.execute(query_1, (firm_name,))
result = cursor.fetchall()
if result:
for row in result:
count = row[2]
count = count + 1
query_1 = "UPDATE firm_count SET fcount = %s WHERE firm = %s"
cursor.execute(query_1, (count, firm_name))
connection.commit()
query_5 = "SELECT * FROM firm_sentiment_details WHERE firm = %s"
cursor.execute(query_5, (firm_name,))
result_1 = cursor.fetchall()
if result_1:
for row in result_1:
sentiment = row[2]
sentiment = ((sentiment*(count-1)) + predicted_sentiment)/count
query_6 = "UPDATE firm_sentiment_details SET Predicted_sentiments = %s WHERE firm = %s"
cursor.execute(query_6, (sentiment, firm_name))
connection.commit()
query_7 = "SELECT * FROM firm_details WHERE firm_name = %s"
cursor.execute(query_7, (firm_name,))
result_2 = cursor.fetchall()
if result_2:
for row in result_2:
work_life_balance_o = row[2]
culture_values_o = row[3]
diversity_inclusion_o = row[4]
career_opp_o = row[5]
comp_benefits_o = row[6]
senior_mgmt_o = row[7]
recommend_o = row[8]
ceo_approv_o = row[9]
work_life_balance_n = ((work_life_balance_o*(count-1)) + work_life_balance)/count
culture_values_n = ((culture_values_o*(count-1)) + culture_values)/count
diversity_inclusion_n = ((diversity_inclusion_o*(count-1)) + diversity_inclusion)/count
career_opp_n = ((career_opp_o*(count-1)) + career_opp)/count
comp_benefits_n = ((comp_benefits_o*(count-1)) + comp_benefits)/count
senior_mgmt_n = ((senior_mgmt_o*(count-1)) + senior_mgmt)/count
recommend_n = ((recommend_o*(count-1)) + recommend)/count
ceo_approv_n = ((ceo_approv_o*(count-1)) + ceo_approv)/count
query_8 = "UPDATE firm_details SET work_life_balance = %s, culture_values = %s, diversity_inclusion = %s, career_opp = %s, comp_benefits = %s, senior_mgmt = %s, recommend = %s, ceo_approv = %s WHERE firm_name = %s"
cursor.execute(query_8, (work_life_balance_n, culture_values_n, diversity_inclusion_n, career_opp_n, comp_benefits_n, senior_mgmt_n, recommend_n, ceo_approv_n, firm_name))
connection.commit()
else:
query_4 = "INSERT INTO firm_count (firm, fcount) VALUES (%s, %s)"
cursor.execute(query_4, (firm_name, 1))
connection.commit()
query_2 = "INSERT INTO firm_sentiment_details (firm, Predicted_sentiments) VALUES (%s, %s)"
cursor.execute(query_2, (firm_name, predicted_sentiment))
connection.commit()
query_3 = "INSERT INTO firm_details (firm_name, work_life_balance, culture_values, diversity_inclusion, career_opp, comp_benefits, senior_mgmt, recommend, ceo_approv) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
cursor.execute(query_3,(firm_name, work_life_balance, culture_values, diversity_inclusion, career_opp, comp_benefits, senior_mgmt, recommend, ceo_approv))
connection.commit()
query_9 = "SELECT * FROM title_count WHERE firm = %s AND job_title = %s"
cursor.execute(query_9, (firm_name, job_title))
result_3 = cursor.fetchall()
if result_3:
for row in result_3:
count = row[3]
count = count + 1
query_10 = "UPDATE title_count SET title_count = %s WHERE firm = %s AND job_title = %s"
cursor.execute(query_10, (count, firm_name, job_title))
connection.commit()
else:
query_11 = "INSERT INTO title_count (firm, job_title, title_count) VALUES (%s, %s, %s)"
cursor.execute(query_11, (firm_name, job_title, 1))
connection.commit()
query_12 = "SELECT * FROM year_count WHERE firm = %s AND year = %s"
cursor.execute(query_12, (firm_name, year))
result_4 = cursor.fetchall()
if result_4:
for row in result_4:
count = row[3]
count = count + 1
query_13 = "UPDATE year_count SET year_count = %s WHERE firm = %s AND year = %s"
cursor.execute(query_13, (count, firm_name, year))
connection.commit()
else:
query_14 = "INSERT INTO year_count (firm, year, year_count) VALUES (%s, %s, %s)"
cursor.execute(query_14, (firm_name, year, 1))
connection.commit()
return jsonify(message="Data successfully stored in the database.")
except Exception as e:
print(e)
return jsonify(error=str(e))
finally:
cursor.close()
connection.close()
if __name__ == '__main__':
app.run(port=8000,host='0.0.0.0')