-
Notifications
You must be signed in to change notification settings - Fork 1
feat(breadbox): Add optional expiration date to transient datasets #364
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -262,6 +262,7 @@ def upload_dataset( | |
| allowed_values=valid_fields.valid_allowed_values, | ||
| dataset_metadata=dataset_metadata, | ||
| dataset_md5=None, | ||
| expiry=None, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that we are manually setting this field multiple places even though it is already default to None. Is there a reason we need to explicitly set this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's being set to None in the legacy code paths. I've only updated the dataset-v2 endpoint's code path to be aware of expiry.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see what you're saying. On the pydantic model I'd love to leave out the |
||
| ) | ||
|
|
||
| added_dataset = dataset_service.add_matrix_dataset( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import logging | ||
| from collections import defaultdict | ||
| from datetime import datetime, timedelta | ||
| from typing import Any, Dict, Optional, List, Type, Union, Tuple, Set | ||
| from uuid import UUID, uuid4 | ||
| import warnings | ||
|
|
@@ -677,6 +678,35 @@ def update_dataset( | |
| return dataset | ||
|
|
||
|
|
||
| def get_current_datetime(): | ||
| # this method only exists to allow us to mock it in tests. Since `datetime` is a built-in we're not able to | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks adding this informational comment! |
||
| # mutate it. | ||
| return datetime.now() | ||
|
|
||
|
|
||
| def find_expired_datasets(db: SessionWithUser, max_age: timedelta) -> List[Dataset]: | ||
| """ | ||
| Finds transient datasets which can be deleted (because they've "expired") | ||
| Two ways a transient dataset can be expired: | ||
| 1. the `expiry` field can be explictly set, and that time is in the past | ||
| 2. the upload_date is before now - `max_age`. | ||
| """ | ||
|
|
||
| now = get_current_datetime() | ||
| min_upload_date = now - max_age | ||
|
|
||
| expired_datasets = ( | ||
| db.query(Dataset) | ||
| .filter( | ||
| Dataset.is_transient == True, | ||
| or_(Dataset.expiry < now, Dataset.upload_date < min_upload_date,), | ||
| ) | ||
| .all() | ||
| ) | ||
|
|
||
| return expired_datasets | ||
|
|
||
|
|
||
| def delete_dataset( | ||
| db: SessionWithUser, user: str, dataset: Dataset, filestore_location: str | ||
| ): | ||
|
|
||
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.
I personally prefer adding these types of validations within the pydantic model itself since it feels a bit cleaner and will catch malformed request body faster. In addition, this prevents us from needing to do asserts or similar checks later on which I notice you do later on in
breadbox/breadbox/compute/dataset_uploads_tasks.py:87There 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.
I'm less a fan of validations within the pydanic model ... but only for reasons that I'd say more "habit" then anything.
I agree, this is a validation which would make perfect sense to include in the pydantic model. I'll fix.