Passed
Push — master ( 414b30...be43ee )
by Guangyu
14:19 queued 13s
created

generate_excel()   F

Complexity

Conditions 102

Size

Total Lines 1100
Code Lines 869

Duplication

Lines 176
Ratio 16 %

Importance

Changes 0
Metric Value
eloc 869
dl 176
loc 1100
rs 0
c 0
b 0
f 0
cc 102
nop 8

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like excelexporters.combinedequipmentefficiency.generate_excel() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
import base64
2
import gettext
3
import os
4
import re
5
import uuid
6
7
import openpyxl.utils.cell as format_cell
8
from openpyxl import Workbook
9
from openpyxl.chart import LineChart, Reference
10
from openpyxl.chart.label import DataLabelList
11
from openpyxl.drawing.image import Image
12
from openpyxl.styles import PatternFill, Border, Side, Alignment, Font
13
14
15
########################################################################################################################
16
# PROCEDURES
17
# Step 1: Validate the report data
18
# Step 2: Generate excel file
19
# Step 3: Encode the excel file bytes to Base64
20
########################################################################################################################
21
22
23 View Code Duplication
def export(report,
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
24
           name,
25
           base_period_start_datetime_local,
26
           base_period_end_datetime_local,
27
           reporting_start_datetime_local,
28
           reporting_end_datetime_local,
29
           period_type,
30
           language):
31
    ####################################################################################################################
32
    # Step 1: Validate the report data
33
    ####################################################################################################################
34
    if report is None:
35
        return None
36
37
    ####################################################################################################################
38
    # Step 2: Generate excel file from the report data
39
    ####################################################################################################################
40
    filename = generate_excel(report,
41
                              name,
42
                              base_period_start_datetime_local,
43
                              base_period_end_datetime_local,
44
                              reporting_start_datetime_local,
45
                              reporting_end_datetime_local,
46
                              period_type,
47
                              language)
48
    ####################################################################################################################
49
    # Step 3: Encode the excel file to Base64
50
    ####################################################################################################################
51
    binary_file_data = b''
52
    try:
53
        with open(filename, 'rb') as binary_file:
54
            binary_file_data = binary_file.read()
55
    except IOError as ex:
56
        print(str(ex))
57
58
    # Base64 encode the bytes
59
    base64_encoded_data = base64.b64encode(binary_file_data)
60
    # get the Base64 encoded data using human-readable characters.
61
    base64_message = base64_encoded_data.decode('utf-8')
62
    # delete the file from server
63
    try:
64
        os.remove(filename)
65
    except NotImplementedError as ex:
66
        print(str(ex))
67
    return base64_message
68
69
70
def generate_excel(report,
71
                   name,
72
                   base_period_start_datetime_local,
73
                   base_period_end_datetime_local,
74
                   reporting_start_datetime_local,
75
                   reporting_end_datetime_local,
76
                   period_type,
77
                   language):
78
79
    locale_path = './i18n/'
80
    if language == 'zh_CN':
81
        trans = gettext.translation('myems', locale_path, languages=['zh_CN'])
82
    elif language == 'de':
83
        trans = gettext.translation('myems', locale_path, languages=['de'])
84
    elif language == 'en':
85
        trans = gettext.translation('myems', locale_path, languages=['en'])
86
    else:
87
        trans = gettext.translation('myems', locale_path, languages=['en'])
88
    trans.install()
89
    _ = trans.gettext
90
91
    wb = Workbook()
92
    ws = wb.active
93
    ws.title = "CombinedEquipmentEfficiency"
94
95
    # Row height
96
    ws.row_dimensions[1].height = 102
97
    for i in range(2, 2000 + 1):
98
        ws.row_dimensions[i].height = 42
99
100
    # Col width
101
    ws.column_dimensions['A'].width = 1.5
102
103
    ws.column_dimensions['B'].width = 25.0
104
105
    for i in range(ord('C'), ord('Z')):
106
        ws.column_dimensions[chr(i)].width = 15.0
107
108
    # Font
109
    name_font = Font(name='Arial', size=15, bold=True)
110
    title_font = Font(name='Arial', size=15, bold=True)
111
112
    table_fill = PatternFill(fill_type='solid', fgColor='90ee90')
113
    f_border = Border(left=Side(border_style='medium'),
114
                      right=Side(border_style='medium'),
115
                      bottom=Side(border_style='medium'),
116
                      top=Side(border_style='medium')
117
                      )
118
    b_border = Border(
119
        bottom=Side(border_style='medium'),
120
    )
121
122
    b_c_alignment = Alignment(vertical='bottom',
123
                              horizontal='center',
124
                              text_rotation=0,
125
                              wrap_text=True,
126
                              shrink_to_fit=False,
127
                              indent=0)
128
    c_c_alignment = Alignment(vertical='center',
129
                              horizontal='center',
130
                              text_rotation=0,
131
                              wrap_text=True,
132
                              shrink_to_fit=False,
133
                              indent=0)
134
    b_r_alignment = Alignment(vertical='bottom',
135
                              horizontal='right',
136
                              text_rotation=0,
137
                              wrap_text=True,
138
                              shrink_to_fit=False,
139
                              indent=0)
140
    # Img
141
    img = Image("excelexporters/myems.png")
142
    ws.add_image(img, 'A1')
143
144
    # Title
145
    ws['B3'].alignment = b_r_alignment
146
    ws['B3'] = _('Name') + ':'
147
    ws['C3'].border = b_border
148
    ws['C3'].alignment = b_c_alignment
149
    ws['C3'] = name
150
151
    ws['D3'].alignment = b_r_alignment
152
    ws['D3'] = _('Period Type') + ':'
153
    ws['E3'].border = b_border
154
    ws['E3'].alignment = b_c_alignment
155
    ws['E3'] = period_type
156
157
    ws['B4'].alignment = b_r_alignment
158
    ws['B4'] = _('Reporting Start Datetime') + ':'
159
    ws['C4'].border = b_border
160
    ws['C4'].alignment = b_c_alignment
161
    ws['C4'] = reporting_start_datetime_local
162
163
    ws['D4'].alignment = b_r_alignment
164
    ws['D4'] = _('Reporting End Datetime') + ':'
165
    ws['E4'].border = b_border
166
    ws['E4'].alignment = b_c_alignment
167
    ws['E4'] = reporting_end_datetime_local
168
169
    is_base_period_timestamp_exists_flag = is_base_period_timestamp_exists(report['base_period_efficiency'])
170
171
    if is_base_period_timestamp_exists_flag:
172
        ws['B5'].alignment = b_r_alignment
173
        ws['B5'] = _('Base Period Start Datetime') + ':'
174
        ws['C5'].border = b_border
175
        ws['C5'].alignment = b_c_alignment
176
        ws['C5'] = base_period_start_datetime_local
177
178
        ws['D5'].alignment = b_r_alignment
179
        ws['D5'] = _('Base Period End Datetime') + ':'
180
        ws['E5'].border = b_border
