Skip to content

Commit

Permalink
[Feature] DRC-935 Auto-approved the preset check if we know the result
Browse files Browse the repository at this point in the history
- For `SCHEMA_DIFF` and `ROW_COUNT_DIFF` we will mark as approved if we
  know the result don't have any changes

Signed-off-by: Kent Huang <[email protected]>
  • Loading branch information
kentwelcome committed Dec 5, 2024
1 parent a4656a9 commit 2316452
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
12 changes: 8 additions & 4 deletions recce/apis/check_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def _generate_check_name(check_type, params, view_options):
return f"{'check'.capitalize()} - {now}"


def create_check_from_run(run_id, check_name=None, check_description='', check_view_options=None, is_preset=False):
def create_check_from_run(run_id, check_name=None, check_description='', check_view_options=None, is_preset=False,
is_checked=False):
if run_id is None:
raise ValueError('run_id is required')

Expand All @@ -80,21 +81,24 @@ def create_check_from_run(run_id, check_name=None, check_description='', check_v
type=run_type,
params=run_params,
view_options=check_view_options,
is_preset=is_preset)
is_preset=is_preset,
is_checked=is_checked)
CheckDAO().create(check)
run.check_id = check.check_id

return check


def create_check_without_run(check_name, check_description, check_type, params, check_view_options, is_preset=False):
def create_check_without_run(check_name, check_description, check_type, params, check_view_options, is_preset=False,
is_checked=False):
name = check_name if check_name is not None else _generate_check_name(check_type, params, check_view_options)
check = Check(name=name,
description=check_description,
type=check_type,
params=params,
view_options=check_view_options,
is_preset=is_preset)
is_preset=is_preset,
is_checked=is_checked)
CheckDAO().create(check)
return check

Expand Down
51 changes: 48 additions & 3 deletions recce/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
from datetime import datetime, timezone
from typing import List

from deepdiff import DeepDiff
from rich import box
from rich.console import Console
from rich.table import Table

from recce.apis.check_func import create_check_from_run, create_check_without_run, purge_preset_checks
from recce.apis.run_func import submit_run
from recce.config import RecceConfig
from recce.core import default_context
from recce.models.types import RunType
from recce.summary import generate_markdown_summary

Expand All @@ -36,16 +38,56 @@ def load_preset_checks(checks: list):
table.add_column('Type')
table.add_column('Description')
for check in checks:
is_check = False
name = check.get('name')
description = check.get('description', '')
check_type = check.get('type')
check_params = check.get('params', {})
check_options = check.get('view_options', {})
create_check_without_run(name, description, check_type, check_params, check_options, is_preset=True)

if check_type == RunType.SCHEMA_DIFF.value:
is_check = schema_diff_should_be_approved(check_params)

create_check_without_run(name, description, check_type, check_params, check_options, is_preset=True,
is_checked=is_check)
table.add_row(name, check_type.replace('_', ' ').title(), description.strip())
console.print(table)


def schema_diff_should_be_approved(check_params: dict) -> bool:
context = default_context()
nodes = context.adapter.select_nodes(
select=check_params.get('select'),
exclude=check_params.get('exclude'),
packages=check_params.get('packages'),
view_mode=check_params.get('view_mode'),
)
nodes = [node for node in nodes if not node.startswith('test.')]
for node in nodes:
base = context.get_model(node, base=True)
curr = context.get_model(node, base=False)
diff = DeepDiff(base, curr, ignore_order=True)

# If the diff is empty, then the check should be approved
if bool(diff) is False:
return True

return False


def run_should_be_approved(run):
if run.type == RunType.ROW_COUNT_DIFF:
# If the run has an error, then the check should not be approved
if run.error is not None:
return False
# If the row count are exactly the same, then the check should be approved
for column, row_count_result in run.result.items():
if row_count_result['base'] != row_count_result['curr']:
return False
return True
return False


async def execute_preset_checks(preset_checks: list) -> (int, List[dict]):
"""
Execute the preset checks
Expand Down Expand Up @@ -79,12 +121,15 @@ async def execute_preset_checks(preset_checks: list) -> (int, List[dict]):

start = time.time()
if check_type in ['schema_diff']:
is_check = schema_diff_should_be_approved(check_params)
create_check_without_run(check_name, check_description, check_type, check_params, check_options,
is_preset=True)
is_preset=True, is_checked=is_check)
else:
run, future = submit_run(check_type, params=check_params)
await future
create_check_from_run(run.run_id, check_name, check_description, check_options, is_preset=True)
is_check = run_should_be_approved(run)
create_check_from_run(run.run_id, check_name, check_description, check_options, is_preset=True,
is_checked=is_check)

end = time.time()
table.add_row('[[green]Success[/green]]', check_name, check_type.replace('_', ' ').title(),
Expand Down

0 comments on commit 2316452

Please sign in to comment.