윤영준 윤영준 2024-07-17
first commit of refactoring of data transaction - gps data, attempt to fix problems that 1. data being sent too often, 2. in previous method, the data insert operation happens too often. this commit is trying to fix it by 1. data being sent every 15sec (or more) not every sec, 2. reformatted gps data so that data transaction will cost a lot less bandwidth and 3. insertion operation has been replaced with copy, which is more apt for bulk row appendage operation.
@b31a01ea1157f103fbfb92e1ac470d0bc1d91344
action.py
--- action.py
+++ action.py
@@ -12,6 +12,8 @@
 from PIL import Image
 from pothole_model.pothole import pothole
 from datetime import datetime
+import pandas as pd
+from io import StringIO
 import jwt
 
 
@@ -164,14 +166,16 @@
     def post(self):
         db = DB()
         data = request.get_json()
-        timestamp = data['timestamp']
-        trip_id = data['trip_id']
-        location_x = float(data['location_x'])
-        location_y = float(data['location_y'])
+        
+        df = pd.DataFrame(data["trip_log"])
+        df["user_id"] = data["user_id"]
+        df["trip_id"] = data["trip_id"]
+        
+        columns = df.columns
+        data_csv_block = df.to_csv(header=False)
+        
+        
         token = request.headers.get('Authorization')
-        #print(token)
-        print(timestamp,location_x,location_y)
-        #print(token)
         if not token:
             return jsonify({'result': 'fail', 'msg': '토큰이 없습니다.'})
         
@@ -185,8 +189,7 @@
         db.insert_gps_data(trip_id, location_x, location_y, user_id, timestamp)
         return jsonify({'result': 'success'})
             
-
-         
+            
 @Action.route('/pothole_display')
 class fileUpload(Resource):
     @Action.doc(responses={200: 'Success'})
database/database.py
--- database/database.py
+++ database/database.py
@@ -198,18 +198,15 @@
         result=cur.fetchall()
         return result
 
-    def insert_gps_data(self, trip_id, location_x, location_y,user_id, timestamp):
+    def insert_gps_data(self, csv_block, columns):
         cur = self.conn.cursor()
         
-        print(trip_id, location_x, location_y, user_id)
-        trip_id = trip_id['current'] 
-        cur.execute(f'''
-        INSERT INTO "{self.schema}".gps_data (timestamp, trip_id, location_x, location_y,user_id)
-        VALUES (%s, %s, %s, %s,%s);
-        ''', (timestamp, trip_id, location_x, location_y,user_id))
+        data = StringIO(csv_block)
         
+        cur.copy_from(data, 'gps_data', sep=',' columns = columns)
+        self.conn.commit()
         cur.close()
-        return True        
+        return True             
 
 
     
Add a comment
List