181
        ws['E5'].alignment = b_c_alignment
182
        ws['E5'] = base_period_end_datetime_local
183
184
    if "reporting_period_efficiency" not in report.keys() or \
185
            "names" not in report['reporting_period_efficiency'].keys() or len(
186
            report['reporting_period_efficiency']['names']) == 0:
187
        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...
188
        wb.save(filename)
189
190
        return filename
191
192
    ####################################################################################################################
193
194
    current_row_number = 7
195
    reporting_period_data = report['reporting_period_efficiency']
196
    if "names" not in reporting_period_data.keys() or \
197
            reporting_period_data['names'] is None or \
198
            len(reporting_period_data['names']) == 0:
199
        pass
200
    else:
201
        ws['B' + str(current_row_number)].font = title_font
202
        ws['B' + str(current_row_number)] = name + ' ' + _('Reporting Period Cumulative Efficiency')
203
204
        current_row_number += 1
205
206
        category = reporting_period_data['names']
207
        ca_len = len(category)
208
209
        ws.row_dimensions[current_row_number].height = 80
210
        ws['B' + str(current_row_number)].fill = table_fill
211
        ws['B' + str(current_row_number)].border = f_border
212
213
        col = 'C'
214
215
        for i in range(0, ca_len):
216
            ws[col + str(current_row_number)].fill = table_fill
217
            ws[col + str(current_row_number)].font = name_font
218
            ws[col + str(current_row_number)].alignment = c_c_alignment
219
            ws[col + str(current_row_number)].border = f_border
220
            ws[col + str(current_row_number)] = \
221
                reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
222
223
            col = chr(ord(col) + 1)
224
225
            ws[col + str(current_row_number)].fill = table_fill
226
            ws[col + str(current_row_number)].font = name_font
227
            ws[col + str(current_row_number)].alignment = c_c_alignment
228
            ws[col + str(current_row_number)].border = f_border
229
            ws[col + str(current_row_number)] = \
230
                reporting_period_data['names'][i] + '-' + reporting_period_data['numerator_names'][i] + " (" + \
231
                reporting_period_data['numerator_units'][i] + ")"
232
233
            col = chr(ord(col) + 1)
234
235
            ws[col + str(current_row_number)].fill = table_fill
236
            ws[col + str(current_row_number)].font = name_font
237
            ws[col + str(current_row_number)].alignment = c_c_alignment
238
            ws[col + str(current_row_number)].border = f_border
239
            ws[col + str(current_row_number)] = \
240
                reporting_period_data['names'][i] + '-' + reporting_period_data['denominator_names'][i] + " (" + \
241
                reporting_period_data['denominator_units'][i] + ")"
242
243
            col = chr(ord(col) + 1)
244
245
        current_row_number += 1
246
247
        ws['B' + str(current_row_number)].font = title_font
248
        ws['B' + str(current_row_number)].alignment = c_c_alignment
249
        ws['B' + str(current_row_number)].border = f_border
250
        ws['B' + str(current_row_number)] = _('Cumulative Efficiency')
251
252
        col = 'C'
253
254
        for i in range(0, ca_len):
255
            ws[col + str(current_row_number)].font = name_font
256
            ws[col + str(current_row_number)].alignment = c_c_alignment
257
            ws[col + str(current_row_number)].border = f_border
258
            ws[col + str(current_row_number)] = round(reporting_period_data['cumulations'][i], 2)
259
260
            col = chr(ord(col) + 1)
261
262
            ws[col + str(current_row_number)].font = name_font
263
            ws[col + str(current_row_number)].alignment = c_c_alignment
264
            ws[col + str(current_row_number)].border = f_border
265
            ws[col + str(current_row_number)] = round(reporting_period_data['numerator_cumulations'][i], 2)
266
267
            col = chr(ord(col) + 1)
268
269
            ws[col + str(current_row_number)].font = name_font
270
            ws[col + str(current_row_number)].alignment = c_c_alignment
271
            ws[col + str(current_row_number)].border = f_border
272
            ws[col + str(current_row_number)] = round(reporting_period_data['denominator_cumulations'][i], 2)
273
274
            col = chr(ord(col) + 1)
275
276
        current_row_number += 1
277
278
        ws['B' + str(current_row_number)].font = title_font
279
        ws['B' + str(current_row_number)].alignment = c_c_alignment
280
        ws['B' + str(current_row_number)].border = f_border
281
        ws['B' + str(current_row_number)] = _('Increment Rate')
282
283
        col = 'C'
284
285
        for i in range(0, ca_len):
286
            ws[col + str(current_row_number)].font = name_font
287
            ws[col + str(current_row_number)].alignment = c_c_alignment
288
            ws[col + str(current_row_number)].border = f_border
289
            ws[col + str(current_row_number)] = str(
290
                round(reporting_period_data['increment_rates'][i] * 100, 2)) + '%' \
291
                if reporting_period_data['increment_rates'][i] is not None else '-'
292
293
            col = chr(ord(col) + 1)
294
295
            ws[col + str(current_row_number)].font = name_font
296
            ws[col + str(current_row_number)].alignment = c_c_alignment
297
            ws[col + str(current_row_number)].border = f_border
298
            ws[col + str(current_row_number)] = str(
299
                round(reporting_period_data['increment_rates_num'][i] * 100, 2)) + '%' \
300
                if reporting_period_data['increment_rates_num'][i] is not None else '-'
301
302
            col = chr(ord(col) + 1)
303
304
            ws[col + str(current_row_number)].font = name_font
305
            ws[col + str(current_row_number)].alignment = c_c_alignment
306
            ws[col + str(current_row_number)].border = f_border
307
            ws[col + str(current_row_number)] = str(
308
                round(reporting_period_data['increment_rates_den'][i] * 100, 2)) + '%' \
309
                if reporting_period_data['increment_rates_den'][i] is not None else '-'
310
311
            col = chr(ord(col) + 1)
312
313
        current_row_number += 2
314
315
    ####################################################################################################################
316
317
    has_parameters_names_and_timestamps_and_values_data = True
318
    current_sheet_parameters_row_number = current_row_number
319
320 View Code Duplication
    if 'parameters' not in report.keys() or \
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
321
            report['parameters'] is None or \
322
            'names' not in report['parameters'].keys() or \
323
            report['parameters']['names'] is None or \
324
            len(report['parameters']['names']) == 0 or \
325
            'timestamps' not in report['parameters'].keys() or \
326
            report['parameters']['timestamps'] is None or \
327
            len(report['parameters']['timestamps']) == 0 or \
328
            'values' not in report['parameters'].keys() or \
329
            report['parameters']['values'] is None or \
330
            len(report['parameters']['values']) == 0 or \
331
            timestamps_data_all_equal_0(report['parameters']['timestamps']):
332
333
        has_parameters_names_and_timestamps_and_values_data = False
334
335
    ####################################################################################################################
336
    has_values_data = True
337
    has_timestamps_data = True
338
339
    if 'values' not in reporting_period_data.keys() or \
