raj-tomar001 commited on
Commit
96ddd86
·
verified ·
1 Parent(s): 2ec75c5

Update leader_board.py

Browse files
Files changed (1) hide show
  1. leader_board.py +212 -212
leader_board.py CHANGED
@@ -1,212 +1,212 @@
1
- from flask import Flask, jsonify, request
2
- import mysql.connector
3
- import logging
4
- from models import get_db_connection
5
- import ast
6
- import re
7
-
8
- def leaderboard_overall():
9
- connection = None
10
- cursor = None
11
-
12
- connection = get_db_connection()
13
- if connection:
14
- try:
15
- cursor = connection.cursor(dictionary=True)
16
-
17
-
18
- cursor.execute("""
19
- SELECT username, total_score
20
- FROM users
21
- ORDER BY total_score DESC
22
- LIMIT 5
23
- """)
24
-
25
- top_users = cursor.fetchall()
26
-
27
-
28
- logging.info("Top 5 users retrieved for overall leaderboard from users table.")
29
-
30
-
31
- return jsonify({
32
- 'status': 'success',
33
- 'leaderboard': top_users
34
- }), 200
35
-
36
- except mysql.connector.Error as err:
37
- logging.error("Error retrieving leaderboard: %s", err)
38
- return jsonify({
39
- 'status': 'error',
40
- 'message': 'An error occurred while fetching the leaderboard.'
41
- }), 500
42
-
43
- finally:
44
- if cursor:
45
- cursor.close()
46
- if connection:
47
- connection.close()
48
- logging.info("Database connection closed after retrieving overall leaderboard.")
49
- else:
50
- return jsonify({
51
- 'status': 'error',
52
- 'message': 'Database connection failed.'
53
- }), 500
54
-
55
- def leaderboard_weekly():
56
- connection = None
57
- cursor = None
58
- connection = get_db_connection()
59
- if connection:
60
- try:
61
- cursor = connection.cursor(dictionary=True)
62
-
63
-
64
- cursor.execute("""
65
- SELECT u.username, SUM(t.score) AS weekly_score
66
- FROM quiz_response t
67
- JOIN users u ON t.user_id_attempt = u.id
68
- WHERE DATE(t.submitted_on) >= CURDATE() - INTERVAL 6 DAY -- Last 7 days including today
69
- GROUP BY u.username
70
- ORDER BY weekly_score DESC
71
- LIMIT 5;
72
- """)
73
-
74
- top_users = cursor.fetchall()
75
-
76
-
77
- logging.info("Top 5 users retrieved for daily leaderboard.")
78
-
79
-
80
- return jsonify({
81
- 'status': 'success',
82
- 'leaderboard': top_users
83
- }), 200
84
-
85
- except mysql.connector.Error as err:
86
- logging.error("Error retrieving daily leaderboard: %s", err)
87
- return jsonify({
88
- 'status': 'error',
89
- 'message': 'An error occurred while fetching the daily leaderboard.'
90
- }), 500
91
-
92
- finally:
93
- if cursor:
94
- cursor.close()
95
- if connection:
96
- connection.close()
97
- logging.info("Database connection closed after retrieving daily leaderboard.")
98
- else:
99
- return jsonify({
100
- 'status': 'error',
101
- 'message': 'Database connection failed.'
102
- }), 500
103
-
104
-
105
- def leaderboard_daily():
106
- connection = None
107
- cursor = None
108
- connection = get_db_connection()
109
- if connection:
110
- try:
111
- cursor = connection.cursor(dictionary=True)
112
-
113
-
114
- cursor.execute("""
115
- SELECT u.username, SUM(t.score) AS daily_score
116
- FROM quiz_response t
117
- JOIN users u ON t.user_id_attempt = u.id
118
- WHERE DATE(t.submitted_on) = CURDATE() -- Only today's quizzes
119
- GROUP BY u.username
120
- ORDER BY daily_score DESC
121
- LIMIT 5
122
- """)
123
-
124
- top_users = cursor.fetchall()
125
-
126
-
127
- logging.info("Top 5 users retrieved for daily leaderboard.")
128
-
129
-
130
- return jsonify({
131
- 'status': 'success',
132
- 'leaderboard': top_users
133
- }), 200
134
-
135
- except mysql.connector.Error as err:
136
- logging.error("Error retrieving daily leaderboard: %s", err)
137
- return jsonify({
138
- 'status': 'error',
139
- 'message': 'An error occurred while fetching the daily leaderboard.'
140
- }), 500
141
-
142
- finally:
143
- if cursor:
144
- cursor.close()
145
- if connection:
146
- connection.close()
147
- logging.info("Database connection closed after retrieving daily leaderboard.")
148
- else:
149
- return jsonify({
150
- 'status': 'error',
151
- 'message': 'Database connection failed.'
152
- }), 500
153
-
154
-
155
-
156
-
157
-
158
-
159
- def leaderboard_theme():
160
- connection = None
161
- cursor = None
162
- connection = get_db_connection()
163
-
164
- if connection:
165
- try:
166
- cursor = connection.cursor(dictionary=True)
167
- theme = request.form.get('theme')
168
- theme = theme.lower()
169
-
170
-
171
- if not theme:
172
- return jsonify({
173
- 'status': 'error',
174
- 'message': 'Please provide a theme.'
175
- }), 400
176
-
177
- # Fetch top 5 users for the given theme
178
- cursor.execute("""
179
- SELECT u.username, SUM(qr.score) AS total_score
180
- FROM quiz_response qr
181
- JOIN users u ON qr.user_id_attempt = u.id
182
- WHERE qr.theme = %s
183
- GROUP BY u.username
184
- ORDER BY total_score DESC
185
- LIMIT 5
186
- """, (theme,))
187
-
188
- top_users = cursor.fetchall()
189
-
190
- return jsonify({
191
- 'status': 'success',
192
- 'leaderboard': top_users
193
- }), 200
194
-
195
- except mysql.connector.Error as err:
196
- logging.error(f"Error retrieving theme leaderboard for '{theme}': {err}")
197
- return jsonify({
198
- 'status': 'error',
199
- 'message': f'An error occurred while fetching the leaderboard for theme: {theme}.'
200
- }), 500
201
-
202
- finally:
203
- if cursor:
204
- cursor.close()
205
- if connection:
206
- connection.close()
207
- logging.info(f"Database connection closed after retrieving theme leaderboard for {theme}.")
208
- else:
209
- return jsonify({
210
- 'status': 'error',
211
- 'message': 'Database connection failed.'
212
- }), 500
 
