Passed
Push — master ( 3b73d1...bd66cb )
by Guangyu
02:16 queued 11s
created

excelexporters.equipmentstatistics   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 418
Duplicated Lines 9.33 %

Importance

Changes 0
Metric Value
eloc 299
dl 39
loc 418
rs 9.36
c 0
b 0
f 0
wmc 38

2 Functions

Rating   Name   Duplication   Size   Complexity  
F generate_excel() 0 356 33
B export() 39 39 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import base64
2
import uuid
3
import os
4
from openpyxl.chart import (
5
    LineChart,
6
    Reference,
7
)
8
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font
9
from openpyxl.drawing.image import Image
10
from openpyxl import Workbook
11
12
13
####################################################################################################################
14
# PROCEDURES
15
# Step 1: Validate the report data
16
# Step 2: Generate excel file
17
# Step 3: Encode the excel file bytes to Base64
18
####################################################################################################################
19
20
21 View Code Duplication
def export(report,
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
22
           name,
23
           reporting_start_datetime_local,
24
           reporting_end_datetime_local,
25
           period_type):
26
    ####################################################################################################################
27
    # Step 1: Validate the report data
28
    ####################################################################################################################
29
    if report is None:
30
        return None
31
    print(report)
32
33
    ####################################################################################################################
34
    # Step 2: Generate excel file from the report data
35
    ####################################################################################################################
36
    filename = generate_excel(report,
37
                              name,
38
                              reporting_start_datetime_local,
39
                              reporting_end_datetime_local,
40
                              period_type)
41
    ####################################################################################################################
42
    # Step 3: Encode the excel file to Base64
43
    ####################################################################################################################
44
    try:
45
        with open(filename, 'rb') as binary_file:
46
            binary_file_data = binary_file.read()
47
    except IOError as ex:
48
        pass
49
50
    # Base64 encode the bytes
51
    base64_encoded_data = base64.b64encode(binary_file_data)
0 ignored issues
show
introduced by
The variable binary_file_data does not seem to be defined for all execution paths.
Loading history...
52
    # get the Base64 encoded data using human-readable characters.
53
    base64_message = base64_encoded_data.decode('utf-8')
54
    # delete the file from server
55
    try:
56
        os.remove(filename)
57
    except NotImplementedError as ex:
58
        pass
59
    return base64_message
60
61
62
def generate_excel(report,
63
                   name,
64
                   reporting_start_datetime_local,
65
                   reporting_end_datetime_local,
66
                   period_type):
67
    wb = Workbook()
68
    ws = wb.active
69
70
    # Row height
71
    ws.row_dimensions[1].height = 121
72
73
    for i in range(2, 37 + 1):
74
        ws.row_dimensions[i].height = 30
75
76
    for i in range(38, 90 + 1):
77
        ws.row_dimensions[i].height = 30
78
79
    # Col width
80
    ws.column_dimensions['A'].width = 1.5
81
    ws.column_dimensions['B'].width = 20.0
82
83
    for i in range(ord('C'), ord('I')):
84
        ws.column_dimensions[chr(i)].width = 15.0
85
86
    # Font
87
    name_font = Font(name='Constantia', size=15, bold=True)
88
    title_font = Font(name='宋体', size=15, bold=True)
89
    # data_font = Font(name='Franklin Gothic Book', size=11)
90
91
    table_fill = PatternFill(fill_type='solid', fgColor='1F497D')
92
    f_border = Border(left=Side(border_style='medium', color='00000000'),
93
                      right=Side(border_style='medium', color='00000000'),
94
                      bottom=Side(border_style='medium', color='00000000'),
95
                      top=Side(border_style='medium', color='00000000')
96
                      )
97
    b_border = Border(
98
        bottom=Side(border_style='medium', color='00000000'),
99
    )
100
101
    b_c_alignment = Alignment(vertical='bottom',
102
                              horizontal='center',
103
                              text_rotation=0,
104
                              wrap_text=False,
105
                              shrink_to_fit=False,
106
                              indent=0)
107
    c_c_alignment = Alignment(vertical='center',
108
                              horizontal='center',
109
                              text_rotation=0,
110
                              wrap_text=False,
111
                              shrink_to_fit=False,
112
                              indent=0)
113
    b_r_alignment = Alignment(vertical='bottom',
114
                              horizontal='right',
115
                              text_rotation=0,
116
                              wrap_text=False,
117
                              shrink_to_fit=False,
118
                              indent=0)
119
    # c_r_alignment = Alignment(vertical='bottom',
120
    #                           horizontal='center',
121
    #                           text_rotation=0,
122
    #                           wrap_text=False,
