git_issue/dao.py

100 lines
3.4 KiB
Python

__author__ = 'mac'
import pymysql
# build connection
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='zlb_github', charset='utf8')
# get issue info by id
def get_info_by_id(id):
cur = conn.cursor()
sql = 'select title, body from issues where id = '+repr(id)
cur.execute(sql)
return cur
# get data from database, and issue_type indicate the type of issues
def get_data(proj_id):
cur = conn.cursor()
sql = 'select issue_type, title, body, id from issues where project_id = '+repr(proj_id)+\
' and issue_type is not NULL order by rand()'
cur.execute(sql)
return cur
def get_feature(proj_id):
cur = conn.cursor()
sql = 'select title, body, id from issues where project_id = '+repr(proj_id)+\
' and issue_type = "enhancement" '
cur.execute(sql)
return cur
def get_feature_and_bug(proj_id):
cur = conn.cursor()
sql = 'select title, body, number, issue_type from issues where project_id = '+repr(proj_id)+ \
' and issue_type is not NULL'
cur.execute(sql)
return cur
def get_bug(proj_id):
cur = conn.cursor()
sql = 'select title, body, id from issues where project_id = '+repr(proj_id)+\
' and issue_type = "bug"'
cur.execute(sql)
return cur
def insert_data(proj_id,x_id,different,pred,y_test):
table_name = 'project_'+proj_id
cur = conn.cursor()
for i in range(len(x_id)):
sql = 'update '+table_name+ ' set pred_0 = '+ repr(different[i][0]) + \
', pred_1 = '+ repr(different[i][1]) +\
', pred = ' + repr(pred[i]) + \
', y_test = '+ repr(y_test[i])+\
' where id = '+ repr(x_id[i])
# data = (different[i][0],different[i][1],pred[i],y_test[i],x_id[i])
cur.execute(sql)
print(i)
if (i+1)%1000 == 0:
conn.commit()
conn.commit()
def close():
conn.close()
def get_project():
cur = conn.cursor()
sql = 'select project_id,COUNT(*) as num from issues ' \
'where issue_type is not NULL GROUP BY project_id ORDER BY num desc'
cur.execute(sql)
return cur
def get_proj_name_by_id(proj_id):
cur = conn.cursor()
sql = "select CONCAT(user_name,"+repr('\\')+",repo_name) from project where project_id = "+proj_id
cur.execute(sql)
return cur
def get_all_issue_by_proj_id(proj_id):
cur = conn.cursor()
sql = "select title, body, id from issues_for_yu where project_id = "+repr(proj_id)+\
' and issue_type is NULL'
cur.execute(sql)
return cur
def result_handle(pred,test_id):
cur = conn.cursor()
categories = ['bug','enhancement']
for i in range(len(pred)):
issue_type = categories[pred[i]]
sql = "update issues_for_yu set issue_type = " + repr(issue_type) + " where id = "+repr(test_id[i])
cur.execute(sql)
if (i+1)%10000 == 0 or i ==len(pred)-1:
conn.commit()
conn.commit()
def save_kmeans_result(x_id,y_pred,proj_id):
cur = conn.cursor()
for i in range(len(x_id)):
# sql = 'update issues set kmeans = ' + repr(y_pred[i]) + ' where number = ' + repr(x_id[i]) + ' and project_id = ' + proj_id
sql = 'update numpy_sentences set kmeans = ' + repr(y_pred[i]) + ' where number = ' + repr(x_id[i])
cur.execute(sql)
conn.commit()