From 4ba1a221142a4f61a13249cd0dafb2e43fd86f65 Mon Sep 17 00:00:00 2001 From: Yizhe Yuan Date: Sun, 26 May 2019 23:45:58 +0800 Subject: [PATCH] Use accurate history to calculate devrank --- persper/analytics/call_commit_graph.py | 23 +++++++++------- test/test_analytics/test_call_commit_graph.py | 27 +++++++++++++++++++ 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/persper/analytics/call_commit_graph.py b/persper/analytics/call_commit_graph.py index 0c83fc928c6..b6a0b21926c 100644 --- a/persper/analytics/call_commit_graph.py +++ b/persper/analytics/call_commit_graph.py @@ -148,15 +148,15 @@ def _set_all_nodes_size(self, black_set=None): """ for node in self.nodes(): node_history = self._get_node_history(node) - if black_set is not None: - size = 0 - for cid, chist in node_history.items(): - sha = self.commits()[cid]['hexsha'] - if sha not in black_set: - size += (chist['adds'] + chist['dels']) - else: - size = sum([chist['adds'] + chist['dels'] for chist in node_history.values()]) - + size = 0 + for cid, chist in node_history.items(): + sha = self.commits()[cid]['hexsha'] + if black_set is not None and sha in black_set: + continue + if 'added_units' in chist.keys() and 'removed_units' in chist.keys(): + size += (chist['added_units'] + chist['removed_units']) + else: + size += (chist['adds'] + chist['dels']) # set default size to 1 to avoid zero division error if size == 0: size = 1 @@ -206,7 +206,10 @@ def commit_devranks(self, alpha, black_set=None): continue for cid, chist in history.items(): - csize = chist['adds'] + chist['dels'] + if 'added_units' in chist.keys() and 'removed_units' in chist.keys(): + csize = (chist['added_units'] + chist['removed_units']) + else: + csize = (chist['adds'] + chist['dels']) sha = self.commits()[cid]['hexsha'] if black_set is None or sha not in black_set: dr = (csize / size) * func_devranks[func] diff --git a/test/test_analytics/test_call_commit_graph.py b/test/test_analytics/test_call_commit_graph.py index aeae9cca858..bde1b87a655 100644 --- a/test/test_analytics/test_call_commit_graph.py +++ b/test/test_analytics/test_call_commit_graph.py @@ -95,6 +95,33 @@ def test_call_commit_graph(): assert isclose(dev_drs3[second_commit['authorEmail']], 0.201, rel_tol=1e-2) +def test_devrank_with_accurate_history(): + ccgraph = CallCommitGraph() + first_commit = { + 'hexsha': '0x01', + 'authorName': 'koala', + 'authorEmail': 'koala@persper.org', + 'message': 'first commit' + } + ccgraph.add_commit(first_commit['hexsha'], + first_commit['authorName'], + first_commit['authorEmail'], + first_commit['message']) + ccgraph.add_node('f1') + ccgraph.update_node_history_accurate('f1', {'adds': 10, 'dels': 0, 'added_units': 20, 'removed_units': 0}) + ccgraph.add_node('f2') + ccgraph.update_node_history_accurate('f2', {'adds': 10, 'dels': 0, 'added_units': 40, 'removed_units': 0}) + ccgraph.add_edge('f1', 'f2') + + func_drs = ccgraph.function_devranks(0.85) + commit_drs = ccgraph.commit_devranks(0.85) + dev_drs = ccgraph.developer_devranks(0.85) + assert isclose(func_drs['f1'], 0.26, rel_tol=1e-2) + assert isclose(func_drs['f2'], 0.74, rel_tol=1e-2) + assert isclose(commit_drs[first_commit['hexsha']], 1) + assert isclose(dev_drs[first_commit['authorEmail']], 1) + + @pytest.mark.asyncio async def test_black_set(): """