Skip to content

Commit

Permalink
Updated code and added a test.
Browse files Browse the repository at this point in the history
  • Loading branch information
deoyani committed Dec 18, 2023
1 parent 3c6621c commit 654313d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
41 changes: 25 additions & 16 deletions python/nistoar/rmm/mongo/nerdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,44 +338,53 @@ def init_metrics_for(db, nerdm):
for col in record_collection_fields.keys():
if col not in records.keys():
records[col] = record_collection_fields[col]

if(db["recordMetrics"].find_one({"ediid": nerdm["ediid"]}) is None):
db["recordMetrics"].insert_one(records)

#Get files from record components
files = flatten_records(nerdm, record_fields, files_collection_fields)
files = flatten_records(nerdm, files_collection_fields)
files_to_update = []

current_files = db["fileMetrics"].find({"ediid": nerdm["ediid"]})
current_files_filepaths = [x["filepath"] for x in current_files]
for file_item in files:
if file_item['filepath'] not in current_files_filepaths:
files_to_update.append(file_item)
db["fileMetrics"].insert_many(files_to_update)
if 'filepath' in file_item.keys():
if file_item['filepath'] not in current_files_filepaths:
files_to_update.append(file_item)

if len(files_to_update)>0:
db["fileMetrics"].insert_many(files_to_update)


def flatten_records(record, record_fields, initialize_fields):
# This takes a nerdm record and collect the files related data from components.
# Inputs are record=nerdm to be updated
# initialize fields=fileMetrics fields to be updated
def flatten_records(record, initialize_fields):
files = []
keys_to_keep = ['filepath', 'size', 'downloadURL']

keys_to_keep = ['filepath', 'size', 'downloadURL', 'ediid', '@id']
for component in record['components']:
#Get file information
file_dict = {}
if 'filepath' in component.keys():
#Initialize fields
for key in initialize_fields.keys():
file_dict[key] = initialize_fields[key]
#Get file information

if 'filepath' in component.keys():
for key in keys_to_keep:
if key in component.keys():
file_dict[key] = component[key]

if 'size' in file_dict.keys():
file_dict['filesize'] = file_dict.pop('size')
else:
file_dict['filesize'] = 0

if 'downloadURL' not in component.keys():
file_dict['downloadURL'] = ''
#Get record information
for key in record_fields:
file_dict[key] = record[key]
#Initialize other fields
for key in initialize_fields.keys():
file_dict[key] = initialize_fields[key]

file_dict['pdrid'] = record['pdrid']
file_dict['ediid'] = record['ediid']

files.append(file_dict)
return files

1 change: 1 addition & 0 deletions python/tests/nistoar/rmm/mongo/test_nerdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def test_init_metrics_for(self):
self.assertEqual(c[0]['pdrid'], 'ark:/88434/mds2-2106')
c = self.ldr._client.get_database().fileMetrics.find()
self.assertEqual(c[0]['pdrid'], 'ark:/88434/mds2-2106')
self.assertEqual(c[0]['filepath'], "NIST_NPL_InterlabData2019.csv.sha256")
# replace this with checks of successful loading into the database
#self.fail("Tests not implemented")

Expand Down

0 comments on commit 654313d

Please sign in to comment.