Browse Source

Added timer

main
astanley 2 years ago
parent
commit
6364c34e8b
  1. 3
      ImportServerUtilities.py
  2. 39
      ImportUtilities.py

3
ImportServerUtilities.py

@ -44,6 +44,7 @@ class ImportServerUtilities:
print(f"No results found for {pid}")
# Gets PIDS, filtered by namespace directly from objectStore
@IU.ImportUtilities.timeit
def get_pids_from_objectstore(self, namespace=''):
wildcard = '*/*'
if namespace:
@ -76,6 +77,7 @@ class ImportServerUtilities:
writer.writerow({'pid': pid, 'dublin_core': dc})
# Copies digital assets from dataStream store to staging directory
@IU.ImportUtilities.timeit
def stage_files(self, content_model, datastreams=None):
if datastreams is None:
datastreams = ['OBJ']
@ -98,6 +100,7 @@ class ImportServerUtilities:
print(f"Datastream not found for {nid}")
# Builds record directly from objectStore
@IU.ImportUtilities.timeit
def build_record_from_pids(self, namespace, output_file):
pids = self.get_pids_from_objectstore(namespace)
headers = [

39
ImportUtilities.py

@ -7,7 +7,7 @@ import urllib
import urllib.parse
import lxml.etree as ET
import re
import time
import ModsTransformer as MT
@ -18,7 +18,8 @@ class ImportUtilities:
self.fields = ['PID', 'model', 'RELS_EXT_isMemberOfCollection_uri_ms', 'RELS_EXT_isPageOf_uri_ms']
self.objectStore = '/usr/local/fedora/data/objectStore/'
self.datastreamStore = '/usr/local/fedora/data/datastreamStore/'
self.rels_map = {'isMemberOfCollection': 'collection_pid',
self.rels_map = {
'isMemberOfCollection': 'collection_pid',
'isMemberOf': 'collection_pid',
'hasModel': 'content_model',
'isPageOf': 'page_of',
@ -26,10 +27,40 @@ class ImportUtilities:
'isConstituentOf': 'constituent_of',
'mods': 'mods'
}
self.mt = MT.ModsTransformer()
self.namespace = namespace
@staticmethod
def human_readable_time(seconds):
"""Convert seconds to a human-readable format (hours, minutes, seconds, milliseconds)."""
hours, remainder = divmod(seconds, 3600)
minutes, remainder = divmod(remainder, 60)
seconds, milliseconds = divmod(remainder, 1)
milliseconds = int(milliseconds * 1000)
parts = []
if hours:
parts.append(f"{int(hours)}h")
if minutes:
parts.append(f"{int(minutes)}m")
if seconds or (not parts and milliseconds == 0): # Always show seconds if no larger unit exists
parts.append(f"{int(seconds)}s")
if milliseconds:
parts.append(f"{milliseconds}ms")
return " ".join(parts)
@staticmethod
def timeit(func):
"""Decorator to measure the execution time of a function in a human-readable format."""
def wrapper(*args, **kwargs):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Function '{func.__name__}' executed in: {ImportUtilities.human_readable_time(elapsed_time)}")
return result
return wrapper
# Adds node_id to table
@timeit
def add_node_ids(self, table, csv_file):
cursor = self.conn.cursor()
with open(csv_file, newline='') as csvfile:
@ -305,4 +336,4 @@ class ImportUtilities:
if __name__ == '__main__':
MU = ImportUtilities('ivoices')
MU.add_node_ids('ivoices', 'inputs/ivoice-nid-pid.csv')
MU.example_function()
Loading…
Cancel
Save