
--- action.py
+++ action.py
... | ... | @@ -31,9 +31,8 @@ |
31 | 31 |
|
32 | 32 |
|
33 | 33 |
@Action.route('/gps_update') |
34 |
-class fileUpload(Resource): |
|
34 |
+class GPS_update(Resource): |
|
35 | 35 |
def post(self): |
36 |
- db = DB() |
|
37 | 36 |
token = request.headers.get('Authorization') |
38 | 37 |
if not token: |
39 | 38 |
return jsonify({'result': 'fail', 'msg': '토큰이 없습니다.'}) |
... | ... | @@ -42,7 +41,8 @@ |
42 | 41 |
decoded_token = jwt.decode(token, "secret", algorithms=['HS256']) |
43 | 42 |
#print(decoded_token) |
44 | 43 |
user_id = decoded_token['id'] |
45 |
- |
|
44 |
+ |
|
45 |
+ db = DB() |
|
46 | 46 |
|
47 | 47 |
data = request.get_json() |
48 | 48 |
if len(data["trip_id"]) !=64: |
... | ... | @@ -76,18 +76,32 @@ |
76 | 76 |
|
77 | 77 |
|
78 | 78 |
@Action.route('/trip_and_score_update') |
79 |
-class fileUpload(Resource): |
|
79 |
+class TRIP_insert(Resource): |
|
80 | 80 |
def post(self): |
81 |
- db = DB() |
|
82 | 81 |
token = request.headers.get('Authorization') |
82 |
+ |
|
83 |
+ # Check if token is provided |
|
83 | 84 |
if not token: |
84 |
- return jsonify({'result': 'fail', 'msg': '토큰이 없습니다.'}) |
|
85 |
- else: |
|
85 |
+ return jsonify({'result': 'fail', 'msg': '토큰이 없습니다.'}), 401 |
|
86 |
+ |
|
87 |
+ try: |
|
86 | 88 |
# Decode the token to verify it |
87 | 89 |
decoded_token = jwt.decode(token, "secret", algorithms=['HS256']) |
88 |
- # print(decoded_token) |
|
89 | 90 |
user_id = decoded_token['id'] |
91 |
+ except jwt.ExpiredSignatureError: |
|
92 |
+ return jsonify({'result': 'fail', 'msg': '토큰이 만료되었습니다.'}), 401 |
|
93 |
+ except jwt.InvalidTokenError: |
|
94 |
+ return jsonify({'result': 'fail', 'msg': '유효하지 않은 토큰입니다.'}), 401 |
|
90 | 95 |
|
96 |
+ # Interact with the DB to get user history |
|
97 |
+ try: |
|
98 |
+ db = DB() |
|
99 |
+ result, status_code = db.get_history(user_name=user_id) |
|
100 |
+ return jsonify({'result': 'success', 'data': result}), status_code |
|
101 |
+ except Exception as e: |
|
102 |
+ return jsonify({'result': 'fail', 'msg': str(e)}), 500 |
|
103 |
+ |
|
104 |
+ db = DB() |
|
91 | 105 |
data = request.get_json() |
92 | 106 |
if len(data["trip_id"]) != 64: |
93 | 107 |
return jsonify({500: "ERROR! INVALID TRIP_ID!"}) |
... | ... | @@ -120,4 +134,30 @@ |
120 | 134 |
helmet_on, |
121 | 135 |
final_score |
122 | 136 |
) |
123 |
- return jsonify({'result': f'success'})(파일 끝에 줄바꿈 문자 없음) |
|
137 |
+ return jsonify({'result': f'success'}) |
|
138 |
+ |
|
139 |
+@Action.route('/get_history') |
|
140 |
+class Get_history(Resource): |
|
141 |
+ def post(self): |
|
142 |
+ token = request.headers.get('Authorization') |
|
143 |
+ |
|
144 |
+ # Check if token is provided |
|
145 |
+ if not token: |
|
146 |
+ return jsonify({'result': 'fail', 'msg': '토큰이 없습니다.'}), 401 |
|
147 |
+ |
|
148 |
+ try: |
|
149 |
+ # Decode the token to verify it |
|
150 |
+ decoded_token = jwt.decode(token, "secret", algorithms=['HS256']) |
|
151 |
+ user_id = decoded_token['id'] |
|
152 |
+ except jwt.ExpiredSignatureError: |
|
153 |
+ return jsonify({'result': 'fail', 'msg': '토큰이 만료되었습니다.'}), 401 |
|
154 |
+ except jwt.InvalidTokenError: |
|
155 |
+ return jsonify({'result': 'fail', 'msg': '유효하지 않은 토큰입니다.'}), 401 |
|
156 |
+ |
|
157 |
+ # Interact with the DB to get user history |
|
158 |
+ try: |
|
159 |
+ db = DB() |
|
160 |
+ result, status_code = db.get_history(user_name=user_id) |
|
161 |
+ return jsonify({'result': 'success', 'data': result}), status_code |
|
162 |
+ except Exception as e: |
|
163 |
+ return jsonify({'result': 'fail', 'msg': str(e)}), 500(파일 끝에 줄바꿈 문자 없음) |
--- database/database.py
+++ database/database.py
... | ... | @@ -254,8 +254,79 @@ |
254 | 254 |
abrupt_deceleration_count, |
255 | 255 |
helmet_on, |
256 | 256 |
final_score |
257 |
+ ) |
|
257 | 258 |
) |
258 |
- ) |
|
259 |
+ |
|
260 |
+ def get_history(self, user_name): |
|
261 |
+ """ |
|
262 |
+ Retrieves all trip logs for the specified user within the last month and returns them in JSON format. |
|
263 |
+ [ |
|
264 |
+ { |
|
265 |
+ "trip_id": "trip_001", |
|
266 |
+ "timestamp": "2024-09-01 12:45:00", |
|
267 |
+ "total_distance_m": 1000.5, |
|
268 |
+ "total_time_s": 600, |
|
269 |
+ "abrupt_start_count": 3, |
|
270 |
+ "abrupt_stop_count": 2, |
|
271 |
+ "abrupt_acceleration_count": 1, |
|
272 |
+ "abrupt_deceleration_count": 1, |
|
273 |
+ "helmet_on": true, |
|
274 |
+ "final_score": 85.5 |
|
275 |
+ }, |
|
276 |
+ { |
|
277 |
+ "trip_id": "trip_002", |
|
278 |
+ "timestamp": "2024-09-02 14:30:00", |
|
279 |
+ "total_distance_m": 1500.0, |
|
280 |
+ "total_time_s": 720, |
|
281 |
+ "abrupt_start_count": 2, |
|
282 |
+ "abrupt_stop_count": 3, |
|
283 |
+ "abrupt_acceleration_count": 1, |
|
284 |
+ "abrupt_deceleration_count": 2, |
|
285 |
+ "helmet_on": false, |
|
286 |
+ "final_score": 90.0 |
|
287 |
+ } |
|
288 |
+ ] |
|
289 |
+ """ |
|
290 |
+ try: |
|
291 |
+ # Execute the query to retrieve logs within the last month |
|
292 |
+ self.cur.execute(""" |
|
293 |
+ SELECT trip_id, timestamp, total_distance_m, total_time_s, abrupt_start_count, |
|
294 |
+ abrupt_stop_count, abrupt_acceleration_count, abrupt_deceleration_count, |
|
295 |
+ helmet_on, final_score |
|
296 |
+ FROM trip_log |
|
297 |
+ WHERE username = %s |
|
298 |
+ AND timestamp >= NOW() - INTERVAL '1 month' |
|
299 |
+ """, (user_name,)) |
|
300 |
+ |
|
301 |
+ # Fetch all results |
|
302 |
+ rows = self.cur.fetchall() |
|
303 |
+ |
|
304 |
+ # Format the results into a list of dictionaries |
|
305 |
+ result = [] |
|
306 |
+ for row in rows: |
|
307 |
+ trip_log = { |
|
308 |
+ "trip_id": row[0], |
|
309 |
+ "timestamp": row[1].strftime("%Y-%m-%d %H:%M:%S"), # Format timestamp |
|
310 |
+ "total_distance_m": row[2], |
|
311 |
+ "total_time_s": row[3], |
|
312 |
+ "abrupt_start_count": row[4], |
|
313 |
+ "abrupt_stop_count": row[5], |
|
314 |
+ "abrupt_acceleration_count": row[6], |
|
315 |
+ "abrupt_deceleration_count": row[7], |
|
316 |
+ "helmet_on": bool(row[8]), # Convert INT to Boolean |
|
317 |
+ "final_score": row[9] |
|
318 |
+ } |
|
319 |
+ result.append(trip_log) |
|
320 |
+ |
|
321 |
+ # Convert the result list to JSON format |
|
322 |
+ return json.dumps(result), 200 |
|
323 |
+ |
|
324 |
+ except psycopg2.Error as e: |
|
325 |
+ self.conn.rollback() |
|
326 |
+ return {'status': 'error', 'message': str(e)}, 500 |
|
327 |
+ |
|
328 |
+ |
|
329 |
+ |
|
259 | 330 |
|
260 | 331 |
def close_connection(self): |
261 | 332 |
cur = self.cur |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?