Skip to content

Commit 379caa5

Browse files
authored
fix #3224 (#3347)
* fix #3224 * fixing code * rm duplicated edges/nodes
1 parent daf3319 commit 379caa5

File tree

3 files changed

+65
-7
lines changed

3 files changed

+65
-7
lines changed

qiita_db/artifact.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,10 +1277,8 @@ def _add_edge(edges, src, dest):
12771277
qdb.sql_connection.TRN.add(sql, [self.id])
12781278
sql_edges = qdb.sql_connection.TRN.execute_fetchindex()
12791279

1280-
lineage = nx.DiGraph()
1281-
edges = set()
1282-
nodes = {}
1283-
if sql_edges:
1280+
# helper function to reduce code duplication
1281+
def _helper(sql_edges, edges, nodes):
12841282
for jid, pid, cid in sql_edges:
12851283
if jid not in nodes:
12861284
nodes[jid] = ('job',
@@ -1291,9 +1289,29 @@ def _add_edge(edges, src, dest):
12911289
nodes[cid] = ('artifact', qdb.artifact.Artifact(cid))
12921290
edges.add((nodes[pid], nodes[jid]))
12931291
edges.add((nodes[jid], nodes[cid]))
1292+
1293+
lineage = nx.DiGraph()
1294+
edges = set()
1295+
nodes = dict()
1296+
extra_edges = set()
1297+
extra_nodes = dict()
1298+
if sql_edges:
1299+
_helper(sql_edges, edges, nodes)
12941300
else:
12951301
nodes[self.id] = ('artifact', self)
12961302
lineage.add_node(nodes[self.id])
1303+
# if this is an Analysis we need to check if there are extra
1304+
# edges/nodes as there is a chance that there are connecions
1305+
# between them
1306+
if self.analysis is not None:
1307+
roots = [a for a in self.analysis.artifacts
1308+
if not a.parents and a != self]
1309+
for r in roots:
1310+
# add the root to the options then their children
1311+
extra_nodes[r.id] = ('artifact', r)
1312+
qdb.sql_connection.TRN.add(sql, [r.id])
1313+
sql_edges = qdb.sql_connection.TRN.execute_fetchindex()
1314+
_helper(sql_edges, extra_edges, extra_nodes)
12971315

12981316
# The code above returns all the jobs that have been successfully
12991317
# executed. We need to add all the jobs that are in all the other
@@ -1329,8 +1347,10 @@ def _add_edge(edges, src, dest):
13291347
# need to check both the input_artifacts and the
13301348
# pending properties
13311349
for in_art in n_obj.input_artifacts:
1332-
_add_edge(edges, nodes[in_art.id],
1333-
nodes[n_obj.id])
1350+
iid = in_art.id
1351+
if iid not in nodes and iid in extra_nodes:
1352+
nodes[iid] = extra_nodes[iid]
1353+
_add_edge(edges, nodes[iid], nodes[n_obj.id])
13341354

13351355
pending = n_obj.pending
13361356
for pred_id in pending:

qiita_db/test/test_artifact.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,42 @@ def test_has_human(self):
14061406

14071407
self.assertTrue(artifact.has_human)
14081408

1409+
def test_descendants_with_jobs(self):
1410+
# let's tests that we can connect two artifacts with different root
1411+
# in the same analysis
1412+
# 1. make sure there are 3 nodes
1413+
a = qdb.artifact.Artifact(8)
1414+
self.assertEqual(len(a.descendants_with_jobs.nodes), 3)
1415+
self.assertEqual(len(a.analysis.artifacts), 2)
1416+
# 2. add a new root and make sure we see it
1417+
c = qdb.artifact.Artifact.create(
1418+
self.filepaths_root, "BIOM", analysis=a.analysis,
1419+
data_type="16S")
1420+
self.assertEqual(len(a.analysis.artifacts), 3)
1421+
# 3. add jobs conencting the new artifact to the other root
1422+
# a -> job -> b
1423+
# c
1424+
# job1 connects b & c
1425+
# job2 connects a & c
1426+
cmd = qdb.software.Command.create(
1427+
qdb.software.Software(1),
1428+
"CommandWithMultipleInputs", "", {
1429+
'input_b': ['artifact:["BIOM"]', None],
1430+
'input_c': ['artifact:["BIOM"]', None]}, {'out': 'BIOM'})
1431+
params = qdb.software.Parameters.load(
1432+
cmd, values_dict={'input_b': a.children[0].id, 'input_c': c.id})
1433+
job1 = qdb.processing_job.ProcessingJob.create(
1434+
qdb.user.User('test@foo.bar'), params)
1435+
params = qdb.software.Parameters.load(
1436+
cmd, values_dict={'input_b': a.id, 'input_c': c.id})
1437+
job2 = qdb.processing_job.ProcessingJob.create(
1438+
qdb.user.User('test@foo.bar'), params)
1439+
1440+
jobs = [j[1] for e in a.descendants_with_jobs.edges
1441+
for j in e if j[0] == 'job']
1442+
self.assertIn(job1, jobs)
1443+
self.assertIn(job2, jobs)
1444+
14091445

14101446
@qiita_test_checker()
14111447
class ArtifactArchiveTests(TestCase):

qiita_pet/handlers/analysis_handlers/base_handlers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ def analyisis_graph_handler_get_request(analysis_id, user):
196196
# This should never happen, but worth having a useful message
197197
raise ValueError('More than one workflow in a single analysis')
198198

199-
return {'edges': edges, 'nodes': nodes, 'workflow': wf_id,
199+
# the list(set()) is to remove any duplicated nodes
200+
return {'edges': list(set(edges)), 'nodes': list(set(nodes)),
201+
'workflow': wf_id,
200202
'artifacts_being_deleted': artifacts_being_deleted}
201203

202204

0 commit comments

Comments
 (0)