Skip to content

Commit

Permalink
Metadata records
Browse files Browse the repository at this point in the history
* Save the metadata directly under the unit doc, which saves us some reads for the dashboard.
  • Loading branch information
wtgee committed May 18, 2024
1 parent f0ce4e7 commit 244122f
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/panoptes/pocs/utils/cli/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +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.document(f'units/{unit_id}/metadata')
metadata_records_ref = unit_metadata_ref.collection('records')
unit_ref = firestore_db.document(f'units/{unit_id}')
metadata_records_ref = unit_ref.collection('metadata')

def handleEvent(event):
if event.is_directory:
Expand All @@ -115,19 +115,19 @@ def handleEvent(event):
record = json.loads(Path(event.src_path).read_text())

# Unpack the envelope.
collection = record['type']
record_type = record['type']
data = record['data']
data['date'] = record['date']
data['received_time'] = firestore.SERVER_TIMESTAMP

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

# Update the main "current" record for the unit.
unit_metadata_ref.set({collection: data}, merge=True)
# Update the unit's metadata with the record_type.
unit_ref.set({'metadata': {record_type: data}}, merge=True)

# Add the record, storing the collection name in the data.
data['collection'] = collection
# Add the record, storing the record_type name in the data.
data['record_type'] = record_type
doc_ts, doc_id = metadata_records_ref.add(data)
if verbose:
print(f'Added data to firestore with {doc_id.id=} at {doc_ts}')
Expand Down

0 comments on commit 244122f

Please sign in to comment.