Skip to content
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

Better handling of missing package stats #1829

Merged
merged 1 commit into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lambdas/es/indexer/document_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def append(
metadata: str = '',
pointer_file: str = '',
package_hash: str = '',
package_stats: Dict[str, int] = {'total_files': 0, 'total_bytes': 0}.copy(),
package_stats: Dict[str, int] = None,
tags: List[str] = (),
text: str = '',
version_id=None,
Expand Down Expand Up @@ -118,15 +118,25 @@ def append(
if doc_type == DocTypes.PACKAGE:
if not handle or not package_hash or not pointer_file:
raise ValueError("missing required argument for package document")
if (
package_stats
and not isinstance(package_stats, dict)
or isinstance(package_stats, dict)
and any(k not in package_stats for k in ['total_files', 'total_bytes'])
):
raise ValueError("Malformed package_stats")
body.update({
"_id": f"{handle}:{package_hash}",
"handle": handle,
"hash": package_hash,
"metadata": metadata,
"pointer_file": pointer_file,
"package_stats": package_stats,
"tags": ",".join(tags)
})
if package_stats:
body.update({
"package_stats": package_stats,
})
elif doc_type == DocTypes.OBJECT:
body.update({
# Elastic native keys
Expand Down
2 changes: 1 addition & 1 deletion lambdas/es/indexer/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def select_package_stats(s3_client, bucket, manifest_key) -> str:
return stats

except (botocore.exceptions.ClientError, AssertionError, KeyError) as err:
logger_.error("Unable to compute package status via S3 select: %s", err)
logger_.error("Unable to compute package stats via S3 select: %s", err)

return None

Expand Down
20 changes: 20 additions & 0 deletions lambdas/es/indexer/test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ def _make_event(
"last_modified": datetime.datetime(2019, 5, 30, 23, 27, 29, tzinfo=tzutc()),
"pointer_file": "1598026253",
"package_hash": "abc",
"package_stats": None,
}
),
(
Expand Down Expand Up @@ -276,6 +277,25 @@ def _make_event(
"version_id": "abc",
}
),
pytest.param(
"ObjectCreated:Put",
DocTypes.PACKAGE,
{
"bucket": "test",
"etag": "123",
"ext": "",
"handle": "pkg/usr",
"key": "foo",
"last_modified": datetime.datetime(2019, 5, 30, 23, 27, 29, tzinfo=tzutc()),
"pointer_file": "1598026253",
"package_hash": "abc",
"package_stats": {"bad": "data"},
},
marks=pytest.mark.xfail(
raises=ValueError,
reason="Malformed package_stats",
)
),
pytest.param(
"ObjectCreated:Put",
DocTypes.PACKAGE,
Expand Down