Skip to content

Commit

Permalink
logger: temporarily made it singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
kpsherva committed Jul 26, 2024
1 parent 71ab28e commit df513bf
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 25 deletions.
1 change: 1 addition & 0 deletions cds_migrator_kit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from invenio_app.config import APP_DEFAULT_SECURE_HEADERS


def _(x):
"""Identity function used to trigger string extraction."""
return x
Expand Down
2 changes: 1 addition & 1 deletion cds_migrator_kit/rdm/migration/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from cds_migrator_kit.records.log import RDMJsonLogger

cli_logger = logging.getLogger("migrator")
migration_logger = RDMJsonLogger()


@click.group()
def migration():
Expand Down
8 changes: 5 additions & 3 deletions cds_migrator_kit/rdm/migration/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
from invenio_rdm_migrator.load.base import Load
from invenio_rdm_records.proxies import current_rdm_records_service

from cds_migrator_kit.rdm.migration.transform.xml_processing.errors import \
ManualImportRequired
from cds_migrator_kit.rdm.migration.transform.xml_processing.errors import (
ManualImportRequired,
)
from cds_migrator_kit.records.log import RDMJsonLogger

cli_logger = logging.getLogger("migrator")

Expand Down Expand Up @@ -43,8 +45,8 @@ def _prepare(self, entry):

def _load(self, entry):
"""Use the services to load the entries."""
from cds_migrator_kit.rdm.migration.cli import migration_logger, cli_logger
recid = entry.get("record", {}).get("json", {}).get("id")
migration_logger = RDMJsonLogger()
migration_logger.add_recid_to_stats(recid)
identity = system_identity # Should we create an identity for the migration?
draft = current_rdm_records_service.create(identity, entry["record"]["json"])
Expand Down
4 changes: 1 addition & 3 deletions cds_migrator_kit/rdm/migration/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ def run(self):
# on successful stream run, persist state
# STATE.flush_cache()
# self.state.save(filename=f"{stream.name}.db")
from cds_migrator_kit.rdm.migration.cli import migration_logger, \
cli_logger

migration_logger = RDMJsonLogger()
migration_logger.save()
except Exception:
Logger.get_logger().exception(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class CMSSummerStudent(CdsOverdo):
__query__ = "980__:NOTE 037__:CERN-STUDENTS-Note-*"

__ignore_keys__ = {
#"0247_2", # DOI, summer student notes do not have it
#"0247_a", # DOI
# "0247_2", # DOI, summer student notes do not have it
# "0247_a", # DOI
"0248_a", # oai identifier, not needed to migrate, TBD
"0248_p", # oai identifier, not needed to migrate, TBD
# "0248_q", does appear
Expand All @@ -54,7 +54,7 @@ class CMSSummerStudent(CdsOverdo):
# "270__p",
# "500__a", # Note
# "562__c", # note
"595__a", # always value CERN EDS, not displayed, TODO: do we keep?
"595__a", # always value CERN EDS, not displayed, TODO: do we keep?
"595__z", # SOME RECORD HAVE UNCL as value, do we keep it?
"650172", # TODO TBD
"65017a", # TODO TBD
Expand All @@ -80,8 +80,8 @@ class CMSSummerStudent(CdsOverdo):
# "906__p", # probably supervisor TODO: check
# "916__s", # creation date
# "916__w", # creation date
"960__a", # collection id? usuall y valu 12
"963__a", # restriction
"960__a", # collection id? usuall y valu 12
"963__a", # restriction
"980__a",
"980__c", # TODO: remove this one, it should not appear
}
Expand Down
10 changes: 3 additions & 7 deletions cds_migrator_kit/rdm/migration/transform/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,13 @@
RDMRecordTransform,
)


from cds_migrator_kit.rdm.migration.transform.xml_processing.dumper import CDSRecordDump
from cds_migrator_kit.rdm.migration.transform.xml_processing.errors import (
LossyConversion,
)
from cds_migrator_kit.records.log import RDMJsonLogger


CDS_DATACITE_PREFIXES = [
"10.17181"
]
CDS_DATACITE_PREFIXES = ["10.17181"]


class CDSToRDMRecordEntry(RDMRecordEntry):
Expand Down Expand Up @@ -85,7 +82,6 @@ def _media_files(self, entry):
def _pids(self, json_entry):
return []


def _files(self, record_dump):
"""Transform the files of a record."""
record_dump.prepare_files()
Expand Down Expand Up @@ -129,7 +125,7 @@ def _resource_type(data):

def transform(self, entry):
"""Transform a record single entry."""
from cds_migrator_kit.rdm.migration.cli import migration_logger, cli_logger
migration_logger = RDMJsonLogger()
record_dump = CDSRecordDump(
entry,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from dojson.utils import force_list

from cds_migrator_kit.rdm.migration.transform.xml_processing.errors import (
UnexpectedValue, MissingRequiredField,
MissingRequiredField,
UnexpectedValue,
)


Expand Down
23 changes: 18 additions & 5 deletions cds_migrator_kit/records/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,19 @@
)


class JsonLogger(object):
class Singleton(type):
"""Temporary solution for this logger."""

_instances = {}

def __call__(cls, *args, **kwargs):
"""Call."""
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]


class JsonLogger(metaclass=Singleton):
"""Log migration statistic to file controller."""

logger = None
Expand Down Expand Up @@ -79,9 +91,8 @@ def get_json_logger(cls):

def __init__(self, stats_filename, records_filename):
"""Constructor."""
from ..migration_config import CDS_MIGRATOR_KIT_LOGS_PATH

self._logs_path = CDS_MIGRATOR_KIT_LOGS_PATH
self._logs_path = current_app.config["CDS_MIGRATOR_KIT_LOGS_PATH"]
self.stats = {}
self.records = {}
self.STAT_FILEPATH = os.path.join(self._logs_path, stats_filename)
Expand Down Expand Up @@ -122,7 +133,9 @@ def add_log(self, exc, key=None, value=None, output=None):

def resolve_error_type(self, exc, output, key, value):
"""Check the type of exception and log to dict."""
recid = output.get("recid", None) or output.get("record", {}).get("json", {}).get("id")
recid = output.get("recid", None) or output.get("record", {}).get(
"json", {}
).get("id")
rec_stats = self.stats[recid]
rec_stats["clean"] = False
if isinstance(exc, ManualImportRequired):
Expand Down Expand Up @@ -175,7 +188,7 @@ def resolve_error_type(self, exc, output, key, value):
" Contact CDS team to tune the query"
)
else:
raise exc
raise exc


class RDMJsonLogger(JsonLogger):
Expand Down

0 comments on commit df513bf

Please sign in to comment.