1
+ from flask import Flask, jsonify, request
2
+ import mysql.connector
3
+ import logging
4
+ from models import get_db_connection
5
+ import ast
6
+ import re
7
+
8
+ def leaderboard_overall():
9
+ connection = None
10
+ cursor = None
11
+
12
+ connection = get_db_connection()
13
+ if connection:
14
+ try:
15
+ cursor = connection.cursor()
16
+
17
+
18
+ cursor.execute("""
19
+ SELECT username, total_score
20
+ FROM users
21
+ ORDER BY total_score DESC
22
+ LIMIT 5
23
+ """)
24
+
25
+ top_users = cursor.fetchall()
26
+
27
+
28
+ logging.info("Top 5 users retrieved for overall leaderboard from users table.")
29
+
30
+
31
+ return jsonify({
32
+ 'status': 'success',
33
+ 'leaderboard': top_users
34
+ }), 200
35
+
36
+ except mysql.connector.Error as err:
37
+ logging.error("Error retrieving leaderboard: %s", err)
38
+ return jsonify({
39
+ 'status': 'error',
40
+ 'message': 'An error occurred while fetching the leaderboard.'
41
+ }), 500
42
+
43
+ finally:
44
+ if cursor:
45
+ cursor.close()
46
+ if connection:
47
+ connection.close()
48
+ logging.info("Database connection closed after retrieving overall leaderboard.")
49
+ else:
50
+ return jsonify({
51
+ 'status': 'error',
52
+ 'message': 'Database connection failed.'
53
+ }), 500
54
+
55
+ def leaderboard_weekly():
56
+ connection = None
57
+ cursor = None
58
+ connection = get_db_connection()
59
+ if connection:
60
+ try:
61
+ cursor = connection.cursor()
62
+
63
+
64
+ cursor.execute("""
65
+ SELECT u.username, SUM(t.score) AS weekly_score
66
+ FROM quiz_response t
67
+ JOIN users u ON t.user_id_attempt = u.id
68
+ WHERE DATE(t.submitted_on) >= CURDATE() - INTERVAL 6 DAY -- Last 7 days including today
69
+ GROUP BY u.username
70
+ ORDER BY weekly_score DESC
71
+ LIMIT 5;
72
+ """)
73
+
74
+ top_users = cursor.fetchall()
75
+
76
+
77
+ logging.info("Top 5 users retrieved for daily leaderboard.")
78
+
79
+
80
+ return jsonify({
81
+ 'status': 'success',
82
+ 'leaderboard': top_users
83
+ }), 200
84
+
85
+ except mysql.connector.Error as err:
86
+ logging.error("Error retrieving daily leaderboard: %s", err)
87
+ return jsonify({
88
+ 'status': 'error',
89
+ 'message': 'An error occurred while fetching the daily leaderboard.'
90
+ }), 500
91
+
92
+ finally:
93
+ if cursor:
94
+ cursor.close()
95
+ if connection:
96
+ connection.close()
97
+ logging.info("Database connection closed after retrieving daily leaderboard.")
98
+ else:
99
+ return jsonify({
100
+ 'status': 'error',
101
+ 'message': 'Database connection failed.'
102
+ }), 500
103
+
104
+
105
+ def leaderboard_daily():
106
+ connection = None
107
+ cursor = None
108
+ connection = get_db_connection()
109
+ if connection:
110
+ try:
111
+ cursor = connection.cursor()
112
+
113
+
114
+ cursor.execute("""
115
+ SELECT u.username, SUM(t.score) AS daily_score
116
+ FROM quiz_response t
117
+ JOIN users u ON t.user_id_attempt = u.id
118
+ WHERE DATE(t.submitted_on) = CURDATE() -- Only today's quizzes
119
+ GROUP BY u.username
120
+ ORDER BY daily_score DESC
121
+ LIMIT 5
122
+ """)
123
+
124
+ top_users = cursor.fetchall()
125
+
126
+
127
+ logging.info("Top 5 users retrieved for daily leaderboard.")
128
+
129
+
130
+ return jsonify({
131
+ 'status': 'success',
132
+ 'leaderboard': top_users
133
+ }), 200
134
+
135
+ except mysql.connector.Error as err:
136
+ logging.error("Error retrieving daily leaderboard: %s", err)
137
+ return jsonify({
138
+ 'status': 'error',
139
+ 'message': 'An error occurred while fetching the daily leaderboard.'
140
+ }), 500
141
+
142
+ finally:
143
+ if cursor:
144
+ cursor.close()
145
+ if connection:
146
+ connection.close()
147
+ logging.info("Database connection closed after retrieving daily leaderboard.")
148
+ else:
149
+ return jsonify({
150
+ 'status': 'error',
151
+ 'message': 'Database connection failed.'
152
+ }), 500
153
+
154
+
155
+
156
+
157
+
158
+
159
+ def leaderboard_theme():
160
+ connection = None
161
+ cursor = None
162
+ connection = get_db_connection()
163
+
164
+ if connection:
165
+ try:
166
+ cursor = connection.cursor()
167
+ theme = request.form.get('theme')
168
+ theme = theme.lower()
169
+
170
+
171
+ if not theme:
172
+ return jsonify({
173
+ 'status': 'error',
174
+ 'message': 'Please provide a theme.'
175
+ }), 400
176
+
177
+ # Fetch top 5 users for the given theme
178
+ cursor.execute("""
179
+ SELECT u.username, SUM(qr.score) AS total_score
180
+ FROM quiz_response qr
181
+ JOIN users u ON qr.user_id_attempt = u.id
182
+ WHERE qr.theme = %s
183
+ GROUP BY u.username
184
+ ORDER BY total_score DESC
185
+ LIMIT 5
186
+ """, (theme,))
187
+
188
+ top_users = cursor.fetchall()
189
+
190
+ return jsonify({
191
+ 'status': 'success',
192
+ 'leaderboard': top_users
193
+ }), 200
194
+
195
+ except mysql.connector.Error as err:
196
+ logging.error(f"Error retrieving theme leaderboard for '{theme}': {err}")
197
+ return jsonify({
198
+ 'status': 'error',
199
+ 'message': f'An error occurred while fetching the leaderboard for theme: {theme}.'
200
+ }), 500
201
+
202
+ finally:
203
+ if cursor:
204
+ cursor.close()
205
+ if connection:
206
+ connection.close()
207
+ logging.info(f"Database connection closed after retrieving theme leaderboard for {theme}.")
208
+ else:
209
+ return jsonify({
210
+ 'status': 'error',
211
+ 'message': 'Database connection failed.'
212
+ }), 500