From 4fef4da48bdb9731377109acaad2aa5c6ea15ee3 Mon Sep 17 00:00:00 2001 From: Wei Wu <128621069+weiw-w@users.noreply.github.com> Date: Tue, 5 Dec 2023 02:43:18 -0600 Subject: [PATCH] fixed schedule --- ConnectionDB.py | 35 ++++++++++++++++------- __pycache__/ConnectionDB.cpython-311.pyc | Bin 6028 -> 6610 bytes app.py | 17 +++++------ templates/index.html | 33 ++++++++++----------- 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/ConnectionDB.py b/ConnectionDB.py index 9245fff..5905e95 100644 --- a/ConnectionDB.py +++ b/ConnectionDB.py @@ -1,6 +1,7 @@ import mysql.connector import math from datetime import datetime +from datetime import timedelta import numpy as np from sqlalchemy import text @@ -103,8 +104,6 @@ def get_schedule(trip_id): result = mycursor.callproc('stopNum',[trip_id, 0]) stop_num = result[1] - f_count = -1 - s_count = 0 # To get the list of stop names in order @@ -125,25 +124,41 @@ def get_schedule(trip_id): schedule = np.zeros([freq_num,stop_num]) schedule = schedule.astype('str') + day = timedelta(hours=24) for result in mycursor.stored_results(): details = result.fetchall() for det in details: - if (det[1] == 1): - f_count += 1 - schedule[f_count,det[1]-1] = str(det[0]) + t = timedelta(seconds=0) + if det[0] > day: + t = det[0] - day + schedule[det[3]][det[1]-1] = str(t) + else: + schedule[det[3]][det[1]-1] = str(det[0]) + + schedule_list = {} + stop_list = {} - schedule_list = [] - for freq in schedule: - schedule_list.append(dict(zip(stops,freq))) + for i in range(len(schedule)): + l = schedule[i].tolist() + schedule_list[str(i)] = l + + for i in range(len(stops)): + stop_list[str(i)] = stops[i] mycursor.close() db.close() - return schedule_list + result = [stop_list,schedule_list] + return result # get_close_by_routes(-23.439609, -46.807039) -# get_schedule('1012-10-0') +# get_schedule('5141-10-1') +# day = timedelta(hours=24) +# t = timedelta(days=1, seconds=360) +# new_t = t - day +# print(new_t) + # print(get_schedule('1012-10-1')[0]) diff --git a/__pycache__/ConnectionDB.cpython-311.pyc b/__pycache__/ConnectionDB.cpython-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jQSda%ki<%Zh|Gh1Fu}h;Sg8@#e!;`s<^uZQ-Tp*pDmKgd eD}=ol&%sXTdzwx!(vQ>YlP~beL+<|&?EeR}>{4(5 diff --git a/app.py b/app.py index fdb7a5f..865f678 100644 --- a/app.py +++ b/app.py @@ -787,20 +787,17 @@ def get_route_and_trip_info(): @app.route('/post_schedule', methods = ['POST']) def post_schedule(): - data = request.get_json() - trip_id = data.get('trip_id') - - # Serialize the dictionary to JSON without sorting keys - json_str = json.dumps(get_schedule(trip_id), sort_keys=False) - # print(json_str) - - # Use jsonify with the serialized JSON string - response = jsonify(json.loads(json_str)) - + json_data = request.get_json() + trip_id = json_data.get('trip_id') + print(trip_id) + schedule_data = get_schedule(trip_id) + print(schedule_data[0]) + response = jsonify([{'stops': schedule_data[0], 'schedule': schedule_data[1]}]) return response + if __name__ == '__main__': with app.app_context(): create_triggers() diff --git a/templates/index.html b/templates/index.html index 202fa61..05c8953 100644 --- a/templates/index.html +++ b/templates/index.html @@ -508,10 +508,8 @@

Delete Comment

else { // closeByStops: 'stop_name', 'stop_lat', 'stop_lon' closeByStops = data[0].stopInfo; - console.log(closeByStops) // closeByRoutes: 'id' closeByRoutes = data[0].routeInfo; - console.log(closeByRoutes); clearMap(); setNewCenter(searchAddress.latitude, searchAddress.longitude); map.setZoom(16); @@ -1128,7 +1126,7 @@

Delete Comment

//Create a the schedule table with given data - function generateSchedule(schedule){ + function generateSchedule(name, schedule){ // Check if there is an existing table and remove it const tableBody = document.getElementById('schedule-body'); @@ -1138,25 +1136,23 @@

Delete Comment

// Get the header row with all the stops const headerRow = document.createElement('tr'); - Object.keys(schedule[0]).forEach(stop => { + for (n in name){ const th = document.createElement('th'); - th.textContent = stop; + th.textContent = name[n]; headerRow.appendChild(th); - }); tableBody.appendChild(headerRow); + } // Loop through the JSON data and create table rows - schedule.forEach(item => { + for (freq in schedule){ const row = document.createElement('tr'); - - // Create table cells for each property - Object.keys(item).forEach(key => { - const cell = document.createElement('td'); - cell.textContent = item[key]; - row.appendChild(cell); - }); + for (var key in schedule[freq]) { + const col = document.createElement('td'); + col.textContent = schedule[freq][key]; + row.appendChild(col); + } tableBody.appendChild(row); - }); + }; document.getElementById('schedule-body').style.display = 'block'; } @@ -1201,9 +1197,10 @@

Delete Comment

alert(data.error); } else{ - console.log(data) // BUT here it is sorted?????? - console.log(Object.keys(data[0])[0]); - generateSchedule(data); + // console.log(data[0].schedule) + // console.log(typeof data[0].schedule) + console.log(data[0].stops) + generateSchedule(data[0].stops, data[0].schedule); // also populate the route info as well as the comments