340
            reporting_period_data['values'] is None or \
341
            len(reporting_period_data['values']) == 0:
342
        has_values_data = False
343
344
    if 'timestamps' not in reporting_period_data.keys() or \
345
            reporting_period_data['timestamps'] is None or \
346
            len(reporting_period_data['timestamps']) == 0 or \
347
            len(reporting_period_data['timestamps'][0]) == 0:
348
        has_timestamps_data = False
349
350
    if not is_base_period_timestamp_exists_flag:
351
        if has_values_data and has_timestamps_data:
352
            ca_len = len(reporting_period_data['names'])
353
            time = reporting_period_data['timestamps'][0]
354
355
            ws['B' + str(current_row_number)].font = title_font
356
            ws['B' + str(current_row_number)] = name + ' ' + _('Reporting Period Cumulative Efficiency')
357
358
            current_row_number += 1
359
360
            chart_start_row_number = current_row_number
361
362
            current_row_number += ca_len * 3 * 6 + 1
363
364
            if has_parameters_names_and_timestamps_and_values_data:
365
                current_sheet_parameters_row_number = current_row_number
366
                real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps'])
367
                current_row_number += 6 * real_timestamps_len + 2
368
369
            ws['B' + str(current_row_number)].font = title_font
370
            ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data')
371
372
            current_row_number += 1
373
374
            table_start_row_number = current_row_number
375
376
            ws.row_dimensions[current_row_number].height = 85
377
            current_col_number = 2
378
            col = format_cell.get_column_letter(current_col_number)
379
            ws[col + str(current_row_number)].fill = table_fill
380
            ws[col + str(current_row_number)].font = title_font
381
            ws[col + str(current_row_number)].alignment = c_c_alignment
382
            ws[col + str(current_row_number)].border = f_border
383
            ws[col + str(current_row_number)] = _('Datetime')
384
385
            for i in range(0, ca_len):
386
                current_col_number += 1
387
                col = format_cell.get_column_letter(current_col_number)
388
389
                ws[col + str(current_row_number)].fill = table_fill
390
                ws[col + str(current_row_number)].font = title_font
391
                ws[col + str(current_row_number)].alignment = c_c_alignment
392
                ws[col + str(current_row_number)].border = f_border
393
                ws[col + str(current_row_number)] = \
394
                    reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
395
396
                current_col_number += 1
397
                col = format_cell.get_column_letter(current_col_number)
398
399
                ws[col + str(current_row_number)].fill = table_fill
400
                ws[col + str(current_row_number)].font = title_font
401
                ws[col + str(current_row_number)].alignment = c_c_alignment
402
                ws[col + str(current_row_number)].border = f_border
403
                ws[col + str(current_row_number)] = \
404
                    reporting_period_data['names'][i] + "-" + \
405
                    reporting_period_data['numerator_names'][i] + " (" + reporting_period_data['numerator_units'][
406
                        i] + ")"
407
408
                current_col_number += 1
409
                col = format_cell.get_column_letter(current_col_number)
410
411
                ws[col + str(current_row_number)].fill = table_fill
412
                ws[col + str(current_row_number)].font = title_font
413
                ws[col + str(current_row_number)].alignment = c_c_alignment
414
                ws[col + str(current_row_number)].border = f_border
415
                ws[col + str(current_row_number)] = \
416
                    reporting_period_data['names'][i] + "-" + \
417
                    reporting_period_data['denominator_names'][i] + " (" + reporting_period_data['denominator_units'][
418
                        i] + ")"
419
420
            current_row_number += 1
421
422
            for i in range(0, len(time)):
423
                current_col_number = 2
424
                col = format_cell.get_column_letter(current_col_number)
425
                ws[col + str(current_row_number)].font = title_font
426
                ws[col + str(current_row_number)].alignment = c_c_alignment
427
                ws[col + str(current_row_number)].border = f_border
428
                ws[col + str(current_row_number)] = time[i]
429
430
                for j in range(0, ca_len):
431
                    current_col_number += 1
432
                    col = format_cell.get_column_letter(current_col_number)
433
434
                    ws[col + str(current_row_number)].font = title_font
435
                    ws[col + str(current_row_number)].alignment = c_c_alignment
436
                    ws[col + str(current_row_number)].border = f_border
437
                    ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
438
                        if reporting_period_data['values'][j][i] is not None else None
439
440
                    current_col_number += 1
441
                    col = format_cell.get_column_letter(current_col_number)
442
443
                    ws[col + str(current_row_number)].font = title_font
444
                    ws[col + str(current_row_number)].alignment = c_c_alignment
445
                    ws[col + str(current_row_number)].border = f_border
446
                    ws[col + str(current_row_number)] = round(reporting_period_data['numerator_values'][j][i], 2) \
447
                        if reporting_period_data['numerator_values'][j][i] is not None else None
448
449
                    current_col_number += 1
450
                    col = format_cell.get_column_letter(current_col_number)
451
452
                    ws[col + str(current_row_number)].font = title_font
453
                    ws[col + str(current_row_number)].alignment = c_c_alignment
454
                    ws[col + str(current_row_number)].border = f_border
455
                    ws[col + str(current_row_number)] = round(reporting_period_data['denominator_values'][j][i], 2) \
456
                        if reporting_period_data['denominator_values'][j][i] is not None else None
457
458
                current_row_number += 1
459
460
            table_end_row_number = current_row_number - 1
461
462
            current_col_number = 2
463
            col = format_cell.get_column_letter(current_col_number)
464
            ws[col + str(current_row_number)].font = title_font
465
            ws[col + str(current_row_number)].alignment = c_c_alignment
466
            ws[col + str(current_row_number)].border = f_border
467
            ws[col + str(current_row_number)] = _('Subtotal')
468
469
            for i in range(0, ca_len):
470
                current_col_number += 1
471
                col = format_cell.get_column_letter(current_col_number)
472
473
                ws[col + str(current_row_number)].font = title_font
474
                ws[col + str(current_row_number)].alignment = c_c_alignment
475
                ws[col + str(current_row_number)].border = f_border
476
                ws[col + str(current_row_number)] = round(reporting_period_data['cumulations'][i], 2) \
477
                    if reporting_period_data['cumulations'][i] is not None else None
478
479
                current_col_number += 1
480
                col = format_cell.get_column_letter(current_col_number)
481
482
                ws[col + str(current_row_number)].font = title_font
483
                ws[col + str(current_row_number)].alignment = c_c_alignment
484
                ws[col + str(current_row_number)].border = f_border
485
                ws[col + str(current_row_number)] = round(reporting_period_data['numerator_cumulations'][i], 2) \
486
                    if reporting_period_data['numerator_cumulations'][i] is not None else None
487
488
                current_col_number += 1
489
                col = format_cell.get_column_letter(current_col_number)
490
491
                ws[col + str(current_row_number)].font = title_font
492
                ws[col + str(current_row_number)].alignment = c_c_alignment
493
                ws[col + str(current_row_number)].border = f_border
