-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcel_generator.py
More file actions
174 lines (136 loc) · 7.13 KB
/
excel_generator.py
File metadata and controls
174 lines (136 loc) · 7.13 KB
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
import xlsxwriter
def get_column_name(index):
symbols = [chr(x) for x in range(ord('A'), ord('Z') + 1)]
current = index % len(symbols)
leftover = index - current
result = symbols[current]
if leftover == 0:
return result
return get_column_name(int(leftover / len(symbols)) - 1) + result
def get_cell_name(row, column):
return '{}{}'.format(get_column_name(column), int(row + 1))
class ExcelGenerator():
def __init__(self, outputFileName=None):
self.file_name = outputFileName
def GenerateReport(self, fullReport):
if self.file_name is None:
self.file_name = 'report.xlsx'
workbook = xlsxwriter.Workbook(self.file_name)
bold = workbook.add_format({'bold': 1})
summary_sheet = workbook.add_worksheet('summary')
charts_sheet = workbook.add_worksheet('charts')
commits_sheet = workbook.add_worksheet('commits')
additions_sheet = workbook.add_worksheet('additions')
removals_sheet = workbook.add_worksheet('removals')
total_commits_sheet = workbook.add_worksheet('commits_with_merges')
# write summary on repo
summary_table_options = {
'columns':
[
{'header': 'Repo name'},
{'header': 'Repo URL'}
]
}
repo_list = fullReport.getRepoInfo()
description_row = 1
start_row = 3
start_column = 1
end_row = start_row + len(repo_list)
end_column = 2
cell_format = workbook.add_format({
'bold': True,
'align': 'center',
'valign': 'vcenter',
})
summary_sheet.merge_range('{}:{}'.format(
get_cell_name(row=description_row, column=start_column), get_cell_name(row=description_row, column=end_column))
, 'Repository used to collect data', cell_format)
name_column=get_column_name(start_column)
url_column=get_column_name(end_column)
summary_sheet.set_column(f'{name_column}:{name_column}', 20)
summary_sheet.set_column(f'{url_column}:{url_column}', 60)
summary_repo_table = summary_sheet.add_table('{}:{}'.format(
get_cell_name(row=start_row, column=start_column), get_cell_name(row=end_row, column=end_column)), options=summary_table_options)
current_row = start_row + 1
for r in repo_list:
summary_sheet.write_row(get_cell_name(row=current_row, column=start_column), [r.name, r.link])
current_row = current_row + 1
# write headings
commits_sheet.write_string(0, 0, 'Month', bold)
additions_sheet.write_string(0, 0, 'Month', bold)
removals_sheet.write_string(0, 0, 'Month', bold)
total_commits_sheet.write_string(0, 0, 'Month', bold)
author_to_column = {}
for c, a in enumerate(fullReport.getAuthors()):
column_index = c+1
author_to_column[a] = column_index
commits_sheet.write(0, column_index, a, bold)
additions_sheet.write(0, column_index, a, bold)
removals_sheet.write(0, column_index, a, bold)
total_commits_sheet.write(0, column_index, a, bold)
current_row = 1
ranges = fullReport.getSortedRanges()
for date_range in ranges:
period_string = '{} {}'.format(date_range.start.month, date_range.start.year)
commits_sheet.write_string(current_row, 0, period_string)
additions_sheet.write_string(current_row, 0, period_string)
removals_sheet.write_string(current_row, 0, period_string)
total_commits_sheet.write_string(current_row, 0, period_string)
for author_name,colunm_index in author_to_column.items():
authorReport = fullReport.getReportEntry(date_range, author_name)
commits_sheet.write_number(current_row, colunm_index, authorReport.commits)
additions_sheet.write_number(current_row, colunm_index, authorReport.additions)
removals_sheet.write_number(current_row, colunm_index, authorReport.removals)
total_commits_sheet.write_number(current_row, colunm_index, authorReport.commits + authorReport.merges)
current_row = current_row + 1
chartCommits = workbook.add_chart({'type': 'line'})
chartCommitsWithMerges = workbook.add_chart({'type': 'line'})
chartAdditions = workbook.add_chart({'type': 'line'})
chartRemovals = workbook.add_chart({'type': 'line'})
for k,v in author_to_column.items():
chartCommits.add_series({
'name': [commits_sheet.name, 0, v],
'categories': [commits_sheet.name, 1, 0, current_row-1, 0],
'values': [commits_sheet.name, 1, v, current_row-1, v]
})
chartAdditions.add_series({
'name': [additions_sheet.name, 0, v],
'categories': [additions_sheet.name, 1, 0, current_row-1, 0],
'values': [additions_sheet.name, 1, v, current_row-1, v]
})
chartRemovals.add_series({
'name': [removals_sheet.name, 0, v],
'categories': [removals_sheet.name, 1, 0, current_row-1, 0],
'values': [removals_sheet.name, 1, v, current_row-1, v]
})
chartCommitsWithMerges.add_series({
'name': [total_commits_sheet.name, 0, v],
'categories': [total_commits_sheet.name, 1, 0, current_row-1, 0],
'values': [total_commits_sheet.name, 1, v, current_row-1, v]
})
chartCommits.set_title ({'name': 'Commits per month'})
chartCommits.set_x_axis({'name': 'Month'})
chartCommits.set_y_axis({'name': 'Commits'})
chartAdditions.set_title ({'name': 'Additions per month'})
chartAdditions.set_x_axis({'name': 'Month'})
chartAdditions.set_y_axis({'name': 'Additions'})
chartRemovals.set_title ({'name': 'Removals per month'})
chartRemovals.set_x_axis({'name': 'Month'})
chartRemovals.set_y_axis({'name': 'Removals'})
chartCommitsWithMerges.set_title ({'name': 'Commits and merges'})
chartCommitsWithMerges.set_x_axis({'name': 'Month'})
chartCommitsWithMerges.set_y_axis({'name': 'Commits'})
chartCommits.set_style(10)
chartAdditions.set_style(10)
chartRemovals.set_style(10)
chartCommitsWithMerges.set_style(10)
chartCommits.set_size({'width': 720, 'height': 576})
chartAdditions.set_size({'width': 720, 'height': 576})
chartRemovals.set_size({'width': 720, 'height': 576})
chartCommitsWithMerges.set_size({'width': 720, 'height': 576})
# Insert the chart into the worksheet (with an offset).
charts_sheet.insert_chart('B2', chartCommits, {'x_offset': 25, 'y_offset': 10})
charts_sheet.insert_chart('B32', chartAdditions, {'x_offset': 25, 'y_offset': 10})
charts_sheet.insert_chart('B64', chartRemovals, {'x_offset': 25, 'y_offset': 10})
charts_sheet.insert_chart('B96', chartCommitsWithMerges, {'x_offset': 25, 'y_offset': 10})
workbook.close()