-
Notifications
You must be signed in to change notification settings - Fork 2
Implement API client and add Pydantic classes #56
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
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
dbed180
support docker based ingest
shreddd b090ddc
reformat w/ ruff
shreddd cdb0c57
Update src/server.py
shreddd a781242
Update mongodb/README.md
shreddd 16527d1
raise 404
shreddd 46931aa
Add API endpoints
shreddd 52f34a0
Merge branch 'main' into api-endpoints
shreddd 28f9baf
ruff formatting
shreddd 6be3853
fix merge conflict
shreddd aaba928
Add support for pydantic schema model
shreddd c99491c
Add schema file
shreddd 5832341
return pydantic objects
shreddd e7affd3
ruff
shreddd fa26166
example client
shreddd 3d78515
update schema
shreddd 009cb90
typos
shreddd 788bbef
update pydantic model
shreddd d5e77b3
ruff
shreddd 7cafeef
Merge branch 'main' into pydantic-models
shreddd 7c8b2dd
fix linting
shreddd e0ee8b5
Switch to bertron-schema installed via pip from git
shreddd db2c84a
remove redundant entity conversion
shreddd 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
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 was deleted.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -44,6 +44,20 @@ def connect(self) -> None: | |
| logger.error(f"Failed to connect to MongoDB: {e}") | ||
| sys.exit(1) | ||
|
|
||
| def clean_collections(self) -> None: | ||
| """Delete existing collections to start fresh.""" | ||
| try: | ||
| collection_names = self.db.list_collection_names() | ||
| if 'entities' in collection_names: | ||
| logger.info("Dropping existing 'entities' collection") | ||
| self.db.entities.drop() | ||
| logger.info("Successfully dropped 'entities' collection") | ||
| else: | ||
| logger.info("No existing 'entities' collection found") | ||
| except PyMongoError as e: | ||
| logger.error(f"Error dropping collections: {e}") | ||
| sys.exit(1) | ||
|
|
||
| def load_schema(self) -> Dict: | ||
| """Load the JSON schema from file.""" | ||
| try: | ||
|
|
@@ -82,7 +96,7 @@ def insert_entity(self, entity: Dict) -> Optional[str]: | |
| if 'coordinates' in entity: | ||
| coordinates = entity['coordinates'] | ||
| if isinstance(coordinates, dict) and 'latitude' in coordinates and 'longitude' in coordinates: | ||
| entity['coordinates'] = { | ||
| entity['geojson'] = { | ||
| 'type': 'Point', | ||
| 'coordinates': [coordinates['longitude'], coordinates['latitude']] | ||
| } | ||
|
|
@@ -97,7 +111,7 @@ def insert_entity(self, entity: Dict) -> Optional[str]: | |
| self.db.entities.create_index('data_type') | ||
|
|
||
| # Create 2dsphere index for geospatial queries on coordinates | ||
| self.db.entities.create_index([('coordinates', pymongo.GEOSPHERE)]) | ||
| self.db.entities.create_index([('geojson', pymongo.GEOSPHERE)]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am surprised to see indexes being created each time an entity gets inserted (as opposed to after all entities have been inserted). Edit: Maybe these are effectively "no op"s when the index already exists. |
||
|
|
||
| # Insert with upsert to handle potential duplicates based on URI | ||
| result = self.db.entities.update_one( | ||
|
|
@@ -167,6 +181,8 @@ def main(): | |
| help='Path or URL to the BERtron schema JSON file') | ||
| parser.add_argument('--input', required=True, | ||
| help='Path to the input JSON file or directory') | ||
| parser.add_argument('--clean', action='store_true', | ||
| help='Delete existing collections before ingesting new data') | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
|
|
@@ -180,6 +196,11 @@ def main(): | |
| ingestor.connect() | ||
| ingestor.load_schema() | ||
|
|
||
| # Clean collections if requested | ||
| if args.clean: | ||
| logger.info("Clean flag enabled - removing existing collections") | ||
| ingestor.clean_collections() | ||
|
|
||
| total_stats = { | ||
| 'processed': 0, | ||
| 'valid': 0, | ||
|
|
||
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
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.