494
                ws[col + str(current_row_number)] = round(reporting_period_data['denominator_cumulations'][i], 2) \
495
                    if reporting_period_data['denominator_cumulations'][i] is not None else None
496
497
            current_row_number += 2
498
499
            for i in range(0, ca_len):
500
                line = LineChart()
501
                line.title = _('Reporting Period Cumulative Efficiency') + ' - ' + \
502
                    reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
503
                labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
504
                line_data = Reference(ws, min_col=3 + i * 3, min_row=table_start_row_number,
505
                                      max_row=table_end_row_number)
506
                line.add_data(line_data, titles_from_data=True)
507
                line.set_categories(labels)
508
                line_data = line.series[0]
509
                line_data.marker.symbol = "circle"
510
                line_data.smooth = True
511
                line.x_axis.crosses = 'min'
512
                line.height = 8.25
513
                line.width = 24
514
                line.dLbls = DataLabelList()
515
                line.dLbls.dLblPos = 't'
516
                line.dLbls.showVal = True
517
                line.dLbls.showPercent = False
518
                chart_col = 'B'
519
                chart_cell = chart_col + str(chart_start_row_number)
520
                chart_start_row_number += 6
521
                ws.add_chart(line, chart_cell)
522
523
                line = LineChart()
524
                line.title = _('Reporting Period Cumulative Efficiency') + ' - ' + \
525
                    reporting_period_data['names'][i] + "-" + \
526
                    reporting_period_data['numerator_names'][i] \
527
                    + " (" + reporting_period_data['numerator_units'][i] + ")"
528
                labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
529
                line_data = Reference(ws, min_col=4 + i * 3, min_row=table_start_row_number,
530
                                      max_row=table_end_row_number)
531
                line.add_data(line_data, titles_from_data=True)
532
                line.set_categories(labels)
533
                line_data = line.series[0]
534
                line_data.marker.symbol = "circle"
535
                line_data.smooth = True
536
                line.x_axis.crosses = 'min'
537
                line.height = 8.25
538
                line.width = 24
539
                line.dLbls = DataLabelList()
540
                line.dLbls.dLblPos = 't'
541
                line.dLbls.showVal = True
542
                line.dLbls.showPercent = False
543
                chart_col = 'B'
544
                chart_cell = chart_col + str(chart_start_row_number)
545
                chart_start_row_number += 6
546
                ws.add_chart(line, chart_cell)
547
548
                line = LineChart()
549
                line.title = _('Reporting Period Cumulative Efficiency') + ' - ' + \
550
                    reporting_period_data['names'][i] + "-" + \
551
                    reporting_period_data['denominator_names'][i] \
552
                    + " (" + reporting_period_data['denominator_units'][i] + ")"
553
                labels = Reference(ws, min_col=2, min_row=table_start_row_number + 1, max_row=table_end_row_number)
554
                line_data = Reference(ws, min_col=5 + i * 3, min_row=table_start_row_number,
555
                                      max_row=table_end_row_number)
556
                line.add_data(line_data, titles_from_data=True)
557
                line.set_categories(labels)
558
                line_data = line.series[0]
559
                line_data.marker.symbol = "circle"
560
                line_data.smooth = True
561
                line.x_axis.crosses = 'min'
562
                line.height = 8.25
563
                line.width = 24
564
                line.dLbls = DataLabelList()
565
                line.dLbls.dLblPos = 't'
566
                line.dLbls.showVal = True
567
                line.dLbls.showPercent = False
568
                chart_col = 'B'
569
                chart_cell = chart_col + str(chart_start_row_number)
570
                chart_start_row_number += 6
571
                ws.add_chart(line, chart_cell)
572
    else:
573
        if has_values_data and has_timestamps_data:
574
            base_period_data = report['base_period_efficiency']
575
            reporting_period_data = report['reporting_period_efficiency']
576
            base_period_timestamps = base_period_data['timestamps']
577
            reporting_period_timestamps = reporting_period_data['timestamps']
578
            # Tip:
579
            #     base_period_data['names'] == reporting_period_data['names']
580
            #     base_period_data['units'] == reporting_period_data['units']
581
            reporting_period_data_ca_len = len(reporting_period_data['names'])
582
            base_period_data_ca_len = reporting_period_data_ca_len
583
            ws['B' + str(current_row_number)].font = title_font
584
            ws['B' + str(current_row_number)] = name + ' ' + _('Reporting Period Cumulative Efficiency')
585
586
            current_row_number += 1
587
588
            chart_start_row_number = current_row_number
589
590
            current_row_number += reporting_period_data_ca_len * 3 * 6 + 1
591
592
            if has_parameters_names_and_timestamps_and_values_data:
593
                current_sheet_parameters_row_number = current_row_number
594
                real_timestamps_len = timestamps_data_not_equal_0(report['parameters']['timestamps'])
595
                current_row_number += 6 * real_timestamps_len + 2
596
597
            ws['B' + str(current_row_number)].font = title_font
598
            ws['B' + str(current_row_number)] = name + ' ' + _('Detailed Data')
599
600
            current_row_number += 1
601
602
            table_start_row_number = current_row_number
603
604
            ws.row_dimensions[current_row_number].height = 85
605
            current_col_number = 2
606
            col = format_cell.get_column_letter(current_col_number)
607
            ws[col + str(current_row_number)].fill = table_fill
608
            ws[col + str(current_row_number)].font = title_font
609
            ws[col + str(current_row_number)].alignment = c_c_alignment
610
            ws[col + str(current_row_number)].border = f_border
611
            ws[col + str(current_row_number)] = _('Base Period') + " - " + _('Datetime')
612
613
            for i in range(0, base_period_data_ca_len):
614
                current_col_number += 1
615
                col = format_cell.get_column_letter(current_col_number)
616
617
                ws[col + str(current_row_number)].fill = table_fill
618
                ws[col + str(current_row_number)].font = title_font
619
                ws[col + str(current_row_number)].alignment = c_c_alignment
620
                ws[col + str(current_row_number)] = _('Base Period') + " - " + reporting_period_data['names'][i] + \
621
                    " (" + reporting_period_data['units'][i] + ")"
622
                ws[col + str(current_row_number)].border = f_border
623
624
                current_col_number += 1
625
                col = format_cell.get_column_letter(current_col_number)
626
627
                ws[col + str(current_row_number)].fill = table_fill
628
                ws[col + str(current_row_number)].font = title_font
629
                ws[col + str(current_row_number)].alignment = c_c_alignment
630
                ws[col + str(current_row_number)].border = f_border
631
                ws[col + str(current_row_number)] = _('Base Period') + " - " + \
632
                    reporting_period_data['names'][i] + "-" + \
633
                    reporting_period_data['numerator_names'][i] + " (" + \
634
                    reporting_period_data['numerator_units'][
635
                    i] + ")"
636
637
                current_col_number += 1
638
                col = format_cell.get_column_letter(current_col_number)
639
640
                ws[col + str(current_row_number)].fill = table_fill
