Skip to content

Commit

Permalink
Dump selected sysinfo to tracklog.
Browse files Browse the repository at this point in the history
  • Loading branch information
perolavsvendsen committed Jan 19, 2024
1 parent 5bcc749 commit c1f8ae6
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 deletions.
5 changes: 5 additions & 0 deletions schema/definitions/0.8.0/examples/surface_depth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ tracklog:
user:
id: peesv
event: created
sysinfo:
fmu-dataio:
version: 1.2.3
komodo: # only added when running in Komodo environment
release: 2023.12.05-py38
- datetime: 2020-10-28T14:46:14
user:
id: peesv
Expand Down
27 changes: 27 additions & 0 deletions schema/definitions/0.8.0/schema/fmu_results.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,33 @@
"created",
"updated"
]
},
"sysinfo": {
"type": "object",
"properties": {
"fmu-dataio": {
"type": "object",
"properties": {
"version": {
"type": "string",
"examples": [
"1.2.3"
]
}
}
},
"komodo": {
"type": "object",
"properties": {
"version": {
"type": "string",
"examples": [
"2023.12.05-py38"
]
}
}
}
}
}
}
},
Expand Down
22 changes: 18 additions & 4 deletions src/fmu/dataio/_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing import Any, Final
from warnings import warn

from fmu import dataio
from fmu.dataio._definitions import SCHEMA, SOURCE, VERSION
from fmu.dataio._filedata_provider import _FileDataProvider
from fmu.dataio._fmu_provider import _FmuProvider
Expand All @@ -25,6 +26,7 @@
export_file_compute_checksum_md5,
glue_metadata_preprocessed,
read_metadata,
read_named_envvar,
)

logger: Final = logging.getLogger(__name__)
Expand All @@ -46,13 +48,25 @@ def default_meta_dollars() -> dict:


def generate_meta_tracklog() -> list[dict]:
"""Create the tracklog metadata, which here assumes 'created' only."""
meta = []
"""Initialize the tracklog with the 'created' event only."""

dtime = datetime.datetime.now(timezone.utc).isoformat()
user = getpass.getuser()
meta.append({"datetime": dtime, "user": {"id": user}, "event": "created"})
return meta
sysinfo = {
"fmu-dataio": {"version": dataio.__version__},
}
_kmd = read_named_envvar("KOMODO_RELEASE")
if _kmd is not None:
sysinfo["komodo"] = {"version": _kmd}

return [
{
"datetime": dtime,
"user": {"id": user},
"event": "created",
"sysinfo": sysinfo,
}
]


def generate_meta_masterdata(config: dict) -> dict | None:
Expand Down
5 changes: 5 additions & 0 deletions src/fmu/dataio/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ def some_config_from_env(envvar: str = "FMU_GLOBAL_CONFIG") -> dict | None:
return ut.yaml_load(cfg_path, loader="fmu")


def read_named_envvar(envvar: str) -> str | None:
"""Read a specific (named) environment variable."""
return os.environ.get(envvar, None)


def filter_validate_metadata(metadata_in: dict) -> dict:
"""Validate metadatadict at topmost_level and strip away any alien keys."""

Expand Down
16 changes: 15 additions & 1 deletion tests/test_units/test_metadata_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from dateutil.parser import isoparse
from fmu.dataio._metadata import SCHEMA, SOURCE, VERSION, ConfigurationError, _MetaData
from fmu.dataio._utils import prettyprint_dict
from fmu.dataio._utils import prettyprint_dict, read_named_envvar

# pylint: disable=no-member

Expand Down Expand Up @@ -44,6 +44,20 @@ def test_generate_meta_tracklog(edataobj1):
assert "user" in logentry and "id" in logentry["user"]
assert "datetime" in logentry

# sysinfo contains versions of components used, for debugging purposes
assert "sysinfo" in logentry
assert "fmu-dataio" in logentry["sysinfo"]
assert "version" in logentry["sysinfo"]["fmu-dataio"]

_vrs = logentry["sysinfo"]["fmu-dataio"]["version"]
assert _vrs
assert isinstance(_vrs, str)

_kmd = read_named_envvar("KOMODO_RELEASE")
if _kmd: # test shall run only when in a Komodo environment
assert "komodo" in logentry["sysinfo"]
assert logentry["sysinfo"]["komodo"]["version"] == _kmd

# datetime in tracklog shall include time zone offset
assert isoparse(logentry["datetime"]).tzinfo is not None

Expand Down
8 changes: 8 additions & 0 deletions tests/test_units/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test the utils module"""

import os
from pathlib import Path
from tempfile import NamedTemporaryFile

Expand Down Expand Up @@ -184,3 +185,10 @@ def test_generate_description():

with pytest.raises(ValueError):
utils.generate_description(object())


def test_read_named_envvar():
assert utils.read_named_envvar("DONTEXIST") is None

os.environ["MYTESTENV"] = "mytestvalue"
assert utils.read_named_envvar("MYTESTENV") == "mytestvalue"

0 comments on commit c1f8ae6

Please sign in to comment.