-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_group.py
282 lines (224 loc) · 8.66 KB
/
py_group.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# coding=UTF-8
import mysql.connector
import xlrd
import xlsxwriter
import os
from mysql.connector import errorcode
from datetime import datetime
# 符号化后的 Excel 文件名
EXCEL_NAME = '20170223_4.0.1_feedback_result_py'
DB_NAME = 'zl_crash'
config = {
'user': 'root',
'password': '123456',
'host': '127.0.0.1',
'database': 'zl_crash',
}
class Report(object):
'''
Report class used to encapsulate the row data in EXCEL
'''
def __init__(self, report_id, exception_type, device_id, exception_symbols, os_version):
self.report_id = report_id;
self.exception_type = exception_type;
self.device_id = device_id;
self.exception_symbols = exception_symbols;
self.os_version = os_version;
def main():
begin_time = datetime.now()
# 表名
table_name = 'report_' + begin_time.strftime("%Y_%m_%d_%H_%M_%S")
# 建表
create_table_in_db(table_name)
# 插入数据
insert_symbolication_result_into_db(table_name)
# 对数据进行分组并导出
generate_grouped_exception(table_name)
end_time = datetime.now()
print('耗时:' + str(end_time - begin_time))
def create_table_in_db(table_name):
'''
Create a table in database, and named as `table_name`
:param table_name: table_name
'''
SQLS = {}
SQLS['drop_report'] = (
"DROP TABLE IF EXISTS `" + table_name + "`")
SQLS['report'] = (
"CREATE TABLE `" + table_name + "` ( "
"`report_id` int(11) NOT NULL AUTO_INCREMENT, "
"`exception_type` varchar(255) DEFAULT NULL, "
"`device_id` varchar(255) DEFAULT NULL, "
"`exception_symbols` longtext, "
"`os_version` varchar(255) DEFAULT NULL, "
"PRIMARY KEY (`report_id`)"
") ENGINE=InnoDB DEFAULT CHARSET=utf8")
try:
conn = mysql.connector.connect(**config)
cursor = conn.cursor();
for name, sql in SQLS.items():
try:
print("Executing sql {}.".format(name))
cursor.execute(sql)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
print('Table already exists.')
else:
print(err.msg)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err.msg)
finally:
cursor.close()
conn.close()
def insert_symbolication_result_into_db(table_name):
'''
Insert the symbolicated result into database
:param table_name: table_name in database
'''
try:
conn = mysql.connector.connect(**config)
# print('connected to db')
cursor = conn.cursor()
insert_report = (
"INSERT INTO " + table_name + " "
"(exception_type, device_id, exception_symbols, os_version) "
"VALUES (%s, %s, %s, %s)")
work_book = xlrd.open_workbook(EXCEL_NAME + '.xlsx')
sheet = work_book.sheets()[0]
nrows = sheet.nrows
ncols = sheet.ncols
row_index = 1
for row_index in range(1, nrows):
data_row = sheet.row_values(row_index)
# assert col < ncols
device_id = data_row[0]
os_version = data_row[1]
exception_type = data_row[2]
exception_symbols = data_row[3]
if exception_symbols == '':
continue
data_report = (exception_type, device_id, exception_symbols, os_version)
# insert report data
cursor.execute(insert_report, data_report)
conn.commit()
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err.msg)
finally:
cursor.close()
conn.close()
def generate_grouped_exception(table_name):
'''
According the group data in database, make all exception to group data.
:param table_name: table_name in zl_crash database
'''
EXCEPTION_TYPE_COUNT = {}
EXCEPTION_MAPPING = {}
try:
conn = mysql.connector.connect(**config)
cursor = conn.cursor()
group_exception_type = (
"SELECT exception_type, COUNT(*) as nums "
"FROM " + table_name + " GROUP BY exception_type")
query_specific_exception = (
"SELECT * FROM " + table_name + " "
"WHERE exception_type = %s")
cursor.execute(group_exception_type)
for (exception_type, nums) in cursor:
EXCEPTION_TYPE_COUNT[exception_type] = nums
# print("exception_type:" + exception_type + ", nums:" + str(nums))
for exception_type in EXCEPTION_TYPE_COUNT.keys():
cursor.execute(query_specific_exception, (exception_type,))
exception_list = []
for (report_id, exception_type, device_id, exception_symbols, os_version) in cursor:
report = Report(report_id, exception_type, device_id, exception_symbols, os_version)
exception_list.append(report)
EXCEPTION_MAPPING[exception_type] = exception_list
write_grouped_exception_to_file(EXCEPTION_TYPE_COUNT, EXCEPTION_MAPPING)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err.msg)
finally:
cursor.close()
conn.close()
def write_grouped_exception_to_file(count, mapping):
'''
Export grouped exception to file
:param count: 字典 key:exception_type value:count
:param mapping: 字典 key:exception_type value:exception_list
'''
output_file_name = EXCEL_NAME + '_grouped.xlsx'
os.system('rm -rf ' + output_file_name)
workbook = xlsxwriter.Workbook(output_file_name)
worksheet = workbook.add_worksheet()
# 设置列宽
worksheet.set_column('A:A', 25)
worksheet.set_column('B:B', 10)
worksheet.set_column('C:C', 25)
worksheet.set_column('D:D', 40)
worksheet.set_column('E:E', 500)
# 粗体格式
bold = workbook.add_format({'font_size': 14,
'align': 'center',
'bold': True})
# 标题行
worksheet.write('A1', 'exception_type', bold)
worksheet.write('B1', 'count', bold)
worksheet.write('C1', 'os_version', bold)
worksheet.write('D1', 'device_id', bold)
worksheet.write('E1', 'symbols', bold)
# 写入 Excel Index 指示器
row_index = 1
col_index = 0
colors = ('#A8BAAA', '#FFF6CF', '#DCCDAE', '#B49D7E',
'#816854', '#334D5C', '#45B29D', '#EFC94C')
count_index = 0
pattern = 0.5
for (type, num) in count.items():
bg_color = colors[count_index % len(colors)]
col_format = workbook.add_format({'pattern': pattern,
'bg_color': bg_color})
num_col_format = workbook.add_format({'pattern': pattern,
'bg_color': bg_color,
'bold': True,
'align': 'center'})
count_index += 1
list = mapping[type]
for i in range(num):
report_item = list[i]
if i == 0:
worksheet.write(row_index, col_index, report_item.exception_type, col_format)
col_index += 1
worksheet.write(row_index, col_index, num, num_col_format)
col_index += 1
else:
worksheet.write(row_index, col_index, '', col_format)
col_index += 1
worksheet.write(row_index, col_index, '', col_format)
col_index += 1
worksheet.write(row_index, col_index, report_item.os_version, col_format)
col_index += 1
worksheet.write(row_index, col_index, report_item.device_id, col_format)
col_index += 1
worksheet.write(row_index, col_index, report_item.exception_symbols, col_format)
# 设置 index
row_index += 1
col_index = 0
# 关闭文件
workbook.close()
print("Exporting grouped data to " + output_file_name)
if __name__ == '__main__':
main()