-
Notifications
You must be signed in to change notification settings - Fork 13
refactor: Function to get execution status stats #95
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
yuechao-qin
wants to merge
1
commit into
master
Choose a base branch
from
ycq/function_get_execution_status_stats
base: master
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
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
There are no files selected for viewing
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
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 |
|---|---|---|
| @@ -1,5 +1,50 @@ | ||
| from sqlalchemy import orm | ||
|
|
||
| from cloud_pipelines_backend import backend_types_sql as bts | ||
| from cloud_pipelines_backend.api_server_sql import ExecutionStatusSummary | ||
| from cloud_pipelines_backend import database_ops | ||
| from cloud_pipelines_backend.api_server_sql import ( | ||
| ExecutionStatusSummary, | ||
| PipelineRunsApiService_Sql, | ||
| ) | ||
|
|
||
|
|
||
| def _initialize_db_and_get_session_factory(): | ||
| db_engine = database_ops.create_db_engine_and_migrate_db(database_uri="sqlite://") | ||
| return lambda: orm.Session(bind=db_engine) | ||
|
|
||
|
|
||
| def _create_execution_node(session, task_spec=None, status=None, parent=None): | ||
| """Helper to create an ExecutionNode with optional status and parent.""" | ||
| node = bts.ExecutionNode(task_spec=task_spec or {}) | ||
| if parent is not None: | ||
| node.parent_execution = parent | ||
| if status is not None: | ||
| node.container_execution_status = status | ||
| session.add(node) | ||
| session.flush() | ||
| return node | ||
|
|
||
|
|
||
| def _link_ancestor(session, execution_node, ancestor_node): | ||
| """Create an ExecutionToAncestorExecutionLink.""" | ||
| link = bts.ExecutionToAncestorExecutionLink( | ||
| ancestor_execution=ancestor_node, | ||
| execution=execution_node, | ||
| ) | ||
| session.add(link) | ||
| session.flush() | ||
|
|
||
|
|
||
| def _create_pipeline_run(session, root_execution, created_by=None, annotations=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. Let's use Python type annotations everywhere in function signatures. |
||
| """Helper to create a PipelineRun linked to a root execution node.""" | ||
| run = bts.PipelineRun(root_execution=root_execution) | ||
| if created_by: | ||
|
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. This conditionals are not needed. You can just construct the PipelineRun object. |
||
| run.created_by = created_by | ||
| if annotations: | ||
| run.annotations = annotations | ||
| session.add(run) | ||
| session.flush() | ||
| return run | ||
|
|
||
|
|
||
| class TestExecutionStatusSummary: | ||
|
|
@@ -55,3 +100,59 @@ def test_accumulate_all_statuses(self): | |
| assert summary.total_executions == expected_total | ||
| assert summary.ended_executions == expected_ended | ||
| assert summary.has_ended == (expected_ended == expected_total) | ||
|
|
||
|
|
||
| class TestPipelineRunServiceList: | ||
| def test_list_empty(self): | ||
| session_factory = _initialize_db_and_get_session_factory() | ||
| service = PipelineRunsApiService_Sql() | ||
| with session_factory() as session: | ||
| result = service.list(session=session) | ||
| assert result.pipeline_runs == [] | ||
morgan-wowk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assert result.next_page_token is None | ||
|
|
||
| def test_list_returns_pipeline_runs(self): | ||
| session_factory = _initialize_db_and_get_session_factory() | ||
| service = PipelineRunsApiService_Sql() | ||
| with session_factory() as session: | ||
| root = _create_execution_node(session) | ||
| root_id = root.id | ||
| _create_pipeline_run(session, root, created_by="user1") | ||
| session.commit() | ||
|
|
||
| with session_factory() as session: | ||
| result = service.list(session=session) | ||
| assert len(result.pipeline_runs) == 1 | ||
| assert result.pipeline_runs[0].root_execution_id == root_id | ||
| assert result.pipeline_runs[0].created_by == "user1" | ||
| assert result.pipeline_runs[0].execution_status_stats is None | ||
|
|
||
| def test_list_with_execution_stats(self): | ||
| session_factory = _initialize_db_and_get_session_factory() | ||
morgan-wowk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| service = PipelineRunsApiService_Sql() | ||
| with session_factory() as session: | ||
| root = _create_execution_node(session) | ||
| root_id = root.id | ||
| child1 = _create_execution_node( | ||
| session, | ||
| parent=root, | ||
| status=bts.ContainerExecutionStatus.SUCCEEDED, | ||
| ) | ||
| child2 = _create_execution_node( | ||
| session, | ||
| parent=root, | ||
| status=bts.ContainerExecutionStatus.RUNNING, | ||
| ) | ||
| _link_ancestor(session, child1, root) | ||
| _link_ancestor(session, child2, root) | ||
| _create_pipeline_run(session, root) | ||
| session.commit() | ||
|
|
||
| with session_factory() as session: | ||
| result = service.list(session=session, include_execution_stats=True) | ||
| assert len(result.pipeline_runs) == 1 | ||
| assert result.pipeline_runs[0].root_execution_id == root_id | ||
| stats = result.pipeline_runs[0].execution_status_stats | ||
| assert stats is not None | ||
| assert stats["SUCCEEDED"] == 1 | ||
| assert stats["RUNNING"] == 1 | ||
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.
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.
:nit
If we are treating test code with the same standard as production code then I'll just mention:
Alexey has asked me to import modules (
api_server_sql) rather than directly importing classes or function.So you would use
api_server_sql.ExecutionStatusSummaryrather thanExecutionStatusSummarydirectly in this file. Same with other imports.