-
Notifications
You must be signed in to change notification settings - Fork 82
feat(Presto): Generate split filter file from set-up-config.sh
command
#1170
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
Open
quinntaylormitchell
wants to merge
14
commits into
y-scope:main
Choose a base branch
from
quinntaylormitchell:presto-generate-metadata-filter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c599ded
Generate metadata filter file from set-up-config.sh
quinntaylormitchell e16f4ff
Rabbit
quinntaylormitchell 597d60b
Address some of Haiqi's comments
quinntaylormitchell 5a7460f
Merge branch 'main' into feature branch
quinntaylormitchell 14f18a1
Address the rest of Haiqi's comments
quinntaylormitchell 33ba92c
Address Rabbit comments
quinntaylormitchell 7df5135
Mark BOLD and RESET as constants
quinntaylormitchell 5ef6d95
Address Haiqi's comments
quinntaylormitchell d404b7d
Revert config file back to default (mistakenly changed in most recent…
quinntaylormitchell 7a3fc6d
Add newline to user warning
quinntaylormitchell f2a84b5
Merge branch 'main' into feature branch
quinntaylormitchell 906a2d8
Address Haiqi's comments
quinntaylormitchell 7bf80b3
Rabbit
quinntaylormitchell 985a7e5
Merge branch 'main' into feature branch
quinntaylormitchell 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
197 changes: 197 additions & 0 deletions
197
tools/deployment/presto-clp/scripts/generate-split-filter-file.py
This file contains hidden or 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,197 @@ | ||
import argparse | ||
import json | ||
import logging | ||
import sys | ||
from pathlib import Path | ||
from typing import Dict, Final, List, Optional, TypedDict | ||
quinntaylormitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# CONSTANTS | ||
ANSI_BOLD: Final[str] = "\033[1m" | ||
ANSI_RESET: Final[str] = "\033[0m" | ||
DEFAULT_TIMESTAMP_KEY: Final[str] = "timestamp" | ||
DEFAULT_REQUIRED: Final[bool] = False | ||
|
||
# Set up console logging | ||
logging_console_handler = logging.StreamHandler() | ||
logging_formatter = logging.Formatter( | ||
"%(asctime)s.%(msecs)03d %(levelname)s [%(module)s] %(message)s", datefmt="%Y-%m-%dT%H:%M:%S" | ||
) | ||
logging_console_handler.setFormatter(logging_formatter) | ||
|
||
# Set up root logger | ||
root_logger = logging.getLogger() | ||
root_logger.setLevel(logging.INFO) | ||
root_logger.addHandler(logging_console_handler) | ||
|
||
# Create logger | ||
logger = logging.getLogger(__name__) | ||
|
||
quinntaylormitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class RangeMapping(TypedDict): | ||
lowerBound: str | ||
upperBound: str | ||
|
||
|
||
class CustomOptions(TypedDict): | ||
rangeMapping: RangeMapping | ||
|
||
|
||
class SplitFilterRule(TypedDict): | ||
columnName: str | ||
customOptions: CustomOptions | ||
required: bool | ||
|
||
|
||
SplitFilterDict = Dict[str, List[SplitFilterRule]] | ||
DEFAULT_RANGE_MAPPING: Final[RangeMapping] = { | ||
"lowerBound": "begin_timestamp", | ||
"upperBound": "end_timestamp", | ||
} | ||
DEFAULT_CUSTOM_OPTIONS: Final[CustomOptions] = {"rangeMapping": DEFAULT_RANGE_MAPPING} | ||
|
||
quinntaylormitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def validate_dir(path: Path, label: str) -> bool: | ||
""" | ||
Determines whether or not the path exists and whether or not it is a directory. If either of | ||
those are false, logs error message with path and returns False. | ||
|
||
:param path: | ||
:param label: | ||
:param logger: | ||
:return: True if path exists and is a directory; False if either of those are not true. | ||
""" | ||
if not path.exists(): | ||
logger.error("%s directory does not exist: %s", label, path) | ||
return False | ||
if not path.is_dir(): | ||
logger.error("%s path is not a directory: %s", label, path) | ||
return False | ||
return True | ||
|
||
|
||
def main(argv=None) -> int: | ||
if argv is None: | ||
argv = sys.argv | ||
|
||
args_parser = argparse.ArgumentParser( | ||
quinntaylormitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
description="Generates a split filter file for all user-configured datasets." | ||
) | ||
args_parser.add_argument( | ||
"--clp-package-dir", help="CLP package directory.", required=True, type=Path | ||
) | ||
args_parser.add_argument( | ||
"--output-file", help="Path for the split filter file.", required=True, type=Path | ||
) | ||
|
||
quinntaylormitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parsed_args = args_parser.parse_args(argv[1:]) | ||
quinntaylormitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
clp_package_dir: Path = parsed_args.clp_package_dir.resolve() | ||
archives_dir = clp_package_dir / "var" / "data" / "archives" | ||
json_output_file: Path = parsed_args.output_file.resolve() | ||
out_dir = json_output_file.parent | ||
|
||
if not validate_dir(archives_dir, "Archives"): | ||
return 1 | ||
if not validate_dir(out_dir, "Output"): | ||
return 1 | ||
|
||
if json_output_file.exists() and json_output_file.is_dir(): | ||
logger.error("Output path is a directory: %s", json_output_file) | ||
return 1 | ||
|
||
datasets = _get_dataset_names(archives_dir) | ||
if datasets == None: | ||
logger.error("No datasets found in %s. Did you compress any logs yet?", archives_dir) | ||
return 1 | ||
|
||
try: | ||
timestamp_keys_by_dataset = _prompt_timestamp_keys(datasets) | ||
except KeyboardInterrupt: | ||
logger.error("Interrupted while collecting timestamp keys.") | ||
return 1 | ||
|
||
quinntaylormitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
split_filters = _construct_split_filters(datasets, timestamp_keys_by_dataset) | ||
if split_filters is None: | ||
logger.error("Missing timestamp key(s) for dataset(s).") | ||
return 1 | ||
|
||
try: | ||
with open(json_output_file, "w") as json_file: | ||
json.dump(split_filters, json_file, indent=2) | ||
except OSError as e: | ||
logger.error("Failed to write output file %s: %s", json_output_file, e) | ||
return 1 | ||
|
||
quinntaylormitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return 0 | ||
|
||
|
||
def _get_dataset_names(archives_dir: Path) -> Optional[List[str]]: | ||
""" | ||
Return the names of first-level subdirectories in <clp-package-dir>/var/data/archives. Each | ||
subdirectory name is treated as a dataset name. | ||
|
||
:param archives_dir: | ||
:return: List of dataset names. None if there are no directories within | ||
<clp-package-dir>/var/data/archives. | ||
""" | ||
|
||
datasets = sorted([p.name for p in archives_dir.iterdir() if p.is_dir()]) | ||
return datasets if len(datasets) >= 1 else None | ||
|
||
|
||
def _prompt_timestamp_keys(datasets: List[str]) -> Dict[str, str]: | ||
""" | ||
Prompt the user to provide the timestamp key for each dataset. If the user doesn't provide one, | ||
falls back to `DEFAULT_TIMESTAMP_KEY`. | ||
|
||
:param datasets: | ||
:return: mapping of `dataset` -> `timestamp_keys`. | ||
""" | ||
print( | ||
"\nPlease enter the timestamp key that corresponds to each of your archived datasets." | ||
"\nPress <Enter> to accept the default key.\n" | ||
) | ||
|
||
data_time_pairs: Dict[str, str] = {} | ||
for dataset in datasets: | ||
user_input = input( | ||
f">>> {ANSI_BOLD}{dataset}{ANSI_RESET} [default key: {ANSI_BOLD}{DEFAULT_TIMESTAMP_KEY}{ANSI_RESET}]: " | ||
) | ||
key = DEFAULT_TIMESTAMP_KEY if 0 == len(user_input) else user_input | ||
quinntaylormitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data_time_pairs[dataset] = key | ||
|
||
return data_time_pairs | ||
quinntaylormitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def _construct_split_filters( | ||
datasets: List[str], | ||
timestamp_keys: Dict[str, str], | ||
) -> Optional[SplitFilterDict]: | ||
""" | ||
Constructs a split filter for each dataset using a per-dataset timestamp key. | ||
|
||
:param datasets: | ||
:param timestamp_keys: Mapping from dataset name -> timestamp key. | ||
:return: A SplitFilterDict containing all the SplitFilterRule objects for the JSON file. | ||
:return: A `SplitFilterDict` containing all the `SplitFilterRule` objects for the JSON file, or | ||
None if there are any datasets that don't have an associated timestamp key. | ||
""" | ||
|
||
missing = [d for d in datasets if d not in timestamp_keys] | ||
if len(missing) != 0: | ||
logger.error("Missing timestamp key(s) for dataset(s): %s", ", ".join(missing)) | ||
return None | ||
|
||
split_filters: SplitFilterDict = {} | ||
for dataset in datasets: | ||
rule: SplitFilterRule = { | ||
"columnName": timestamp_keys[dataset], | ||
"customOptions": DEFAULT_CUSTOM_OPTIONS, | ||
"required": DEFAULT_REQUIRED, | ||
} | ||
quinntaylormitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
split_filters[f"clp.default.{dataset}"] = [rule] | ||
quinntaylormitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return split_filters | ||
quinntaylormitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
if "__main__" == __name__: | ||
sys.exit(main(sys.argv)) | ||
quinntaylormitchell marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.