Skip to content

Commit

Permalink
Update metadata records format (#1269)
Browse files Browse the repository at this point in the history
* Update metadata records format

* 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.

* Change back the state labels in metadata status.
  • Loading branch information
wtgee authored May 18, 2024
1 parent c0e6750 commit f0ce4e7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/panoptes/pocs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ def should_retry(self):
def status(self) -> dict:
try:
status = {
'from_state': self.state,
'to_state': self.next_state,
'state': self.state,
'next_state': self.next_state,
'system': {
'free_space': str(self._free_space),
},
Expand Down
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')

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']
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}')

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

# 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)
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 f0ce4e7

Please sign in to comment.