|
9 | 9 | ) |
10 | 10 | from typing import ( |
11 | 11 | cast, |
| 12 | + Literal, |
12 | 13 | Optional, |
13 | 14 | Union, |
14 | 15 | ) |
|
77 | 78 | WriteHistoryTo, |
78 | 79 | ) |
79 | 80 | from galaxy.schema.types import LatestLiteral |
| 81 | +from galaxy.schema.workflows import ( |
| 82 | + WorkflowExtractionJob, |
| 83 | + WorkflowExtractionOutput, |
| 84 | + WorkflowExtractionSummary, |
| 85 | +) |
80 | 86 | from galaxy.security.idencoding import IdEncodingHelper |
81 | 87 | from galaxy.short_term_storage import ShortTermStorageAllocator |
82 | 88 | from galaxy.util import restore_text |
|
90 | 96 | ) |
91 | 97 | from galaxy.webapps.galaxy.services.notifications import NotificationService |
92 | 98 | from galaxy.webapps.galaxy.services.sharable import ShareableService |
| 99 | +from galaxy.workflow.extract import summarize |
93 | 100 |
|
94 | 101 | log = logging.getLogger(__name__) |
95 | 102 |
|
@@ -751,6 +758,102 @@ def archive_history( |
751 | 758 | history = self.manager.archive_history(history, archive_export_id=archive_export_id) |
752 | 759 | return self._serialize_archived_history(trans, history) |
753 | 760 |
|
| 761 | + def create_workflow_extraction_summary( |
| 762 | + self, |
| 763 | + history_id: DecodedDatabaseIdField, |
| 764 | + trans: ProvidesHistoryContext, |
| 765 | + ) -> WorkflowExtractionSummary: |
| 766 | + history = self.manager.get_accessible(history_id, trans.user, current_history=trans.history) |
| 767 | + jobs, warnings = summarize(trans, history) |
| 768 | + |
| 769 | + def serialize_output(content) -> WorkflowExtractionOutput: |
| 770 | + return WorkflowExtractionOutput.model_validate( |
| 771 | + { |
| 772 | + "id": content.id, |
| 773 | + "hid": content.hid, |
| 774 | + "name": content.name, |
| 775 | + "state": content.state, |
| 776 | + "deleted": content.deleted, |
| 777 | + "history_content_type": content.history_content_type, |
| 778 | + } |
| 779 | + ) |
| 780 | + |
| 781 | + def input_step_type(outputs: list[WorkflowExtractionOutput]) -> Literal["input_dataset", "input_collection"]: |
| 782 | + if outputs and outputs[0].history_content_type == "dataset_collection": |
| 783 | + return "input_collection" |
| 784 | + return "input_dataset" |
| 785 | + |
| 786 | + jobs_list = [] |
| 787 | + for job, datasets in jobs.items(): |
| 788 | + is_fake = getattr(job, "is_fake", False) |
| 789 | + outputs = [serialize_output(data) for _, data in datasets] |
| 790 | + checked = any(not data.deleted for _, data in datasets) |
| 791 | + |
| 792 | + if is_fake: |
| 793 | + # FakeJob / DatasetCollectionCreationJob: input with no creating tool. |
| 794 | + jobs_list.append( |
| 795 | + WorkflowExtractionJob( |
| 796 | + id=None, |
| 797 | + step_type=input_step_type(outputs), |
| 798 | + tool_name=getattr(job, "name", None), |
| 799 | + tool_id=None, |
| 800 | + tool_version=None, |
| 801 | + checked=checked, |
| 802 | + tool_version_warning=None, |
| 803 | + outputs=outputs, |
| 804 | + ) |
| 805 | + ) |
| 806 | + else: |
| 807 | + tool = trans.app.toolbox.get_tool(job.tool_id, tool_version=job.tool_version) |
| 808 | + if tool is None: |
| 809 | + # Tool missing |
| 810 | + continue |
| 811 | + if not tool.is_workflow_compatible: |
| 812 | + # Not a workflow step (e.g. upload, data fetch) — treat as input. |
| 813 | + jobs_list.append( |
| 814 | + WorkflowExtractionJob( |
| 815 | + id=None, |
| 816 | + step_type=input_step_type(outputs), |
| 817 | + tool_name=tool.name, |
| 818 | + tool_id=None, |
| 819 | + tool_version=None, |
| 820 | + checked=checked, |
| 821 | + tool_version_warning=None, |
| 822 | + outputs=outputs, |
| 823 | + ) |
| 824 | + ) |
| 825 | + else: |
| 826 | + tool_version_warning = ( |
| 827 | + ( |
| 828 | + f'Dataset was created with tool version "{job.tool_version}", ' |
| 829 | + f'but workflow extraction will use version "{tool.version}".' |
| 830 | + ) |
| 831 | + if tool.version != job.tool_version |
| 832 | + else None |
| 833 | + ) |
| 834 | + jobs_list.append( |
| 835 | + WorkflowExtractionJob.model_validate( |
| 836 | + { |
| 837 | + "id": job.id, |
| 838 | + "step_type": "tool", |
| 839 | + "tool_name": tool.name, |
| 840 | + "tool_id": job.tool_id, |
| 841 | + "tool_version": job.tool_version, |
| 842 | + "checked": checked, |
| 843 | + "tool_version_warning": tool_version_warning, |
| 844 | + "outputs": outputs, |
| 845 | + } |
| 846 | + ) |
| 847 | + ) |
| 848 | + |
| 849 | + return WorkflowExtractionSummary.model_validate( |
| 850 | + { |
| 851 | + "history_id": history.id, |
| 852 | + "warnings": list(warnings), |
| 853 | + "jobs": jobs_list, |
| 854 | + } |
| 855 | + ) |
| 856 | + |
754 | 857 | def _ensure_export_record_can_be_associated_with_history_archival( |
755 | 858 | self, history_id: int, export_record: model.StoreExportAssociation |
756 | 859 | ): |
|
0 commit comments