123
    #                           shrink_to_fit=False,
124
    #                           indent=0)
125
126
    # Img
127
    img = Image("excelexporters/myems.png")
128
    # img = Image("myems.png")
129
    ws.add_image(img, 'B1')
130
131
    # Title
132
    ws['B3'].font = name_font
133
    ws['B3'].alignment = b_r_alignment
134
    ws['B3'] = 'Name:'
135
    ws['C3'].border = b_border
136
    ws['C3'].alignment = b_c_alignment
137
    ws['C3'].font = name_font
138
    ws['C3'] = name
139
140
    ws['D3'].font = name_font
141
    ws['D3'].alignment = b_r_alignment
142
    ws['D3'] = 'Period:'
143
    ws['E3'].border = b_border
144
    ws['E3'].alignment = b_c_alignment
145
    ws['E3'].font = name_font
146
    ws['E3'] = period_type
147
148
    ws['F3'].font = name_font
149
    ws['F3'].alignment = b_r_alignment
150
    ws['F3'] = 'Date:'
151
    ws['G3'].border = b_border
152
    ws['G3'].alignment = b_c_alignment
153
    ws['G3'].font = name_font
154
    ws['G3'] = reporting_start_datetime_local + "__" + reporting_end_datetime_local
155
    ws.merge_cells("G3:H3")
156
157
    if "reporting_period" not in report.keys() or \
158
            "names" not in report['reporting_period'].keys() or len(report['reporting_period']['names']) == 0:
159
        filename = str(uuid.uuid4()) + '.xlsx'
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable str does not seem to be defined.
Loading history...
160
        wb.save(filename)
161
162
        return filename
163
    #################################################
164
    # First: 统计分析
165
    # 6: title
166
    # 7: table title
167
    # 8~ca_len table_data
168
    #################################################
169
    reporting_period_data = report['reporting_period']
170
171
    has_energy_data_flag = True
172
173
    if "names" not in reporting_period_data.keys() or \
174
            reporting_period_data['names'] is None or \
175
            len(reporting_period_data['names']) == 0:
176
        has_energy_data_flag = False
177
178
        filename = str(uuid.uuid4()) + '.xlsx'
179
        wb.save(filename)
180
181
        return filename
182
183
    if has_energy_data_flag:
184
        ws['B6'].font = title_font
185
        ws['B6'] = name + ' 统计分析'
186
187
        category = reporting_period_data['names']
188
189
        # table_title
190
        ws['B7'].fill = table_fill
191
        ws['B7'].font = title_font
192
        ws['B7'].alignment = c_c_alignment
193
        ws['B7'] = '报告期'
194
        ws['B7'].border = f_border
195
196
        ws['C7'].font = title_font
197
        ws['C7'].alignment = c_c_alignment
198
        ws['C7'] = '算术平均数'
199
        ws['C7'].border = f_border
200
201
        ws['D7'].font = title_font
202
        ws['D7'].alignment = c_c_alignment
203
        ws['D7'] = '中位数'
204
        ws['D7'].border = f_border
205
206
        ws['E7'].font = title_font
207
        ws['E7'].alignment = c_c_alignment
208
        ws['E7'] = '最小值'
209
        ws['E7'].border = f_border
210
211
        ws['F7'].font = title_font
212
        ws['F7'].alignment = c_c_alignment
213
        ws['F7'] = '最大值'
214
        ws['F7'].border = f_border
215
216
        ws['G7'].font = title_font
217
        ws['G7'].alignment = c_c_alignment
218
        ws['G7'] = '样本标准差'
219
        ws['G7'].border = f_border
220
221
        ws['H7'].font = title_font
222
        ws['H7'].alignment = c_c_alignment
223
        ws['H7'] = '样本方差'
224
        ws['H7'].border = f_border
225
226
        # table_data
227
228
        for i, value in enumerate(category):
229
            row = i * 2 + 8
230
            ws['B' + str(row)].font = name_font
231
            ws['B' + str(row)].alignment = c_c_alignment
232
            ws['B' + str(row)] = reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + " )"
233
            ws['B' + str(row)].border = f_border
234
235
            ws['B' + str(row + 1)].font = name_font
236
            ws['B' + str(row + 1)].alignment = c_c_alignment
237
            ws['B' + str(row + 1)] = "环比"
238
            ws['B' + str(row + 1)].border = f_border
239
240
            ws['C' + str(row)].font = name_font
241
            ws['C' + str(row)].alignment = c_c_alignment
242
            ws['C' + str(row)] = reporting_period_data['means'][i] \
243
                if reporting_period_data['means'][i] is not None else ''
244
            ws['C' + str(row)].border = f_border