641
                ws[col + str(current_row_number)].font = title_font
642
                ws[col + str(current_row_number)].alignment = c_c_alignment
643
                ws[col + str(current_row_number)].border = f_border
644
                ws[col + str(current_row_number)] = _('Base Period') + " - " + \
645
                    reporting_period_data['names'][i] + "-" + \
646
                    reporting_period_data['denominator_names'][i] + " (" + \
647
                    reporting_period_data['denominator_units'][
648
                    i] + ")"
649
650
            current_col_number += 1
651
            col = format_cell.get_column_letter(current_col_number)
652
653
            ws[col + str(current_row_number)].fill = table_fill
654
            ws[col + str(current_row_number)].font = title_font
655
            ws[col + str(current_row_number)].border = f_border
656
            ws[col + str(current_row_number)].alignment = c_c_alignment
657
            ws[col + str(current_row_number)] = _('Reporting Period') + " - " + _('Datetime')
658
659
            for i in range(0, reporting_period_data_ca_len):
660
                current_col_number += 1
661
                col = format_cell.get_column_letter(current_col_number)
662
                ws[col + str(current_row_number)].fill = table_fill
663
                ws[col + str(current_row_number)].font = title_font
664
                ws[col + str(current_row_number)].alignment = c_c_alignment
665
                ws[col + str(current_row_number)] = _('Reporting Period') + " - " \
666
                    + reporting_period_data['names'][i] + " (" + \
667
                    reporting_period_data['units'][i] + ")"
668
                ws[col + str(current_row_number)].border = f_border
669
670
                current_col_number += 1
671
                col = format_cell.get_column_letter(current_col_number)
672
673
                ws[col + str(current_row_number)].fill = table_fill
674
                ws[col + str(current_row_number)].font = title_font
675
                ws[col + str(current_row_number)].alignment = c_c_alignment
676
                ws[col + str(current_row_number)].border = f_border
677
                ws[col + str(current_row_number)] = _('Reporting Period') + " - " + \
678
                    reporting_period_data['names'][i] + "-" + \
679
                    reporting_period_data['numerator_names'][i] + " (" + \
680
                    reporting_period_data['numerator_units'][i] + ")"
681
682
                current_col_number += 1
683
                col = format_cell.get_column_letter(current_col_number)
684
685
                ws[col + str(current_row_number)].fill = table_fill
686
                ws[col + str(current_row_number)].font = title_font
687
                ws[col + str(current_row_number)].alignment = c_c_alignment
688
                ws[col + str(current_row_number)].border = f_border
689
                ws[col + str(current_row_number)] = _('Reporting Period') + " - " + \
690
                    reporting_period_data['names'][i] + "-" + \
691
                    reporting_period_data['denominator_names'][i] + " (" + \
692
                    reporting_period_data['denominator_units'][i] + ")"
693
694
            current_row_number += 1
695
696
            max_timestamps_len = len(base_period_timestamps[0]) \
697
                if len(base_period_timestamps[0]) >= len(reporting_period_timestamps[0]) \
698
                else len(reporting_period_timestamps[0])
699
700
            for i in range(0, max_timestamps_len):
701
                current_col_number = 2
702
                col = format_cell.get_column_letter(current_col_number)
703
                ws[col + str(current_row_number)].font = title_font
704
                ws[col + str(current_row_number)].alignment = c_c_alignment
705
                ws[col + str(current_row_number)] = base_period_timestamps[0][i] \
706
                    if i < len(base_period_timestamps[0]) else None
707
                ws[col + str(current_row_number)].border = f_border
708
709
                for j in range(0, base_period_data_ca_len):
710
                    current_col_number += 1
711
                    col = format_cell.get_column_letter(current_col_number)
712
713
                    ws[col + str(current_row_number)].font = title_font
714
                    ws[col + str(current_row_number)].alignment = c_c_alignment
715
                    ws[col + str(current_row_number)] = round(base_period_data['values'][j][i], 2) \
716
                        if i < len(base_period_data['values'][j]) \
717
                        and base_period_data['values'][j][i] is not None else None
718
                    ws[col + str(current_row_number)].border = f_border
719
720
                    current_col_number += 1
721
                    col = format_cell.get_column_letter(current_col_number)
722
723
                    ws[col + str(current_row_number)].font = title_font
724
                    ws[col + str(current_row_number)].alignment = c_c_alignment
725
                    ws[col + str(current_row_number)] = round(base_period_data['numerator_values'][j][i], 2) \
726
                        if i < len(base_period_data['numerator_values'][j]) \
727
                        and base_period_data['numerator_values'][j][i] is not None else None
728
                    ws[col + str(current_row_number)].border = f_border
729
730
                    current_col_number += 1
731
                    col = format_cell.get_column_letter(current_col_number)
732
733
                    ws[col + str(current_row_number)].font = title_font
734
                    ws[col + str(current_row_number)].alignment = c_c_alignment
735
                    ws[col + str(current_row_number)] = round(base_period_data['denominator_values'][j][i], 2) \
736
                        if i < len(base_period_data['denominator_values'][j]) \
737
                        and base_period_data['denominator_values'][j][i] is not None else None
738
                    ws[col + str(current_row_number)].border = f_border
739
                current_col_number += 1
740
                col = format_cell.get_column_letter(current_col_number)
741
742
                ws[col + str(current_row_number)].font = title_font
743
                ws[col + str(current_row_number)].alignment = c_c_alignment
744
                ws[col + str(current_row_number)] = reporting_period_timestamps[0][i] \
745
                    if i < len(reporting_period_timestamps[0]) else None
746
                ws[col + str(current_row_number)].border = f_border
747
748
                for j in range(0, reporting_period_data_ca_len):
749
                    current_col_number += 1
750
                    col = format_cell.get_column_letter(current_col_number)
751
752
                    ws[col + str(current_row_number)].font = title_font
753
                    ws[col + str(current_row_number)].alignment = c_c_alignment
754
                    ws[col + str(current_row_number)] = round(reporting_period_data['values'][j][i], 2) \
755
                        if i < len(reporting_period_data['values'][j]) \
756
                        and reporting_period_data['values'][j][i] is not None else None
757
                    ws[col + str(current_row_number)].border = f_border
758
759
                    current_col_number += 1
760
                    col = format_cell.get_column_letter(current_col_number)
761
762
                    ws[col + str(current_row_number)].font = title_font
763
                    ws[col + str(current_row_number)].alignment = c_c_alignment
764
                    ws[col + str(current_row_number)] = round(reporting_period_data['numerator_values'][j][i], 2) \
765
                        if i < len(reporting_period_data['numerator_values'][j]) \
766
                        and reporting_period_data['numerator_values'][j][i] is not None else None
767
                    ws[col + str(current_row_number)].border = f_border
768
769
                    current_col_number += 1
770
                    col = format_cell.get_column_letter(current_col_number)
771
772
                    ws[col + str(current_row_number)].font = title_font
773
                    ws[col + str(current_row_number)].alignment = c_c_alignment
