Skip to content

Commit

Permalink
Update metadata records format
Browse files Browse the repository at this point in the history
* Store one top-level document called `metadata` for each unit, with the specific sub-collection stored as a key name.
* Also create a document in the `records` collection for just the collection.
  • Loading branch information
wtgee committed May 17, 2024
1 parent c0e6750 commit bc78930
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/panoptes/pocs/utils/cli/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ def upload_metadata(dir_path: Path = '.', unit_id: str = None, verbose: bool = F
event_handler = FileSystemEventHandler()
firestore_db = firestore.Client()
# Get the unit reference to link metadata to unit.
unit_metadata_ref = firestore_db.collection(f'units/{unit_id}/metadata')
unit_metadata_ref = firestore_db.document(f'units/{unit_id}/metadata')
metadata_records_ref = unit_metadata_ref.collection('records')

Check warning on line 103 in src/panoptes/pocs/utils/cli/network.py

View check run for this annotation

Codecov / codecov/patch

src/panoptes/pocs/utils/cli/network.py#L102-L103

Added lines #L102 - L103 were not covered by tests

def handleEvent(event):
if event.is_directory:
Expand All @@ -112,23 +113,22 @@ def handleEvent(event):

try:
record = json.loads(Path(event.src_path).read_text())
collection = record['type']

# Get the "current" record and collections refs.
metadata_record = unit_metadata_ref.document(collection)
records_ref = metadata_record.collection('records')

# Unpack the envelope.
collection = record['type']

Check warning on line 118 in src/panoptes/pocs/utils/cli/network.py

View check run for this annotation

Codecov / codecov/patch

src/panoptes/pocs/utils/cli/network.py#L118

Added line #L118 was not covered by tests
data = record['data']
data['date'] = record['date']
data['received_time'] = firestore.SERVER_TIMESTAMP

if verbose:
print(f'Adding {data=}')
print(f'Adding data for {collection=}: {data}')

Check warning on line 124 in src/panoptes/pocs/utils/cli/network.py

View check run for this annotation

Codecov / codecov/patch

src/panoptes/pocs/utils/cli/network.py#L124

Added line #L124 was not covered by tests

# Update the main "current" record for the unit.
unit_metadata_ref.set({collection: data}, merge=True)

Check warning on line 127 in src/panoptes/pocs/utils/cli/network.py

View check run for this annotation

Codecov / codecov/patch

src/panoptes/pocs/utils/cli/network.py#L127

Added line #L127 was not covered by tests

# Update the "current" record.
metadata_record.set(data)
# Add a new record.
doc_ts, doc_id = records_ref.add(data)
# Add the record, storing the collection name in the data.
data['collection'] = collection
doc_ts, doc_id = metadata_records_ref.add(data)

Check warning on line 131 in src/panoptes/pocs/utils/cli/network.py

View check run for this annotation

Codecov / codecov/patch

src/panoptes/pocs/utils/cli/network.py#L130-L131

Added lines #L130 - L131 were not covered by tests
if verbose:
print(f'Added data to firestore with {doc_id.id=} at {doc_ts}')
except Exception as e:
Expand All @@ -152,7 +152,8 @@ def handleEvent(event):
def upload_image_cmd(file_path: Path, bucket_path: str,
bucket_name: str = 'panoptes-images-incoming',
timeout: float = 180.,
storage_client=None) -> str:
storage_client=None
) -> str:
"""Uploads an image to google storage bucket."""
public_url = upload_image(
file_path, bucket_path,
Expand Down

0 comments on commit bc78930

Please sign in to comment.