245
            ws['C' + str(row)].number_format = '0.00'
246
247
            ws['C' + str(row + 1)].font = name_font
248
            ws['C' + str(row + 1)].alignment = c_c_alignment
249
            ws['C' + str(row + 1)] = str(round(reporting_period_data['means_increment_rate'][i] * 100, 2)) + "%" \
250
                if reporting_period_data['means_increment_rate'][i] is not None else '0.00%'
251
            ws['C' + str(row + 1)].border = f_border
252
253
            ws['D' + str(row)].font = name_font
254
            ws['D' + str(row)].alignment = c_c_alignment
255
            ws['D' + str(row)] = reporting_period_data['medians'][i] \
256
                if reporting_period_data['medians'][i] is not None else ''
257
            ws['D' + str(row)].border = f_border
258
            ws['D' + str(row)].number_format = '0.00'
259
260
            ws['D' + str(row + 1)].font = name_font
261
            ws['D' + str(row + 1)].alignment = c_c_alignment
262
            ws['D' + str(row + 1)] = str(round(reporting_period_data['medians_increment_rate'][i] * 100, 2)) + "%" \
263
                if reporting_period_data['medians_increment_rate'][i] is not None else '0.00%'
264
            ws['D' + str(row + 1)].border = f_border
265
266
            ws['E' + str(row)].font = name_font
267
            ws['E' + str(row)].alignment = c_c_alignment
268
            ws['E' + str(row)] = reporting_period_data['minimums'][i] \
269
                if reporting_period_data['minimums'][i] is not None else ''
270
            ws['E' + str(row)].border = f_border
271
            ws['E' + str(row)].number_format = '0.00'
272
273
            ws['E' + str(row + 1)].font = name_font
274
            ws['E' + str(row + 1)].alignment = c_c_alignment
275
            ws['E' + str(row + 1)] = str(round(reporting_period_data['minimums_increment_rate'][i] * 100, 2)) + "%" \
276
                if reporting_period_data['minimums_increment_rate'][i] is not None else '0.00%'
277
            ws['E' + str(row + 1)].border = f_border
278
279
            ws['F' + str(row)].font = name_font
280
            ws['F' + str(row)].alignment = c_c_alignment
281
            ws['F' + str(row)] = reporting_period_data['maximums'][i] \
282
                if reporting_period_data['maximums'][i] is not None else ''
283
            ws['F' + str(row)].border = f_border
284
            ws['F' + str(row)].number_format = '0.00'
285
286
            ws['F' + str(row + 1)].font = name_font
287
            ws['F' + str(row + 1)].alignment = c_c_alignment
288
            ws['F' + str(row + 1)] = str(round(reporting_period_data['maximums_increment_rate'][i] * 100, 2)) + "%" \
289
                if reporting_period_data['maximums_increment_rate'][i] is not None else '0.00%'
290
            ws['F' + str(row + 1)].border = f_border
291
292
            ws['G' + str(row)].font = name_font
293
            ws['G' + str(row)].alignment = c_c_alignment
294
            ws['G' + str(row)] = reporting_period_data['stdevs'][i] \
295
                if reporting_period_data['stdevs'][i] is not None else ''
296
            ws['G' + str(row)].border = f_border
297
            ws['G' + str(row)].number_format = '0.00'
298
299
            ws['G' + str(row + 1)].font = name_font
300
            ws['G' + str(row + 1)].alignment = c_c_alignment
301
            ws['G' + str(row + 1)] = str(round(reporting_period_data['stdevs_increment_rate'][i] * 100, 2)) + "%" \
302
                if reporting_period_data['stdevs_increment_rate'][i] is not None else '0.00%'
303
            ws['G' + str(row + 1)].border = f_border
304
305
            ws['H' + str(row)].font = name_font
306
            ws['H' + str(row)].alignment = c_c_alignment
307
            ws['H' + str(row)] = reporting_period_data['variances'][i] \
308
                if reporting_period_data['variances'][i] is not None else ''
309
            ws['H' + str(row)].border = f_border
310
            ws['H' + str(row)].number_format = '0.00'
311
312
            ws['H' + str(row + 1)].font = name_font
313
            ws['H' + str(row + 1)].alignment = c_c_alignment
314
            ws['H' + str(row + 1)] = str(round(reporting_period_data['variances_increment_rate'][i] * 100, 2)) + "%" \
315
                if reporting_period_data['variances_increment_rate'][i] is not None else '0.00%'
316
            ws['H' + str(row + 1)].border = f_border
317
318
    ########################################################
319
    # Second: 详细数据
320
    # row_sat+1~ row_sat+1+row_lines+: line
