-
Notifications
You must be signed in to change notification settings - Fork 6
fix: reduce memory usage in export_csv function. #1469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
davidgamez
merged 6 commits into
main
from
Export-CSV-function-exceeds-memory-limit-898
Nov 19, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
03f4715
Modified to reduce memory usage in export_csv function.
jcpitre c39136d
Added missing python package
jcpitre fe821db
Increased timeout to 1 hour for export_csv
jcpitre c90b6e7
Merge branch 'main' into Export-CSV-function-exceeds-memory-limit-898
jcpitre c9c30e2
Limited the changes to code used by export_csv.
jcpitre 49a3c8c
Added query by batch.
jcpitre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,4 +27,4 @@ python-dotenv==1.0.0 | |
|
|
||
| # Other dependencies | ||
| natsort | ||
|
|
||
| psutil | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,30 @@ def populate_database(db_session): | |
| fake = Faker() | ||
|
|
||
| feeds = [] | ||
|
|
||
| # Put the deprecated feeds before the active feeds in the DB so they will be listed first | ||
| # in GtfsRealtimeFeed.gtfs_feeds (the RT feed references). This allows testing that active feeds | ||
| # are put first in GtfsRealtimeFeed.gtfs_feeds. Admittedly, it's a bit weak but it works for now. | ||
| for i in range(2): | ||
| feed = Gtfsfeed( | ||
| id=fake.uuid4(), | ||
| data_type="gtfs", | ||
| feed_name=f"deprecated-gtfs-{i} Some fake name", | ||
| note=f"deprecated-gtfs-{i} Some fake note", | ||
| producer_url=f"https://deprecated-gtfs-{i}_some_fake_producer_url", | ||
| authentication_type="0" if (i == 0) else "1", | ||
| authentication_info_url=None, | ||
| api_key_parameter_name=None, | ||
| license_url=f"https://gtfs-{i}_some_fake_license_url", | ||
| stable_id=f"deprecated-gtfs-{i}", | ||
| status="deprecated", | ||
| feed_contact_email=f"deprecated-gtfs-{i}[email protected]", | ||
| provider=f"deprecated-gtfs-{i} Some fake company", | ||
| operational_status="published", | ||
| official=True, | ||
| ) | ||
| db_session.add(feed) | ||
|
|
||
| # We create 3 feeds. The first one is active. The third one is inactive and redirected to the first one. | ||
| # The second one is active but not redirected. | ||
| # First fill the generic parameters | ||
|
|
@@ -97,25 +121,6 @@ def populate_database(db_session): | |
| for feed in feeds: | ||
| db_session.add(feed) | ||
| db_session.flush() | ||
| for i in range(2): | ||
| feed = Gtfsfeed( | ||
| id=fake.uuid4(), | ||
| data_type="gtfs", | ||
| feed_name=f"gtfs-deprecated-{i} Some fake name", | ||
| note=f"gtfs-deprecated-{i} Some fake note", | ||
| producer_url=f"https://gtfs-deprecated-{i}_some_fake_producer_url", | ||
| authentication_type="0" if (i == 0) else "1", | ||
| authentication_info_url=None, | ||
| api_key_parameter_name=None, | ||
| license_url=f"https://gtfs-{i}_some_fake_license_url", | ||
| stable_id=f"gtfs-deprecated-{i}", | ||
| status="deprecated", | ||
| feed_contact_email=f"gtfs-deprecated-{i}[email protected]", | ||
| provider=f"gtfs-deprecated-{i} Some fake company", | ||
| operational_status="published", | ||
| official=True, | ||
| ) | ||
| db_session.add(feed) | ||
|
|
||
| location_entity = Location(id="CA-quebec-montreal") | ||
|
|
||
|
|
@@ -273,10 +278,28 @@ def populate_database(db_session): | |
| entitytypes=[vp_entitytype, tu_entitytype] if i == 0 else [vp_entitytype], | ||
| operational_status="published", | ||
| official=True, | ||
| gtfs_feeds=[active_gtfs_feeds[0]] if i == 0 else [], | ||
| # Do not attach GTFS feeds at creation; we'll set them in a controlled order below | ||
| # gtfs_feeds=[], | ||
| ) | ||
| gtfs_rt_feeds.append(feed) | ||
|
|
||
| db_session.add_all(gtfs_rt_feeds) | ||
|
|
||
| # --- Attach both a deprecated GTFS feed and an active GTFS feed to the first RT feed | ||
| try: | ||
| deprecated_feeds = ( | ||
| db_session.query(Gtfsfeed) | ||
| .filter(Gtfsfeed.status == "deprecated") | ||
| .order_by(Gtfsfeed.stable_id) | ||
| .all() | ||
| ) | ||
| if deprecated_feeds: | ||
| gtfs_rt_feeds[0].gtfs_feeds = [deprecated_feeds[0], active_gtfs_feeds[0]] | ||
| db_session.flush() | ||
| except Exception: | ||
| # Best effort in test setup; if it fails the rest of the tests will surface the issue. | ||
| pass | ||
|
|
||
| # Add redirecting IDs (from main branch logic) | ||
| gtfs_rt_feeds[1].redirectingids = [ | ||
| Redirectingid( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,12 +24,12 @@ | |
| # the data is correct. | ||
| expected_csv = """ | ||
| id,data_type,entity_type,location.country_code,location.subdivision_name,location.municipality,provider,is_official,name,note,feed_contact_email,static_reference,urls.direct_download,urls.authentication_type,urls.authentication_info,urls.api_key_parameter_name,urls.latest,urls.license,location.bounding_box.minimum_latitude,location.bounding_box.maximum_latitude,location.bounding_box.minimum_longitude,location.bounding_box.maximum_longitude,location.bounding_box.extracted_on,status,features,redirect.id,redirect.comment | ||
| deprecated-gtfs-0,gtfs,,,,,deprecated-gtfs-0 Some fake company,True,deprecated-gtfs-0 Some fake name,deprecated-gtfs-0 Some fake note,[email protected],,https://deprecated-gtfs-0_some_fake_producer_url,0,,,,https://gtfs-0_some_fake_license_url,,,,,,deprecated,,, | ||
| deprecated-gtfs-1,gtfs,,,,,deprecated-gtfs-1 Some fake company,True,deprecated-gtfs-1 Some fake name,deprecated-gtfs-1 Some fake note,[email protected],,https://deprecated-gtfs-1_some_fake_producer_url,1,,,,https://gtfs-1_some_fake_license_url,,,,,,deprecated,,, | ||
| gtfs-0,gtfs,,CA,Quebec,Laval,gtfs-0 Some fake company,True,gtfs-0 Some fake name,gtfs-0 Some fake note,[email protected],,https://gtfs-0_some_fake_producer_url,0,,,https://url_prefix/gtfs-0/latest.zip,https://gtfs-0_some_fake_license_url,-9.0,9.0,-18.0,18.0,2025-01-12 00:00:00+00:00,active,Route Colors|Shapes,, | ||
| gtfs-1,gtfs,,CA,Quebec,Montreal,gtfs-1 Some fake company,True,gtfs-1 Some fake name,gtfs-1 Some fake note,[email protected],,https://gtfs-1_some_fake_producer_url,0,,,https://url_prefix/gtfs-1/latest.zip,https://gtfs-1_some_fake_license_url,-9.0,9.0,-18.0,18.0,2025-01-12 00:00:00+00:00,active,Route Colors|Shapes,, | ||
| gtfs-2,gtfs,,,,,gtfs-2 Some fake company,True,gtfs-2 Some fake name,gtfs-2 Some fake note,[email protected],,https://gtfs-2_some_fake_producer_url,0,,,,https://gtfs-2_some_fake_license_url,,,,,,inactive,,gtfs-0,Some redirect comment | ||
| gtfs-deprecated-0,gtfs,,,,,gtfs-deprecated-0 Some fake company,True,gtfs-deprecated-0 Some fake name,gtfs-deprecated-0 Some fake note,[email protected],,https://gtfs-deprecated-0_some_fake_producer_url,0,,,,https://gtfs-0_some_fake_license_url,,,,,,deprecated,,, | ||
| gtfs-deprecated-1,gtfs,,,,,gtfs-deprecated-1 Some fake company,True,gtfs-deprecated-1 Some fake name,gtfs-deprecated-1 Some fake note,[email protected],,https://gtfs-deprecated-1_some_fake_producer_url,1,,,,https://gtfs-1_some_fake_license_url,,,,,,deprecated,,, | ||
| gtfs-rt-0,gtfs_rt,tu|vp,,,,gtfs-rt-0 Some fake company,True,gtfs-rt-0 Some fake name,gtfs-rt-0 Some fake note,[email protected],gtfs-0,https://gtfs-rt-0_some_fake_producer_url,0,https://gtfs-rt-0_some_fake_authentication_info_url,gtfs-rt-0_fake_api_key_parameter_name,,https://gtfs-rt-0_some_fake_license_url,-9.0,9.0,-18.0,18.0,2025-01-12 00:00:00+00:00,active,,, | ||
| gtfs-rt-0,gtfs_rt,tu|vp,,,,gtfs-rt-0 Some fake company,True,gtfs-rt-0 Some fake name,gtfs-rt-0 Some fake note,[email protected],gtfs-0|deprecated-gtfs-0,https://gtfs-rt-0_some_fake_producer_url,0,https://gtfs-rt-0_some_fake_authentication_info_url,gtfs-rt-0_fake_api_key_parameter_name,,https://gtfs-rt-0_some_fake_license_url,-9.0,9.0,-18.0,18.0,2025-01-12 00:00:00+00:00,active,,, | ||
| gtfs-rt-1,gtfs_rt,vp,,,,gtfs-rt-1 Some fake company,True,gtfs-rt-1 Some fake name,gtfs-rt-1 Some fake note,[email protected],,https://gtfs-rt-1_some_fake_producer_url,1,https://gtfs-rt-1_some_fake_authentication_info_url,gtfs-rt-1_fake_api_key_parameter_name,,https://gtfs-rt-1_some_fake_license_url,,,,,,inactive,,gtfs-rt-0|gtfs-rt-2,comment 1|comment 2 | ||
| gtfs-rt-2,gtfs_rt,vp,,,,gtfs-rt-2 Some fake company,True,gtfs-rt-2 Some fake name,gtfs-rt-2 Some fake note,[email protected],,https://gtfs-rt-2_some_fake_producer_url,2,https://gtfs-rt-2_some_fake_authentication_info_url,gtfs-rt-2_fake_api_key_parameter_name,,https://gtfs-rt-2_some_fake_license_url,,,,,,active,,, | ||
| """ # noqa | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.