fix the get changed

This commit is contained in:
zy 2022-08-24 22:28:56 +08:00
parent fe0427e2bf
commit 03dd8c12cd
1 changed files with 98 additions and 46 deletions

View File

@ -39,42 +39,40 @@ class RiskEvaluator(object):
id=self.repoInfo.id,
separator=GlobalConstants.SEPARATOR,
)
blob_methods = "{id}{separator}blob_methods".format(
id=self.repoInfo.id,
separator=GlobalConstants.SEPARATOR,
)
blob_commit_relations = "{id}{separator}blob_commit_relations".format(
id=self.repoInfo.id,
separator=GlobalConstants.SEPARATOR,
)
# Fix: only consider the life time of clone pair(clone pair existing commits)
method_ids_1 = []
method_ids_2 = []
clone_methods_1 = []
clone_methods_2 = []
clone_pairs = []
sql_clone_pairs_1 = """
select method_id_1, method_id_2, commit_id from `{tablename}`
where function_id_1 = {function_id_1} and function_id_2 = {function_id_2}
relate_commits = []
sql_clone_pairs = """
select method_id_1,function_id_1,method_id_2,function_id_2,commit_id from `{tablename}`
where function_id_1 = {function_id_1} and function_id_2 = {function_id_2} or
function_id_1 = {function_id_2} and function_id_2 = {function_id_1}
""".format(
tablename=clone_relations_function,
function_id_1=self.function_id_1,
function_id_2=self.function_id_2,
)
mysqlOp.cursor.execute(sql_clone_pairs_1)
clone_pairs_1 = mysqlOp.cursor.fetchall()
for clone_pair in clone_pairs_1:
method_ids_1.append(clone_pair.get("method_id_1"))
method_ids_2.append(clone_pair.get("method_id_2"))
clone_pairs.extend(clone_pairs_1)
sql_clone_pairs_2 = """
select method_id_1, method_id_2, commit_id from `{tablename}`
where function_id_1 = {function_id_2} and function_id_2 = {function_id_1}
""".format(
tablename=clone_relations_function,
function_id_1=self.function_id_1,
function_id_2=self.function_id_2,
)
mysqlOp.cursor.execute(sql_clone_pairs_2)
clone_pairs_2 = mysqlOp.cursor.fetchall()
for clone_pair in clone_pairs_2:
method_ids_2.append(clone_pair.get("method_id_1"))
method_ids_1.append(clone_pair.get("method_id_2"))
clone_pairs.extend(clone_pairs_2)
mysqlOp.cursor.execute(sql_clone_pairs)
clone_pairs = mysqlOp.cursor.fetchall()
for clone_pair in clone_pairs:
if clone_pair.get("function_id_1") == self.function_id_1:
method_ids_1.append(clone_pair.get("method_id_1"))
method_ids_2.append(clone_pair.get("method_id_2"))
relate_commits.append(clone_pair.get("commit_id"))
else:
method_ids_1.append(clone_pair.get("method_id_2"))
method_ids_2.append(clone_pair.get("method_id_1"))
relate_commits.append(clone_pair.get("commit_id"))
for method_id, count in Counter(method_ids_1).items():
clone_methods_1.append(method_id)
@ -83,28 +81,82 @@ class RiskEvaluator(object):
# get the CpI
# Find changed methods in clone pairs
result_changes_1 = []
result_changes_2 = []
for method_id in clone_methods_1:
sql_change = """
select * from `{tablename1}` where method_id_1 = {method_id}
""".format(
tablename1=method_function_relations,
method_id=method_id,
def method_change(clone_methods, relate_commits, function_id):
result_changes = [] # 最终涉及到的所有的变化
out_clone_methods = []
all_method_id = []
mysqlOp.cursor.execute(
"select id from `{tablename1}` where function_id = {function_id}".format(
tablename1=blob_methods, function_id=function_id
)
)
all_methods = mysqlOp.cursor.fetchall()
for method in all_methods: # 该function下的所有method_id
all_method_id.append(method.get("id"))
for method_id in clone_methods: # 找到所有与克隆相关的change
sql_change = """
select * from `{tablename1}` where method_id_1 = {method_id}
""".format(
tablename1=method_function_relations,
method_id=method_id,
)
mysqlOp.cursor.execute(sql_change)
result_changes.extend(mysqlOp.cursor.fetchall())
for change in result_changes: # 挑出发生变化后不属于克隆对但还是属于该function_id下的method_id
if (
change.get("method_id_2") in all_method_id
and change.get("method_id_2") not in clone_methods
):
out_clone_methods.append(change.get("method_id_2"))
mysqlOp.cursor.execute(sql_change)
result_changes_1.extend(mysqlOp.cursor.fetchall())
for method_id in clone_methods_2:
sql_change = """
select * from `{tablename1}` where method_id_1 = {method_id}
""".format(
tablename1=method_function_relations,
method_id=method_id,
)
def out_clone_change(out_clone_methods):
next_methods = []
for method_id in out_clone_methods:
check_commit = """
select commit_id from `{tablename1}` where blob_id = (
select blob_id from `{tablename2}` where id = {id}
)
""".format(
tablename1=blob_commit_relations,
tablename2=blob_methods,
id=method_id,
)
mysqlOp.cursor.execute(check_commit)
commit_id = mysqlOp.cursor.fetchone()[0]
if commit_id in relate_commits: # 检查这个method所在的commit是否属于克隆的生命周期内
mysqlOp.cursor.execute(
"select * from `{tablename1}` where method_id_1 = {method_id}".format(
tablename1=method_function_relations,
method_id=method_id,
)
)
results = mysqlOp.cursor.fetchall()
if len(results) == 0: # 未产生变化就返回空值
return []
else:
for (
result
) in results: # 挑出发生变化后不属于克隆对但还是属于该function_id下的method_id
if (
result.get("method_id_2") in all_method_id
and result.get("method_id_2") not in clone_methods
):
next_methods.append(result.get("method_id_2"))
results.extend(out_clone_change(next_methods))
return results
else:
return []
mysqlOp.cursor.execute(sql_change)
result_changes_2.extend(mysqlOp.cursor.fetchall())
if len(out_clone_methods) != 0:
result_changes.extend(out_clone_change(out_clone_methods))
return result_changes
result_changes_1 = method_change(
clone_methods_1, relate_commits, self.function_id_1
)
result_changes_2 = method_change(
clone_methods_2, relate_commits, self.function_id_2
)
sum_changes = len(result_changes_1) + len(result_changes_2)
print(sum_changes)
@ -216,6 +268,6 @@ repoInfos: List[RepoInfo] = FileOperator("repos").load_repos()
mysqlOp: MySQLOperator = MySQLOperator(config_path="config.yml")
for repoInfo in repoInfos:
clone_pair = RiskEvaluator(
8, 10, repoInfo
10, 8, repoInfo
) # Once evaluate the risk of one clone pair(function_id)
print(clone_pair.evaluate(mysqlOp))