774
                    ws[col + str(current_row_number)] = round(reporting_period_data['denominator_values'][j][i], 2) \
775
                        if i < len(reporting_period_data['denominator_values'][j]) \
776
                        and reporting_period_data['denominator_values'][j][i] is not None else None
777
                    ws[col + str(current_row_number)].border = f_border
778
779
                current_row_number += 1
780
781
            current_col_number = 2
782
            col = format_cell.get_column_letter(current_col_number)
783
784
            ws[col + str(current_row_number)].font = title_font
785
            ws[col + str(current_row_number)].alignment = c_c_alignment
786
            ws[col + str(current_row_number)] = _('Subtotal')
787
            ws[col + str(current_row_number)].border = f_border
788
789
            for i in range(0, base_period_data_ca_len):
790
                current_col_number += 1
791
                col = format_cell.get_column_letter(current_col_number)
792
                ws[col + str(current_row_number)].font = title_font
793
                ws[col + str(current_row_number)].alignment = c_c_alignment
794
                ws[col + str(current_row_number)] = round(base_period_data['cumulations'][i], 2) \
795
                    if base_period_data['cumulations'][i] is not None else None
796
                ws[col + str(current_row_number)].border = f_border
797
798
                current_col_number += 1
799
                col = format_cell.get_column_letter(current_col_number)
800
                ws[col + str(current_row_number)].font = title_font
801
                ws[col + str(current_row_number)].alignment = c_c_alignment
802
                ws[col + str(current_row_number)] = round(base_period_data['numerator_cumulations'][i], 2) \
803
                    if base_period_data['numerator_cumulations'][i] is not None else None
804
                ws[col + str(current_row_number)].border = f_border
805
806
                current_col_number += 1
807
                col = format_cell.get_column_letter(current_col_number)
808
                ws[col + str(current_row_number)].font = title_font
809
                ws[col + str(current_row_number)].alignment = c_c_alignment
810
                ws[col + str(current_row_number)] = round(base_period_data['denominator_cumulations'][i], 2) \
811
                    if base_period_data['denominator_cumulations'][i] is not None else None
812
                ws[col + str(current_row_number)].border = f_border
813
814
            current_col_number += 1
815
            col = format_cell.get_column_letter(current_col_number)
816
817
            ws[col + str(current_row_number)].font = title_font
818
            ws[col + str(current_row_number)].alignment = c_c_alignment
819
            ws[col + str(current_row_number)] = _('Subtotal')
820
            ws[col + str(current_row_number)].border = f_border
821
822
            for i in range(0, reporting_period_data_ca_len):
823
                current_col_number += 1
824
                col = format_cell.get_column_letter(current_col_number)
825
                ws[col + str(current_row_number)].font = title_font
826
                ws[col + str(current_row_number)].alignment = c_c_alignment
827
                ws[col + str(current_row_number)] = round(reporting_period_data['cumulations'][i], 2) \
828
                    if reporting_period_data['cumulations'][i] is not None else None
829
                ws[col + str(current_row_number)].border = f_border
830
831
                current_col_number += 1
832
                col = format_cell.get_column_letter(current_col_number)
833
                ws[col + str(current_row_number)].font = title_font
834
                ws[col + str(current_row_number)].alignment = c_c_alignment
835
                ws[col + str(current_row_number)] = round(reporting_period_data['numerator_cumulations'][i], 2) \
836
                    if reporting_period_data['numerator_cumulations'][i] is not None else None
837
                ws[col + str(current_row_number)].border = f_border
838
839
                current_col_number += 1
840
                col = format_cell.get_column_letter(current_col_number)
841
                ws[col + str(current_row_number)].font = title_font
842
                ws[col + str(current_row_number)].alignment = c_c_alignment
843
                ws[col + str(current_row_number)] = round(reporting_period_data['denominator_cumulations'][i], 2) \
844
                    if reporting_period_data['denominator_cumulations'][i] is not None else None
845
                ws[col + str(current_row_number)].border = f_border
846
847
            current_row_number += 2
848
849
            for i in range(0, reporting_period_data_ca_len):
850
                line = LineChart()
851
                line.title = _('Base Period Cumulative Efficiency') + ' / ' \
852
                    + _('Reporting Period Cumulative Efficiency') + ' - ' \
853
                    + reporting_period_data['names'][i] + " (" + reporting_period_data['units'][i] + ")"
854
                labels = Reference(ws, min_col=2 + base_period_data_ca_len * 3 + 1,
855
                                   min_row=table_start_row_number + 1,
856
                                   max_row=table_start_row_number + len(reporting_period_timestamps[0]))
857
                base_line_data = Reference(ws, min_col=3 + i * 3,
858
                                           min_row=table_start_row_number,
859
                                           max_row=table_start_row_number + len(reporting_period_timestamps[0]))
860
                reporting_line_data = Reference(ws, min_col=3 + base_period_data_ca_len * 3 + 1 + i * 3,
861
                                                min_row=table_start_row_number,
862
                                                max_row=table_start_row_number + len(reporting_period_timestamps[0]))
863
                line.add_data(base_line_data, titles_from_data=True)
864
                line.add_data(reporting_line_data, titles_from_data=True)
865
                line.set_categories(labels)
866
                for j in range(len(line.series)):
867
                    line.series[j].marker.symbol = "circle"
868
                    line.series[j].smooth = True
869
                line.x_axis.crosses = 'min'
870
                line.height = 8.25
871
                line.width = 24
872
                line.dLbls = DataLabelList()
873
                line.dLbls.dLblPos = 't'
874
                line.dLbls.showVal = True
875
                line.dLbls.showPercent = False
876
                chart_col = 'B'
877
                chart_cell = chart_col + str(chart_start_row_number)
878
                chart_start_row_number += 6
879
                ws.add_chart(line, chart_cell)
880
881
                line = LineChart()
882
                line.title = _('Base Period Cumulative Efficiency') + ' / ' \
883
                    + _('Reporting Period Cumulative Efficiency') + ' - ' \
884
                    + reporting_period_data['names'][i] + "-" \
885
                    + reporting_period_data['numerator_names'][i] \
886
                    + " (" + reporting_period_data['numerator_units'][i] + ")"
887
                labels = Reference(ws, min_col=2 + base_period_data_ca_len * 3 + 1,
888
                                   min_row=table_start_row_number + 1,
889
                                   max_row=table_start_row_number + len(reporting_period_timestamps[0]))
890
                base_line_data = Reference(ws, min_col=4 + i * 3,
891
                                           min_row=table_start_row_number,
892
                                           max_row=table_start_row_number + len(reporting_period_timestamps[0]))
893
                reporting_line_data = Reference(ws, min_col=4 + base_period_data_ca_len * 3 + 1 + i * 3,
894
                                                min_row=table_start_row_number,
895
                                                max_row=table_start_row_number + len(reporting_period_timestamps[0]))
896
                line.add_data(base_line_data, titles_from_data=True)
897
                line.add_data(reporting_line_data, titles_from_data=True)
