Skip to content

Commit

Permalink
fixed schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
weiw-w committed Dec 5, 2023
1 parent 934d294 commit 4fef4da
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
35 changes: 25 additions & 10 deletions ConnectionDB.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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])
Binary file modified __pycache__/ConnectionDB.cpython-311.pyc
Binary file not shown.
17 changes: 7 additions & 10 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
33 changes: 15 additions & 18 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,8 @@ <h2>Delete Comment</h2>
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);
Expand Down Expand Up @@ -1128,7 +1126,7 @@ <h2>Delete Comment</h2>


//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');

Expand All @@ -1138,25 +1136,23 @@ <h2>Delete Comment</h2>

// 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';
}
Expand Down Expand Up @@ -1201,9 +1197,10 @@ <h2>Delete Comment</h2>
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

Expand Down

0 comments on commit 4fef4da

Please sign in to comment.