Skip to content

Commit

Permalink
[Feature] Change to use RecceStateLoader to export file
Browse files Browse the repository at this point in the history
Signed-off-by: Kent Huang <[email protected]>
  • Loading branch information
kentwelcome committed Jun 12, 2024
1 parent 7351e80 commit 4e3223a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
16 changes: 14 additions & 2 deletions recce/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def server(host, port, state_file=None, **kwargs):
@add_options(dbt_related_options)
@add_options(sqlmesh_related_options)
@add_options(recce_options)
@add_options(recce_cloud_options)
def run(output, **kwargs):
is_github_action, pr_url = check_github_ci_env(**kwargs)
if is_github_action is True and pr_url is not None:
Expand All @@ -217,7 +218,16 @@ def run(output, **kwargs):
# Initialize Recce Config
RecceConfig(config_file=kwargs.get('config'))

return asyncio.run(cli_run(output, **kwargs))
cloud_mode = kwargs.get('cloud', False)
state_file = kwargs.get('state_file')
cloud_options = {
'host': kwargs.get('state_file_host'),
'secret': kwargs.get('state_file_secret'),
} if cloud_mode else None
recce_state = RecceStateLoader(review_mode=False, cloud_mode=cloud_mode,
state_file=state_file, cloud_options=cloud_options)

return asyncio.run(cli_run(output, recce_state=recce_state, **kwargs))


@cli.command(cls=TrackCommand)
Expand All @@ -229,9 +239,11 @@ def summary(state_file, **kwargs):
from rich.console import Console
from .core import load_context
console = Console()
recce_state = RecceStateLoader(review_mode=False, cloud_mode=False,
state_file=state_file, cloud_options=None)
try:
# Load context in review mode, won't need to check dbt_project.yml file.
ctx = load_context(**kwargs, state_file=state_file, review=True)
ctx = load_context(**kwargs, recce_state=recce_state, review=True)
except Exception as e:
console.print("[[red]Error[/red]] Failed to generate summary:")
console.print(f"{e}")
Expand Down
11 changes: 9 additions & 2 deletions recce/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from recce.core import RecceContext
from recce.models.types import RunType
from recce.pull_request import fetch_pr_metadata_from_event_path
from recce.state import RecceState, PullRequestInfo
from recce.state import RecceState, PullRequestInfo, RecceStateLoader


def check_github_ci_env(**kwargs):
Expand Down Expand Up @@ -253,6 +253,13 @@ async def cli_run(output_state_file: str, **kwargs):
state: RecceState = ctx.export_state()
state.pull_request = fetch_pr_metadata(**kwargs)

state.to_state_file(output_state_file)
cloud_mode = kwargs.get('cloud', False)
cloud_options = {
'host': kwargs.get('state_file_host'),
'secret': kwargs.get('state_file_secret'),
} if cloud_mode else None
recce_state = RecceStateLoader(review_mode=False, cloud_mode=cloud_mode, state_file=output_state_file,
cloud_options=cloud_options)
recce_state.export(state)
print(f'The state file is stored at [{output_state_file}]')
return rc
24 changes: 18 additions & 6 deletions recce/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def __init__(self,

# Load the state
self.load()

pass

def verify(self) -> bool:
Expand All @@ -178,11 +179,14 @@ def update(self, state: RecceState):
self.state = state

def load(self) -> RecceState:
if self.cloud_mode:
# TODO: Load the state from cloud storage
try:
if self.cloud_mode:
# TODO: Load the state from cloud storage
pass
else:
self.state = RecceState.from_file(self.state_file)
except Exception:
pass
else:
self.state = RecceState.from_file(self.state_file)
return self.state

def export(self, state: RecceState = None):
Expand All @@ -194,11 +198,19 @@ def export(self, state: RecceState = None):
else:
self._export_state_to_file()

def _export_state_to_file(self):
def _export_state_to_cloud(self):
# TODO: export the state to remote cloud storage
print('TODO: export the state to remote cloud storage')
print(f" remote host: {self.cloud_options.get('host')}")
pass

def _export_state_to_cloud(self):
def _export_state_to_recce_cloud(self):
pass

def _export_state_to_s3_bucket(self):
pass

def _export_state_to_file(self):
"""
Store the state to a file. Store happens when terminating the server or run instance.
"""
Expand Down

0 comments on commit 4e3223a

Please sign in to comment.