898
                line.set_categories(labels)
899
                for j in range(len(line.series)):
900
                    line.series[j].marker.symbol = "circle"
901
                    line.series[j].smooth = True
902
                line.x_axis.crosses = 'min'
903
                line.height = 8.25
904
                line.width = 24
905
                line.dLbls = DataLabelList()
906
                line.dLbls.dLblPos = 't'
907
                line.dLbls.showVal = True
908
                line.dLbls.showPercent = False
909
                chart_col = 'B'
910
                chart_cell = chart_col + str(chart_start_row_number)
911
                chart_start_row_number += 6
912
                ws.add_chart(line, chart_cell)
913
914
                line = LineChart()
915
                line.title = _('Base Period Cumulative Efficiency') + ' / ' \
916
                    + _('Reporting Period Cumulative Efficiency') + ' - ' \
917
                    + reporting_period_data['names'][i] + "-" \
918
                    + reporting_period_data['denominator_names'][i] \
919
                    + " (" + reporting_period_data['denominator_units'][i] + ")"
920
                labels = Reference(ws, min_col=2 + base_period_data_ca_len * 3 + 1,
921
                                   min_row=table_start_row_number + 1,
922
                                   max_row=table_start_row_number + len(reporting_period_timestamps[0]))
923
                base_line_data = Reference(ws, min_col=5 + i * 3,
924
                                           min_row=table_start_row_number,
925
                                           max_row=table_start_row_number + len(reporting_period_timestamps[0]))
926
                reporting_line_data = Reference(ws, min_col=5 + base_period_data_ca_len * 3 + 1 + i * 3,
927
                                                min_row=table_start_row_number,
928
                                                max_row=table_start_row_number + len(reporting_period_timestamps[0]))
929
                line.add_data(base_line_data, titles_from_data=True)
930
                line.add_data(reporting_line_data, titles_from_data=True)
931
                line.set_categories(labels)
932
                for j in range(len(line.series)):
933
                    line.series[j].marker.symbol = "circle"
934
                    line.series[j].smooth = True
935
                line.x_axis.crosses = 'min'
936
                line.height = 8.25
937
                line.width = 24
938
                line.dLbls = DataLabelList()
939
                line.dLbls.dLblPos = 't'
940
                line.dLbls.showVal = True
941
                line.dLbls.showPercent = False
942
                chart_col = 'B'
943
                chart_cell = chart_col + str(chart_start_row_number)
944
                chart_start_row_number += 6
945
                ws.add_chart(line, chart_cell)
946
947
    ####################################################################################################################
948
    has_associated_equipment_flag = True
949
950
    if "associated_equipment" not in report.keys() or \
951
            "energy_category_names" not in report['associated_equipment'].keys() or \
952
            len(report['associated_equipment']["energy_category_names"]) == 0 \
953
            or 'associated_equipment_names_array' not in report['associated_equipment'].keys() \
954
            or report['associated_equipment']['associated_equipment_names_array'] is None \
955
            or len(report['associated_equipment']['associated_equipment_names_array']) == 0 \
956
            or len(report['associated_equipment']['associated_equipment_names_array'][0]) == 0:
957
        has_associated_equipment_flag = False
958
959
    if has_associated_equipment_flag:
960
        associated_equipment = report['associated_equipment']
961
962
        ws['B' + str(current_row_number)].font = title_font
963
        ws['B' + str(current_row_number)] = name + ' ' + _('Associated Equipment Data')
964
965
        current_row_number += 1
966
967
        ws.row_dimensions[current_row_number].height = 60
968
        ws['B' + str(current_row_number)].fill = table_fill
969
        ws['B' + str(current_row_number)].font = name_font
970
        ws['B' + str(current_row_number)].alignment = c_c_alignment
971
        ws['B' + str(current_row_number)].border = f_border
972
        ws['B' + str(current_row_number)] = _('Associated Equipment')
973
        ca_len = len(associated_equipment['energy_category_names'])
974
975
        for i in range(0, ca_len):
976
            row = chr(ord('C') + i)
977
            ws[row + str(current_row_number)].fill = table_fill
978
            ws[row + str(current_row_number)].font = name_font
979
            ws[row + str(current_row_number)].alignment = c_c_alignment
980
            ws[row + str(current_row_number)].border = f_border
981
            ws[row + str(current_row_number)] = \
982
                associated_equipment['energy_category_names'][i] + " (" + associated_equipment['units'][i] + ")"
983
984
        associated_equipment_len = len(associated_equipment['associated_equipment_names_array'][0])
985
986
        for i in range(0, associated_equipment_len):
987
            current_row_number += 1
988
            row = str(current_row_number)
989
990
            ws['B' + row].font = title_font
991
            ws['B' + row].alignment = c_c_alignment
992
            ws['B' + row] = associated_equipment['associated_equipment_names_array'][0][i]
993
            ws['B' + row].border = f_border
994
995
            for j in range(0, ca_len):
996
                col = chr(ord('C') + j)
997
                ws[col + row].font = title_font
998
                ws[col + row].alignment = c_c_alignment
999
                ws[col + row] = round(associated_equipment['subtotals_array'][j][i], 2)
1000
                ws[col + row].border = f_border
1001
    ####################################################################################################################
1002
1003 View Code Duplication
    if has_parameters_names_and_timestamps_and_values_data:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1004
1005
        ################################################################################################################
1006
        # new worksheet
1007
        ################################################################################################################
1008
1009
        parameters_data = report['parameters']
1010
        parameters_names_len = len(parameters_data['names'])
1011
1012
        file_name = (re.sub(r'[^A-Z]', '', ws.title))+'_'
1013
        parameters_ws = wb.create_sheet(file_name + _('Parameters'))
1014
1015
        parameters_timestamps_data_max_len = \
1016
            get_parameters_timestamps_lists_max_len(list(parameters_data['timestamps']))
1017
1018
        # Row height
1019
        parameters_ws.row_dimensions[1].height = 102
1020
        for i in range(2, 7 + 1):
1021
            parameters_ws.row_dimensions[i].height = 42
1022
1023
        for i in range(8, parameters_timestamps_data_max_len + 10):
1024
            parameters_ws.row_dimensions[i].height = 60
1025
1026
        # Col width
1027
        parameters_ws.column_dimensions['A'].width = 1.5
1028
1029
        parameters_ws.column_dimensions['B'].width = 25.0
1030
1031
        for i in range(3, 12+parameters_names_len*3):
1032
            parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = 15.0
1033
1034
        # Img
1035
        img = Image("excelexporters/myems.png")
1036
        parameters_ws.add_image(img, 'A1')
1037
1038
        # Title
1039
        parameters_ws['B3'].alignment = b_r_alignment
1040
        parameters_ws['B3'] = _('Name') + ':'
1041
        parameters_ws['C3'].border = b_border
1042
        parameters_ws['C3'].alignment = b_c_alignment
1043
        parameters_ws['C3'] = name
