-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: use group-name/export-time from log file if present #306
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,11 @@ | ||
"""Support for talking to FHIR servers & handling the FHIR spec""" | ||
|
||
from .fhir_client import FhirClient, create_fhir_client_for_cli | ||
from .fhir_utils import download_reference, get_docref_note, parse_datetime, ref_resource, unref_resource | ||
from .fhir_utils import ( | ||
download_reference, | ||
get_docref_note, | ||
parse_datetime, | ||
parse_group_from_url, | ||
ref_resource, | ||
unref_resource, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
""" | ||
Parsing for bulk export log files | ||
|
||
https://github.com/smart-on-fhir/bulk-data-client/wiki/Bulk-Data-Export-Log-Items | ||
""" | ||
|
||
import datetime | ||
import json | ||
import os | ||
import re | ||
|
||
from cumulus_etl import common, fhir, store | ||
|
||
|
||
class BulkExportLogParser: | ||
""" | ||
Parses the log file generated by bulk exports. | ||
|
||
These are the assumptions we make: | ||
- There will be a log.ndjson or log.*.ndjson file in the given folder. | ||
- There cannot be multiples (unless log.ndjson exists, in which case we always use that) | ||
- That log file will be for a single export. | ||
- e.g. We will generally grab the last "kickoff" event and ignore others. | ||
""" | ||
|
||
class LogParsingError(Exception): | ||
pass | ||
|
||
class IncompleteLog(LogParsingError): | ||
pass | ||
|
||
class MultipleLogs(LogParsingError): | ||
pass | ||
|
||
class NoLogs(LogParsingError): | ||
pass | ||
|
||
def __init__(self, root: store.Root): | ||
self.group_name: str = None | ||
self.export_datetime: datetime.datetime = None | ||
|
||
self._parse(self._find(root)) | ||
|
||
def _parse(self, path: str) -> None: | ||
# Go through every row, looking for the events we care about. | ||
# Note that we parse every kickoff event we hit, for example. | ||
# So we'll end up with the latest one (which works for single-export | ||
# log files with maybe a false start at the beginning). | ||
try: | ||
for row in common.read_ndjson(path): | ||
match row.get("eventId"): | ||
case "kickoff": | ||
self._parse_kickoff(row) | ||
case "status_complete": | ||
self._parse_status_complete(row) | ||
except (KeyError, json.JSONDecodeError) as exc: | ||
raise self.IncompleteLog(f"Error parsing '{path}'") from exc | ||
|
||
if self.group_name is None: | ||
raise self.IncompleteLog(f"No kickoff event found in '{path}'") | ||
if self.export_datetime is None: | ||
raise self.IncompleteLog(f"No status_complete event found in '{path}'") | ||
|
||
def _parse_kickoff(self, row: dict) -> None: | ||
details = row["eventDetail"] | ||
self.group_name = fhir.parse_group_from_url(details["exportUrl"]) | ||
|
||
def _parse_status_complete(self, row: dict) -> None: | ||
details = row["eventDetail"] | ||
self.export_datetime = datetime.datetime.fromisoformat(details["transactionTime"]) | ||
|
||
def _find(self, root: store.Root) -> str: | ||
"""Finds the log file inside the root""" | ||
try: | ||
paths = root.ls() | ||
except FileNotFoundError as exc: | ||
raise self.NoLogs("Folder does not exist") from exc | ||
filenames = {os.path.basename(p): p for p in paths} | ||
|
||
# In the easy case, it's just sitting there at log.ndjson, | ||
# which is the filename that bulk-data-client uses. | ||
# Because this is the standard name, we prefer this and don't | ||
# error out even if there are other log.something.ndjson names in | ||
# the folder (see below). Maybe this is a symlink to the most recent... | ||
if full_path := filenames.get("log.ndjson"): | ||
return full_path | ||
|
||
# But possibly the user does some file renaming to manage different | ||
# exports, so allow log.something.ndjson as well. (Much like we do | ||
# for the input ndjson files.) | ||
pattern = re.compile(r"log\..+\.ndjson") | ||
log_files = list(filter(pattern.match, filenames.keys())) | ||
match len(log_files): | ||
case 0: | ||
raise self.NoLogs("No log.ndjson file found") | ||
case 1: | ||
return filenames[log_files[0]] | ||
case _: | ||
raise self.MultipleLogs("Multiple log.*.ndjson files found") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
"""Tests for bulk export log parsing""" | ||
|
||
import datetime | ||
import tempfile | ||
|
||
import ddt | ||
|
||
from cumulus_etl import common, store | ||
from cumulus_etl.loaders.fhir.export_log import BulkExportLogParser | ||
from tests.utils import AsyncTestCase | ||
|
||
|
||
def kickoff(group: str) -> dict: | ||
url = f"https://host/Group/{group}" if group else "https://host/" | ||
return { | ||
"eventId": "kickoff", | ||
"eventDetail": { | ||
"exportUrl": url, | ||
}, | ||
} | ||
|
||
|
||
def status_complete(timestamp: str) -> dict: | ||
return { | ||
"eventId": "status_complete", | ||
"eventDetail": { | ||
"transactionTime": timestamp, | ||
}, | ||
} | ||
|
||
|
||
@ddt.ddt | ||
class TestBulkExportLogParser(AsyncTestCase): | ||
"""Test case for parsing bulk export logs.""" | ||
|
||
def _assert_results(self, path, expected_result) -> None: | ||
if isinstance(expected_result, tuple): | ||
parser = BulkExportLogParser(store.Root(path)) | ||
expected_group = expected_result[0] | ||
expected_datetime = datetime.datetime.fromisoformat(expected_result[1]) | ||
self.assertEqual(expected_group, parser.group_name) | ||
self.assertEqual(expected_datetime, parser.export_datetime) | ||
else: | ||
with self.assertRaises(expected_result): | ||
BulkExportLogParser(store.Root(path)) | ||
|
||
@ddt.data( | ||
# Happy cases: | ||
(["log.ndjson"], None), | ||
(["log.blarg.ndjson"], None), | ||
(["log.0001.ndjson"], None), | ||
(["log.ndjson", "log.1.ndjson"], None), | ||
# Error cases: | ||
([], BulkExportLogParser.NoLogs), | ||
(["log.1.ndjson", "log.2.ndjson"], BulkExportLogParser.MultipleLogs), | ||
) | ||
@ddt.unpack | ||
def test_finding_the_log(self, files, error): | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
common.write_text(f"{tmpdir}/distraction.txt", "hello") | ||
common.write_text(f"{tmpdir}/log.ndjson.bak", "bye") | ||
for file in files: | ||
with common.NdjsonWriter(f"{tmpdir}/{file}") as writer: | ||
writer.write(kickoff("G")) | ||
writer.write(status_complete("2020-10-17")) | ||
|
||
error = error or ("G", "2020-10-17") | ||
self._assert_results(tmpdir, error) | ||
|
||
def test_no_dir(self): | ||
self._assert_results("/path/does/not/exist", BulkExportLogParser.NoLogs) | ||
|
||
@ddt.data( | ||
# Happy cases: | ||
( # basic simple case | ||
[kickoff("G"), status_complete("2020-10-17")], | ||
("G", "2020-10-17"), | ||
), | ||
( # multiple rows - we should pick last of each | ||
[ | ||
kickoff("1st"), | ||
kickoff("2nd"), | ||
status_complete("2001-01-01"), | ||
status_complete("2002-02-02"), | ||
], | ||
("2nd", "2002-02-02"), | ||
), | ||
([kickoff(""), status_complete("2020-10-17")], ("", "2020-10-17")), # global export group | ||
# Error cases: | ||
([status_complete("2010-03-09")], BulkExportLogParser.IncompleteLog), # missing group | ||
([kickoff("G")], BulkExportLogParser.IncompleteLog), # missing time | ||
([], BulkExportLogParser.IncompleteLog), # missing all | ||
([{"eventId": "kickoff"}], BulkExportLogParser.IncompleteLog), # missing eventDetail | ||
( # missing transactionTime | ||
[{"eventId": "status_complete", "eventDetail": {}}], | ||
BulkExportLogParser.IncompleteLog, | ||
), | ||
) | ||
@ddt.unpack | ||
def test_parsing(self, rows, expected_result): | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
with common.NdjsonWriter(f"{tmpdir}/log.ndjson", allow_empty=True) as writer: | ||
for row in rows: | ||
writer.write(row) | ||
|
||
self._assert_results(tmpdir, expected_result) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: consider a MissingLog error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah that actually does exist (I used
NoLogs
), in thefind
method below (along withMultipleLogs
).Currently nothing actually distinguishes between these flavors of error. Maybe they will in the future, but 🤷