Skip to content

Commit

Permalink
Use max instead of sum on the times count for the denominator
Browse files Browse the repository at this point in the history
  • Loading branch information
gsarrco committed Jan 17, 2024
1 parent 47a5706 commit 46dd530
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
6 changes: 3 additions & 3 deletions server/GTFS/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ def upload_stops_clusters_to_db(self, force=False) -> bool:
''')
stops = self.get_all_stops()
stops_clusters = get_clusters_of_stops(stops)
total_times_count = sum([cluster.times_count for cluster in stops_clusters])
max_times_count = max([cluster.times_count for cluster in stops_clusters])

new_stations = []
new_stops = []

for cluster in stops_clusters:
times_count = round(cluster.times_count / total_times_count,
int(math.log10(total_times_count)) + 1)
times_count = round(cluster.times_count / max_times_count,
int(math.log10(max_times_count)) + 1)
ids = ','.join([str(stop.id) for stop in cluster.stops])
station = Station(id=cluster.name, name=cluster.name, lat=cluster.lat, lon=cluster.lon, ids=ids,
times_count=times_count, source=self.name)
Expand Down
10 changes: 6 additions & 4 deletions server/trenitalia/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,23 @@ def __init__(self, session, typesense, location='', force_update_stations=False)
def save_data(self):
stations = self.session.scalars(select(Station).filter_by(source=self.name, active=True)).all()

total_times_count = 0
max_times_count = 0
times_count = []

tqdm_stations = tqdm(enumerate(stations), total=len(stations), desc=f'Uploading {self.name} data')

for i, station in tqdm_stations:
tqdm_stations.set_description(f'Processing station {station.name}')
stop_times = self.get_stop_times_from_station(station)
total_times_count += len(stop_times)
times_count.append(len(stop_times))
stop_times_count = len(stop_times)
if stop_times_count > max_times_count:
max_times_count = stop_times_count
times_count.append(stop_times_count)
for stop_time in stop_times:
self.upload_trip_stop_time_to_postgres(stop_time)

for i, station in enumerate(stations):
station.times_count = round(times_count[i] / total_times_count, int(math.log10(total_times_count)) + 1)
station.times_count = round(times_count[i] / max_times_count, int(math.log10(max_times_count)) + 1)
self.sync_stations_db(stations)

def get_stop_times_from_station(self, station) -> list[TripStopTime]:
Expand Down

0 comments on commit 46dd530

Please sign in to comment.