Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

v1.2.0 #46

Merged
merged 1 commit into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@

# Change Log

## [v1.2.0] - 2024-08-16

### Added

### Changed
- Reduce time complexity when get student list.

### Fixed


## [v1.1.0] - 2024-08-15

### Added
Expand Down
9 changes: 7 additions & 2 deletions api/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
# Flask
from flask_cors import CORS
from flask import app, Flask, Response, g, send_from_directory, send_file, jsonify
from flask_jwt_extended import JWTManager, jwt_required, get_jwt_identity
from flask_jwt_extended import JWTManager

# Google
from function.google import secret_key

from function.db import get_db
from function.db import get_db, get_dbdict
from function.loadconfig import config, UPLOAD_FOLDER, isDev


Expand Down Expand Up @@ -68,13 +68,18 @@ def index():
@app.before_request
def before_request():
g.db = get_db()
g.dbdict = get_dbdict()


@app.teardown_request
def teardown_request(exception=None):
db = g.pop('db', None)
if db is not None:
db.close()

dbdict = g.pop('dbdict', None)
if dbdict is not None:
dbdict.close()

@app.route('/api/Thumbnail/<filename>')
def get_image_thumbnail(filename):
Expand Down
13 changes: 12 additions & 1 deletion api/src/function/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,15 @@ def get_db():
password=config['DBPASS'],
database=config['DBNAME'],
)
return g.db
return g.db

def get_dbdict():
if 'dbdict' not in g:
g.dbdict = pymysql.connect(
host=config['DBHOST'],
user=config['DBUSER'],
password=config['DBPASS'],
database=config['DBNAME'],
cursorclass=pymysql.cursors.DictCursor,
)
return g.dbdict
90 changes: 28 additions & 62 deletions api/src/route/api/TA/Student/List_GET.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
def main():
Email = get_jwt_identity()['email']
CSYID = request.args.get('CSYID')
cur = g.db.cursor()
cur = g.dbdict.cursor()

if not isCET(g.db, cur, Email, CSYID):
if not isCET(g.db, g.db.cursor(), Email, CSYID):
jsonify({
'success': False,
'msg': "You don't have permission.",
Expand All @@ -21,14 +21,15 @@ def main():
# Query to get the list of students and their scores
StudentList_query = """
SELECT
STD.UID,
USR.Name,
SCT.Section,
COALESCE(SUM(CASE
WHEN JSON_CONTAINS(LB.CID, CAST(STD.CID AS CHAR), "$") OR JSON_CONTAINS(LB.GID, CAST(STD.GID AS CHAR), "$")
THEN SMT.Score
ELSE 0
END), 0) AS Score,
STD.UID AS `ID`,
USR.Name AS `Name (English)`,
SCT.Section AS `Section`,
TRUNCATE(COALESCE(SUM(CASE
WHEN JSON_CONTAINS(LB.CID, CAST(STD.CID AS CHAR), "$") OR JSON_CONTAINS(LB.GID, CAST(STD.GID AS CHAR), "$")
THEN SMT.Score
ELSE 0
END), 0), 2) AS Score,
SUB.MaxScore AS `MaxScore`,
COALESCE(GRP.Group, '-') AS `Group`
FROM
student STD
Expand All @@ -37,68 +38,33 @@ def main():
LEFT JOIN lab LB ON SMT.LID = LB.LID
INNER JOIN section SCT ON STD.CSYID = SCT.CSYID AND SCT.CID = STD.CID
LEFT JOIN `group` GRP ON GRP.GID = STD.GID
LEFT JOIN (
SELECT
STD.UID,
SUM(QST.MaxScore) AS MaxScore
FROM
question QST
LEFT JOIN lab LB on QST.LID = LB.LID
LEFT JOIN student STD on (JSON_CONTAINS(LB.CID, CAST(STD.CID AS CHAR), "$") OR JSON_CONTAINS(LB.GID, CAST(STD.GID AS CHAR), "$"))
WHERE
QST.CSYID = %s
GROUP BY
STD.UID
) SUB ON STD.UID = SUB.UID
WHERE
STD.CSYID = %s
GROUP BY
STD.UID, USR.Name, SCT.Section, GRP.Group
STD.UID, USR.Name, SCT.Section, GRP.Group, SCT.CID, GRP.GID, SUB.MaxScore
ORDER BY
Section ASC, UID ASC;
Section ASC, ID ASC;
"""
cur.execute(StudentList_query, (CSYID,))
cur.execute(StudentList_query, (CSYID, CSYID,))
ListResult = cur.fetchall()

# Query to get the maximum score of all questions
MaxScore_query = """
SELECT
QST.QID,
QST.MaxScore,
L.CID,
L.GID
FROM
question QST
JOIN lab L ON QST.LID = L.LID
WHERE
QST.CSYID = %s
"""
cur.execute(MaxScore_query, (CSYID,))
MaxScoreResult = cur.fetchall()

# Calculate total MaxScore for each student
student_max_scores = {}
for row in ListResult:
UID, _, _, _, _ = row
student_max_scores[UID] = 0

for QID, MaxScore, CID_json, GID_json in MaxScoreResult:
CID_list = json.loads(CID_json) if CID_json else []
GID_list = json.loads(GID_json) if GID_json else []

for row in ListResult:
UID, _, _, _, _ = row
cur.execute("SELECT CID, GID FROM student WHERE UID = %s AND CSYID = %s", (UID, CSYID))
student_data = cur.fetchone()
student_CID, student_GID = student_data

if student_CID in CID_list or (student_GID and student_GID in GID_list):
student_max_scores[UID] += int(MaxScore)

# Transform data to required format
transformed_data = []
for row in ListResult:
UID, Name, Section, Score, Group = row
transformed_data.append({
'ID': UID,
'Name (English)': Name,
'Section': Section,
'Group': Group,
'Score': Score,
'MaxScore': student_max_scores[UID]
})

return jsonify({
'success': True,
'msg': '',
'data': {
'Students': transformed_data
'Students': ListResult
}
}), 200
Loading