1044
1045
        parameters_ws['D3'].alignment = b_r_alignment
1046
        parameters_ws['D3'] = _('Period Type') + ':'
1047
        parameters_ws['E3'].border = b_border
1048
        parameters_ws['E3'].alignment = b_c_alignment
1049
        parameters_ws['E3'] = period_type
1050
1051
        parameters_ws['B4'].alignment = b_r_alignment
1052
        parameters_ws['B4'] = _('Reporting Start Datetime') + ':'
1053
        parameters_ws['C4'].border = b_border
1054
        parameters_ws['C4'].alignment = b_c_alignment
1055
        parameters_ws['C4'] = reporting_start_datetime_local
1056
1057
        parameters_ws['D4'].alignment = b_r_alignment
1058
        parameters_ws['D4'] = _('Reporting End Datetime') + ':'
1059
        parameters_ws['E4'].border = b_border
1060
        parameters_ws['E4'].alignment = b_c_alignment
1061
        parameters_ws['E4'] = reporting_end_datetime_local
1062
1063
        parameters_ws_current_row_number = 6
1064
1065
        parameters_ws['B' + str(parameters_ws_current_row_number)].font = title_font
1066
        parameters_ws['B' + str(parameters_ws_current_row_number)] = name + ' ' + _('Parameters')
1067
1068
        parameters_ws_current_row_number += 1
1069
1070
        parameters_table_start_row_number = parameters_ws_current_row_number
1071
1072
        parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80
1073
1074
        parameters_ws_current_row_number += 1
1075
1076
        table_current_col_number = 2
1077
1078
        for i in range(0, parameters_names_len):
1079
1080
            if len(parameters_data['timestamps'][i]) == 0:
1081
                continue
1082
1083
            col = format_cell.get_column_letter(table_current_col_number)
1084
1085
            parameters_ws[col + str(parameters_ws_current_row_number-1)].fill = table_fill
1086
            parameters_ws[col + str(parameters_ws_current_row_number-1)].border = f_border
1087
1088
            col = format_cell.get_column_letter(table_current_col_number + 1)
1089
1090
            parameters_ws[col + str(parameters_ws_current_row_number-1)].fill = table_fill
1091
            parameters_ws[col + str(parameters_ws_current_row_number-1)].border = f_border
1092
            parameters_ws[col + str(parameters_ws_current_row_number-1)].font = name_font
1093
            parameters_ws[col + str(parameters_ws_current_row_number-1)].alignment = c_c_alignment
1094
            parameters_ws[col + str(parameters_ws_current_row_number-1)] = parameters_data['names'][i]
1095
1096
            table_current_row_number = parameters_ws_current_row_number
1097
1098
            for j, value in enumerate(list(parameters_data['timestamps'][i])):
1099
                col = format_cell.get_column_letter(table_current_col_number)
1100
1101
                parameters_ws[col + str(table_current_row_number)].border = f_border
1102
                parameters_ws[col + str(table_current_row_number)].font = title_font
1103
                parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment
1104
                parameters_ws[col + str(table_current_row_number)] = value
1105
1106
                col = format_cell.get_column_letter(table_current_col_number + 1)
1107
1108
                parameters_ws[col + str(table_current_row_number)].border = f_border
1109
                parameters_ws[col + str(table_current_row_number)].font = title_font
1110
                parameters_ws[col + str(table_current_row_number)].alignment = c_c_alignment
1111
                parameters_ws[col + str(table_current_row_number)] = round(parameters_data['values'][i][j], 2)
1112
1113
                table_current_row_number += 1
1114
1115
            table_current_col_number = table_current_col_number + 3
1116
1117
        ################################################################################################################
1118
        # parameters chart and parameters table
1119
        ################################################################################################################
1120
1121
        ws['B' + str(current_sheet_parameters_row_number)].font = title_font
1122
        ws['B' + str(current_sheet_parameters_row_number)] = name + ' ' + _('Parameters')
1123
1124
        current_sheet_parameters_row_number += 1
1125
1126
        chart_start_row_number = current_sheet_parameters_row_number
1127
1128
        col_index = 0
1129
1130
        for i in range(0, parameters_names_len):
1131
1132
            if len(parameters_data['timestamps'][i]) == 0:
1133
                continue
1134
1135
            line = LineChart()
1136
            data_col = 3+col_index*3
1137
            labels_col = 2+col_index*3
1138
            col_index += 1
1139
            line.title = _('Parameters') + ' - ' + \
1140
                parameters_ws.cell(row=parameters_table_start_row_number, column=data_col).value
1141
            labels = Reference(parameters_ws, min_col=labels_col, min_row=parameters_table_start_row_number + 1,
1142
                               max_row=(len(parameters_data['timestamps'][i])+parameters_table_start_row_number))
1143
            line_data = Reference(parameters_ws, min_col=data_col, min_row=parameters_table_start_row_number,
1144
                                  max_row=(len(parameters_data['timestamps'][i])+parameters_table_start_row_number))
1145
            line.add_data(line_data, titles_from_data=True)
1146
            line.set_categories(labels)
1147
            line_data = line.series[0]
1148
            line_data.marker.symbol = "circle"
1149
            line_data.smooth = True
1150
            line.x_axis.crosses = 'min'
1151
            line.height = 8.25
1152
            line.width = 24
1153
            line.dLbls = DataLabelList()
1154
            line.dLbls.dLblPos = 't'
1155
            line.dLbls.showVal = False
1156
            line.dLbls.showPercent = False
1157
            chart_col = 'B'
1158
            chart_cell = chart_col + str(chart_start_row_number)
1159
            chart_start_row_number += 6
1160
            ws.add_chart(line, chart_cell)
1161
1162
        current_sheet_parameters_row_number = chart_start_row_number
1163
1164
        current_sheet_parameters_row_number += 1
1165
1166
    filename = str(uuid.uuid4()) + '.xlsx'
1167
    wb.save(filename)
1168
1169
    return filename
1170
1171
1172
def get_parameters_timestamps_lists_max_len(parameters_timestamps_lists):
1173
    max_len = 0
1174
    for i, value in enumerate(list(parameters_timestamps_lists)):
1175
        if len(value) > max_len:
1176
            max_len = len(value)
1177
1178
    return max_len
1179
1180
1181
def timestamps_data_all_equal_0(lists):
1182
    for i, value in enumerate(list(lists)):
1183
        if len(value) > 0:
1184
            return False
1185
1186
    return True
1187
1188
1189
def timestamps_data_not_equal_0(lists):
1190
    number = 0
1191
    for i, value in enumerate(list(lists)):
1192
        if len(value) > 0:
1193
            number += 1
1194
    return number
1195
1196
1197 View Code Duplication
def is_base_period_timestamp_exists(base_period_data):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
1198
    timestamps = base_period_data['timestamps']
1199
1200
    if len(timestamps) == 0:
1201
        return False
1202
1203
    for timestamp in timestamps:
1204
        if len(timestamp) > 0:
1205
            return True
1206
1207
    return False
1208