-
Notifications
You must be signed in to change notification settings - Fork 144
[WIP] feat: Add counter-based validations and tests #1977
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 all commits
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 | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -40,7 +40,8 @@ class ValidationRunner: | |||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| def __init__(self, validation_config_path: str, differ_output: str, | ||||||||||||||||||||||||||||||||||||||||||||||
| stats_summary: str, lint_report: str, validation_output: str): | ||||||||||||||||||||||||||||||||||||||||||||||
| stats_summary: str, lint_report: str, validation_output: str, | ||||||||||||||||||||||||||||||||||||||||||||||
| counters_report: str = None): | ||||||||||||||||||||||||||||||||||||||||||||||
| self.config = ValidationConfig(validation_config_path) | ||||||||||||||||||||||||||||||||||||||||||||||
| self.validation_output = validation_output | ||||||||||||||||||||||||||||||||||||||||||||||
| self.validator = Validator() | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -49,7 +50,8 @@ def __init__(self, validation_config_path: str, differ_output: str, | |||||||||||||||||||||||||||||||||||||||||||||
| 'stats': pd.DataFrame(), | ||||||||||||||||||||||||||||||||||||||||||||||
| 'differ': pd.DataFrame(), | ||||||||||||||||||||||||||||||||||||||||||||||
| 'differ_summary': {}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 'lint': {} | ||||||||||||||||||||||||||||||||||||||||||||||
| 'lint': {}, | ||||||||||||||||||||||||||||||||||||||||||||||
| 'counters': {} | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| self.validation_dispatch = { | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -85,12 +87,22 @@ def __init__(self, validation_config_path: str, differ_output: str, | |||||||||||||||||||||||||||||||||||||||||||||
| 'MAX_VALUE_CHECK': | ||||||||||||||||||||||||||||||||||||||||||||||
| (self.validator.validate_max_value_check, 'stats'), | ||||||||||||||||||||||||||||||||||||||||||||||
| 'GOLDENS_CHECK': (self.validator.validate_goldens, 'stats'), | ||||||||||||||||||||||||||||||||||||||||||||||
| 'COUNTER_ZERO_CHECK': | ||||||||||||||||||||||||||||||||||||||||||||||
| (self.validator.validate_counter_zero, 'counters'), | ||||||||||||||||||||||||||||||||||||||||||||||
| 'COUNTER_MAX_THRESHOLD': | ||||||||||||||||||||||||||||||||||||||||||||||
| (self.validator.validate_counter_max_threshold, 'counters'), | ||||||||||||||||||||||||||||||||||||||||||||||
| 'COUNTER_RATIO_THRESHOLD': | ||||||||||||||||||||||||||||||||||||||||||||||
| (self.validator.validate_counter_ratio_threshold, 'counters'), | ||||||||||||||||||||||||||||||||||||||||||||||
| 'COUNTER_SUM_INTEGRITY': | ||||||||||||||||||||||||||||||||||||||||||||||
| (self.validator.validate_counter_sum_integrity, 'counters'), | ||||||||||||||||||||||||||||||||||||||||||||||
| 'COUNTER_MIN_YIELD': | ||||||||||||||||||||||||||||||||||||||||||||||
| (self.validator.validate_counter_min_yield, 'counters'), | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| self._initialize_data_sources(stats_summary, lint_report, differ_output) | ||||||||||||||||||||||||||||||||||||||||||||||
| self._initialize_data_sources(stats_summary, lint_report, differ_output, counters_report) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| def _initialize_data_sources(self, stats_summary: str, lint_report: str, | ||||||||||||||||||||||||||||||||||||||||||||||
| differ_output: str): | ||||||||||||||||||||||||||||||||||||||||||||||
| differ_output: str, counters_report: str = None): | ||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||
| Checks for and loads the required data sources based on the config. | ||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -167,6 +179,37 @@ def _initialize_data_sources(self, stats_summary: str, lint_report: str, | |||||||||||||||||||||||||||||||||||||||||||||
| logging.warning("lint_report file exists but is empty: %s", | ||||||||||||||||||||||||||||||||||||||||||||||
| lint_report) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| if counters_report: | ||||||||||||||||||||||||||||||||||||||||||||||
| self._load_counters(counters_report) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| def _load_counters(self, counters_report: str): | ||||||||||||||||||||||||||||||||||||||||||||||
| """Loads counters from a CSV file and stores them in data_sources.""" | ||||||||||||||||||||||||||||||||||||||||||||||
| if os.path.exists(counters_report) and os.path.getsize( | ||||||||||||||||||||||||||||||||||||||||||||||
| counters_report) > 0: | ||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||
| df = pd.read_csv(counters_report) | ||||||||||||||||||||||||||||||||||||||||||||||
| def clean_key(x): | ||||||||||||||||||||||||||||||||||||||||||||||
| if not isinstance(x, str): | ||||||||||||||||||||||||||||||||||||||||||||||
| return x | ||||||||||||||||||||||||||||||||||||||||||||||
| if ':' in x: | ||||||||||||||||||||||||||||||||||||||||||||||
| x = x.split(':', 1)[1] | ||||||||||||||||||||||||||||||||||||||||||||||
| if '_' in x: | ||||||||||||||||||||||||||||||||||||||||||||||
| x = x.rsplit('_', 1)[-1] | ||||||||||||||||||||||||||||||||||||||||||||||
| return x | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| df['key'] = df['key'].apply(clean_key) | ||||||||||||||||||||||||||||||||||||||||||||||
| # Aggregate by summing if there are duplicates | ||||||||||||||||||||||||||||||||||||||||||||||
| df = df.groupby('key')['value'].sum().reset_index() | ||||||||||||||||||||||||||||||||||||||||||||||
| self.data_sources['counters'] = dict( | ||||||||||||||||||||||||||||||||||||||||||||||
| zip(df['key'], df['value'])) | ||||||||||||||||||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||||||
| logging.error( | ||||||||||||||||||||||||||||||||||||||||||||||
| f"CSV parse error while reading counters report at {counters_report}: {e}" | ||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+189
to
+208
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. It is recommended to raise an exception if the counters report fails to parse, rather than just logging an error. Continuing with an empty
Suggested change
References
Comment on lines
+205
to
+208
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. Avoid catching broad exceptions. It is better to catch specific errors that
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| elif os.path.exists(counters_report): | ||||||||||||||||||||||||||||||||||||||||||||||
| logging.warning("counters_report file exists but is empty: %s", | ||||||||||||||||||||||||||||||||||||||||||||||
| counters_report) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| def _determine_required_sources(self) -> set[str]: | ||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||
| Parses the validation config to determine which data sources are required. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -265,7 +308,8 @@ def main(_): | |||||||||||||||||||||||||||||||||||||||||||||
| differ_output=_FLAGS.differ_output, | ||||||||||||||||||||||||||||||||||||||||||||||
| stats_summary=_FLAGS.stats_summary, | ||||||||||||||||||||||||||||||||||||||||||||||
| lint_report=_FLAGS.lint_report, | ||||||||||||||||||||||||||||||||||||||||||||||
| validation_output=_FLAGS.validation_output) | ||||||||||||||||||||||||||||||||||||||||||||||
| validation_output=_FLAGS.validation_output, | ||||||||||||||||||||||||||||||||||||||||||||||
| counters_report=_FLAGS.counters_report) | ||||||||||||||||||||||||||||||||||||||||||||||
| overall_status, _ = runner.run_validations() | ||||||||||||||||||||||||||||||||||||||||||||||
| if not overall_status: | ||||||||||||||||||||||||||||||||||||||||||||||
| sys.exit(1) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -283,6 +327,8 @@ def main(_): | |||||||||||||||||||||||||||||||||||||||||||||
| 'Path to the stats summary report file.') | ||||||||||||||||||||||||||||||||||||||||||||||
| flags.DEFINE_string('lint_report', None, | ||||||||||||||||||||||||||||||||||||||||||||||
| 'Path to the mcf lint report file.') | ||||||||||||||||||||||||||||||||||||||||||||||
| flags.DEFINE_string('counters_report', None, | ||||||||||||||||||||||||||||||||||||||||||||||
| 'Path to the counters report file.') | ||||||||||||||||||||||||||||||||||||||||||||||
| flags.DEFINE_string('validation_output', None, | ||||||||||||||||||||||||||||||||||||||||||||||
| 'Path to the validation output file.') | ||||||||||||||||||||||||||||||||||||||||||||||
| flags.mark_flag_as_required('validation_output') | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
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.
The
clean_keylogic is too aggressive and will break counter names that contain underscores. For example, a counter namedrecords_count(following the recommended naming convention) would be incorrectly truncated tocount. Additionally,rsplit('_', 1)only works if the counter name itself has no underscores. A more robust approach would be to use a regular expression to strip the specific stage prefix pattern (e.g.,digit:string_).References