321
    # row_ddt~ : the detailed data table
322
    # row_sat : the number of rows of the analysis table
323
    # row_lines : the number of rows of all line charts
324
    # row_ddt : The number of rows in which the detailed data table header resides
325
    ########################################################
326
    has_timestamps_flag = True
327
    if "timestamps" not in reporting_period_data.keys() or \
328
            reporting_period_data['timestamps'] is None or \
329
            len(reporting_period_data['timestamps']) == 0:
330
        has_timestamps_flag = False
331
332
    if has_timestamps_flag:
333
        timestamps = reporting_period_data['timestamps'][0]
334
        values = reporting_period_data['values']
335
        names = reporting_period_data['names']
336
        ca_len = len(names)
337
        time_len = len(timestamps)
338
        # the detailed title
339
        row_lines = 9 * ca_len
340
        row_sat = 7 + 2 * ca_len
341
        row_ddt = row_sat + row_lines + 2
342
343
        ws['B' + str(row_ddt)].font = title_font
344
        ws['B' + str(row_ddt)] = name + ' 详细数据'
345
        # the detailed table_title
346
        ws['B' + str(row_ddt+1)].fill = table_fill
347
        ws['B' + str(row_ddt+1)].font = name_font
348
        ws['B' + str(row_ddt+1)].alignment = c_c_alignment
349
        ws['B' + str(row_ddt+1)] = "时间"
350
        ws['B' + str(row_ddt+1)].border = f_border
351
352
        for i in range(0, ca_len):
353
            col = chr(ord('C') + i)
354
355
            ws[col + str(row_ddt+1)].font = name_font
356
            ws[col + str(row_ddt+1)].alignment = c_c_alignment
357
            ws[col + str(row_ddt+1)] = names[i] + " - (" + reporting_period_data['units'][i] + ")"
358
            ws[col + str(row_ddt+1)].border = f_border
359
        # the detailed table_date
360
        for i in range(0, time_len):
361
            rows = i + row_ddt + 2
362
363
            ws['B' + str(rows)].font = name_font
364
            ws['B' + str(rows)].alignment = c_c_alignment
365
            ws['B' + str(rows)] = timestamps[i]
366
            ws['B' + str(rows)].border = f_border
367
368
            for index in range(0, ca_len):
369
                col = chr(ord('C') + index)
370
371
                ws[col + str(rows)].font = name_font
372
                ws[col + str(rows)].alignment = c_c_alignment
373
                ws[col + str(rows)] = values[index][i]
374
                ws[col + str(rows)].number_format = '0.00'
375
                ws[col + str(rows)].border = f_border
376
377
        # 小计
378
        row_subtotals = row_ddt + 2 + time_len
379
        ws['B' + str(row_subtotals)].font = name_font
380
        ws['B' + str(row_subtotals)].alignment = c_c_alignment
381
        ws['B' + str(row_subtotals)] = "小计"
382
        ws['B' + str(row_subtotals)].border = f_border
383
384
        for i in range(0, ca_len):
385
            col = chr(ord('C') + i)
386
387
            ws[col + str(row_subtotals)].font = name_font
388
            ws[col + str(row_subtotals)].alignment = c_c_alignment
389
            ws[col + str(row_subtotals)] = reporting_period_data['subtotals'][i]
390
            ws[col + str(row_subtotals)].border = f_border
391
            ws[col + str(row_subtotals)].number_format = '0.00'
392
393
        # LineChart
394
        for i in range(0, ca_len):
395
396
            lc = LineChart()
397
            lc.title = "报告期消耗" + " - " + names[i] + "(" + reporting_period_data['units'][i] + ")"
398
            lc.style = 10
399
            lc.height = 8.40  # cm 1.05*8 1.05cm = 30 pt
400
            lc.width = 23.28
401
            lc.x_axis.majorTickMark = 'in'
402
            lc.y_axis.majorTickMark = 'in'
403
            times = Reference(ws, min_col=2, min_row=row_ddt + 2,
404
                              max_row=row_ddt + 2 + time_len)
405
            lc_data = Reference(ws, min_col=3 + i, min_row=row_ddt + 1,
406
                                max_row=row_ddt + 1 + time_len)
407
            lc.add_data(lc_data, titles_from_data=True)
408
            lc.set_categories(times)
409
            ser = lc.series[0]
410
            ser.marker.symbol = "diamond"
411
            ser.marker.size = 5
412
            ws.add_chart(lc, 'B' + str(row_sat + 2 + 9 * i))
413
414
    filename = str(uuid.uuid4()) + '.xlsx'
415
    wb.save(filename)
416
417
    return filename
418