Skip to content

Commit

Permalink
sort workflows by total_conditions_satisfied (#3342)
Browse files Browse the repository at this point in the history
* sort workflows by total_conditions_satisfied

* for wk -> for _, wk

* add test
  • Loading branch information
antgonza authored Dec 19, 2023
1 parent 02dc7a0 commit b0d12fb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
19 changes: 11 additions & 8 deletions qiita_db/metadata_template/prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,33 +853,32 @@ def _get_predecessors(workflow, node):

all_workflows = [wk for wk in qdb.software.DefaultWorkflow.iter()]
# are there any workflows with parameters?
check_requirements = False
default_parameters = {'prep': {}, 'sample': {}}
if [wk for wk in all_workflows if wk.parameters != default_parameters]:
check_requirements = True
ST = qdb.metadata_template.sample_template.SampleTemplate
workflows = []
for wk in all_workflows:
if wk.artifact_type == pt_artifact and pt_dt in wk.data_type:
if check_requirements and wk.parameters == default_parameters:
continue
wk_params = wk.parameters
reqs_satisfied = True
total_conditions_satisfied = 0

if wk_params['sample']:
df = ST(self.study_id).to_dataframe(samples=list(self))
for k, v in wk_params['sample'].items():
if k not in df.columns or v not in df[k].unique():
reqs_satisfied = False
else:
total_conditions_satisfied += 1

if wk_params['prep']:
df = self.to_dataframe()
for k, v in wk_params['prep'].items():
if k not in df.columns or v not in df[k].unique():
reqs_satisfied = False
else:
total_conditions_satisfied += 1

if reqs_satisfied:
workflows.append(wk)
workflows.append((total_conditions_satisfied, wk))

if not workflows:
# raises option a.
Expand All @@ -888,8 +887,12 @@ def _get_predecessors(workflow, node):
'could be due to required parameters, please check the '
'available workflows.')
raise ValueError(msg)

# let's just keep one, let's give it preference to the one with the
# most total_conditions_satisfied
workflows = sorted(workflows, key=lambda x: x[0], reverse=True)[:1]
missing_artifacts = dict()
for wk in workflows:
for _, wk in workflows:
missing_artifacts[wk] = dict()
for node, degree in wk.graph.out_degree():
if degree != 0:
Expand Down
24 changes: 24 additions & 0 deletions qiita_db/metadata_template/test/test_prep_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,30 @@ def test_artifact_setter(self):
'Pick closed-reference OTUs', 'Pick closed-reference OTUs',
'Pick closed-reference OTUs'])

# at this point we can error all the previous steps, add a new smaller
# workflow and make sure you get the same one as before because it will
# have a higher match than the new one
for pj in wk.graph.nodes:
pj._set_error('Killed')
sql = """UPDATE qiita.default_workflow_data_type
SET data_type_id = 1
WHERE default_workflow_id = 2"""
qdb.sql_connection.perform_as_transaction(sql)
wk = pt.add_default_workflow(qdb.user.User('test@foo.bar'))
self.assertEqual(len(wk.graph.nodes), 5)
self.assertEqual(len(wk.graph.edges), 3)
self.assertCountEqual(
[x.command.name for x in wk.graph.nodes],
# we should have 2 split libraries and 3 close reference
['Split libraries FASTQ', 'Split libraries FASTQ',
'Pick closed-reference OTUs', 'Pick closed-reference OTUs',
'Pick closed-reference OTUs'])
# let's return it back
sql = """UPDATE qiita.default_workflow_data_type
SET data_type_id = 2
WHERE default_workflow_id = 2"""
qdb.sql_connection.perform_as_transaction(sql)

# now let's try to generate again and it should fail cause the jobs
# are already created
with self.assertRaisesRegex(ValueError, "Cannot create job because "
Expand Down

0 comments on commit b0d12fb

Please sign in to comment.