| Total Complexity | 81 |
| Total Lines | 936 |
| Duplicated Lines | 94.98 % |
| Changes | 0 | ||
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:
Complex classes like excelexporters.shopfloorcomparison 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.
| 1 | import base64 |
||
| 2 | from core.utilities import get_translation |
||
| 3 | import os |
||
| 4 | import re |
||
| 5 | import uuid |
||
| 6 | import openpyxl.utils.cell as format_cell |
||
| 7 | from openpyxl import Workbook |
||
| 8 | from openpyxl.chart import LineChart, Reference |
||
| 9 | from openpyxl.drawing.image import Image |
||
| 10 | from openpyxl.styles import PatternFill, Border, Side, Alignment, Font |
||
| 11 | from core.utilities import round2 |
||
| 12 | |||
| 13 | |||
| 14 | ######################################################################################################################## |
||
| 15 | # PROCEDURES |
||
| 16 | # Step 1: Validate the report data |
||
| 17 | # Step 2: Generate excel file from the report data |
||
| 18 | # Step 3: Encode the excel file to Base64 |
||
| 19 | ######################################################################################################################## |
||
| 20 | View Code Duplication | def export( |
|
|
|
|||
| 21 | result, |
||
| 22 | shopfloor1_name, |
||
| 23 | shopfloor2_name, |
||
| 24 | energy_category_name, |
||
| 25 | reporting_start_datetime_local, |
||
| 26 | reporting_end_datetime_local, |
||
| 27 | period_type, |
||
| 28 | language, |
||
| 29 | ): |
||
| 30 | #################################################################################################################### |
||
| 31 | # Step 1: Validate the report data |
||
| 32 | #################################################################################################################### |
||
| 33 | if result is None: |
||
| 34 | return None |
||
| 35 | |||
| 36 | #################################################################################################################### |
||
| 37 | # Step 2: Generate excel file from the report data |
||
| 38 | #################################################################################################################### |
||
| 39 | filename = generate_excel( |
||
| 40 | result, |
||
| 41 | shopfloor1_name, |
||
| 42 | shopfloor2_name, |
||
| 43 | energy_category_name, |
||
| 44 | reporting_start_datetime_local, |
||
| 45 | reporting_end_datetime_local, |
||
| 46 | period_type, |
||
| 47 | language, |
||
| 48 | ) |
||
| 49 | #################################################################################################################### |
||
| 50 | # Step 3: Encode the excel file to Base64 |
||
| 51 | #################################################################################################################### |
||
| 52 | binary_file_data = b"" |
||
| 53 | try: |
||
| 54 | with open(filename, "rb") as binary_file: |
||
| 55 | binary_file_data = binary_file.read() |
||
| 56 | except IOError as ex: |
||
| 57 | print(str(ex)) |
||
| 58 | |||
| 59 | # Base64 encode the bytes |
||
| 60 | base64_encoded_data = base64.b64encode(binary_file_data) |
||
| 61 | # get the Base64 encoded data using human-readable characters. |
||
| 62 | base64_message = base64_encoded_data.decode("utf-8") |
||
| 63 | # delete the file from server |
||
| 64 | try: |
||
| 65 | os.remove(filename) |
||
| 66 | except NotImplementedError as ex: |
||
| 67 | print(str(ex)) |
||
| 68 | return base64_message |
||
| 69 | |||
| 70 | |||
| 71 | View Code Duplication | def generate_excel( |
|
| 72 | report, |
||
| 73 | shopfloor1_name, |
||
| 74 | shopfloor2_name, |
||
| 75 | energy_category_name, |
||
| 76 | reporting_start_datetime_local, |
||
| 77 | reporting_end_datetime_local, |
||
| 78 | period_type, |
||
| 79 | language, |
||
| 80 | ): |
||
| 81 | trans = get_translation(language) |
||
| 82 | trans.install() |
||
| 83 | _ = trans.gettext |
||
| 84 | |||
| 85 | wb = Workbook() |
||
| 86 | ws = wb.active |
||
| 87 | ws.title = "ShopfloorComparison" |
||
| 88 | # Row height |
||
| 89 | ws.row_dimensions[1].height = 102 |
||
| 90 | for i in range(2, 2000 + 1): |
||
| 91 | ws.row_dimensions[i].height = 42 |
||
| 92 | |||
| 93 | # Col width |
||
| 94 | ws.column_dimensions["A"].width = 1.5 |
||
| 95 | |||
| 96 | ws.column_dimensions["B"].width = 25.0 |
||
| 97 | |||
| 98 | for i in range(ord("C"), ord("L")): |
||
| 99 | ws.column_dimensions[chr(i)].width = 15.0 |
||
| 100 | |||
| 101 | # Font |
||
| 102 | name_font = Font(name="Arial", size=15, bold=True) |
||
| 103 | title_font = Font(name="Arial", size=15, bold=True) |
||
| 104 | |||
| 105 | table_fill = PatternFill(fill_type="solid", fgColor="90ee90") |
||
| 106 | f_border = Border( |
||
| 107 | left=Side(border_style="medium"), |
||
| 108 | right=Side(border_style="medium"), |
||
| 109 | bottom=Side(border_style="medium"), |
||
| 110 | top=Side(border_style="medium"), |
||
| 111 | ) |
||
| 112 | b_border = Border( |
||
| 113 | bottom=Side(border_style="medium"), |
||
| 114 | ) |
||
| 115 | |||
| 116 | b_c_alignment = Alignment( |
||
| 117 | vertical="bottom", |
||
| 118 | horizontal="center", |
||
| 119 | text_rotation=0, |
||
| 120 | wrap_text=True, |
||
| 121 | shrink_to_fit=False, |
||
| 122 | indent=0, |
||
| 123 | ) |
||
| 124 | c_c_alignment = Alignment( |
||
| 125 | vertical="center", |
||
| 126 | horizontal="center", |
||
| 127 | text_rotation=0, |
||
| 128 | wrap_text=True, |
||
| 129 | shrink_to_fit=False, |
||
| 130 | indent=0, |
||
| 131 | ) |
||
| 132 | b_r_alignment = Alignment( |
||
| 133 | vertical="bottom", |
||
| 134 | horizontal="right", |
||
| 135 | text_rotation=0, |
||
| 136 | wrap_text=True, |
||
| 137 | shrink_to_fit=False, |
||
| 138 | indent=0, |
||
| 139 | ) |
||
| 140 | |||
| 141 | # Img |
||
| 142 | img = Image("excelexporters/myems.png") |
||
| 143 | ws.add_image(img, "A1") |
||
| 144 | |||
| 145 | # Title |
||
| 146 | ws["B3"].alignment = b_r_alignment |
||
| 147 | ws["B3"] = _("Shopfloor") + "1:" |
||
| 148 | ws["C3"].border = b_border |
||
| 149 | ws["C3"].alignment = b_c_alignment |
||
| 150 | ws["C3"] = shopfloor1_name |
||
| 151 | |||
| 152 | ws["D3"].alignment = b_r_alignment |
||
| 153 | ws["D3"] = _("Shopfloor") + "2:" |
||
| 154 | ws["E3"].border = b_border |
||
| 155 | ws["E3"].alignment = b_c_alignment |
||
| 156 | ws["E3"] = shopfloor2_name |
||
| 157 | |||
| 158 | ws["F3"].alignment = b_r_alignment |
||
| 159 | ws["F3"] = _("Energy Category") + ":" |
||
| 160 | ws["G3"].border = b_border |
||
| 161 | ws["G3"].alignment = b_c_alignment |
||
| 162 | ws["G3"] = energy_category_name |
||
| 163 | |||
| 164 | ws["B4"].alignment = b_r_alignment |
||
| 165 | ws["B4"] = _("Period Type") + ":" |
||
| 166 | ws["C4"].border = b_border |
||
| 167 | ws["C4"].alignment = b_c_alignment |
||
| 168 | ws["C4"] = period_type |
||
| 169 | |||
| 170 | ws["D4"].alignment = b_r_alignment |
||
| 171 | ws["D4"] = _("Reporting Start Datetime") + ":" |
||
| 172 | ws["E4"].border = b_border |
||
| 173 | ws["E4"].alignment = b_c_alignment |
||
| 174 | ws["E4"] = reporting_start_datetime_local |
||
| 175 | |||
| 176 | ws["F4"].alignment = b_r_alignment |
||
| 177 | ws["F4"] = _("Reporting End Datetime") + ":" |
||
| 178 | ws["G4"].border = b_border |
||
| 179 | ws["G4"].alignment = b_c_alignment |
||
| 180 | ws["G4"] = reporting_end_datetime_local |
||
| 181 | |||
| 182 | if ( |
||
| 183 | "reporting_period1" not in report.keys() |
||
| 184 | or "values" not in report["reporting_period1"].keys() |
||
| 185 | or len(report["reporting_period1"]["values"]) == 0 |
||
| 186 | ): |
||
| 187 | filename = str(uuid.uuid4()) + ".xlsx" |
||
| 188 | wb.save(filename) |
||
| 189 | |||
| 190 | return filename |
||
| 191 | #################################################################################################################### |
||
| 192 | # First: Consumption |
||
| 193 | # 6: shopfloor1 title |
||
| 194 | # 7: shopfloor1 table title |
||
| 195 | # 8~9 shopfloor1 table_data |
||
| 196 | # 10: shopfloor2 title |
||
| 197 | # 11: shopfloor2 table title |
||
| 198 | # 12~13: shopfloor2 table_data |
||
| 199 | #################################################################################################################### |
||
| 200 | if ( |
||
| 201 | "values" not in report["reporting_period1"].keys() |
||
| 202 | or len(report["reporting_period1"]["values"]) == 0 |
||
| 203 | ): |
||
| 204 | for i in range(6, 9 + 1): |
||
| 205 | ws.row_dimensions[i].height = 0.1 |
||
| 206 | else: |
||
| 207 | reporting_period_data1 = report["reporting_period1"] |
||
| 208 | |||
| 209 | ws.row_dimensions[7].height = 60 |
||
| 210 | ws["B7"].font = title_font |
||
| 211 | ws["B7"].alignment = c_c_alignment |
||
| 212 | ws["B7"] = shopfloor1_name |
||
| 213 | ws["B7"].fill = table_fill |
||
| 214 | ws["B7"].border = f_border |
||
| 215 | |||
| 216 | ws["B8"].font = title_font |
||
| 217 | ws["B8"].alignment = c_c_alignment |
||
| 218 | ws["B8"] = _("Consumption") |
||
| 219 | ws["B8"].border = f_border |
||
| 220 | |||
| 221 | ws["C7"].fill = table_fill |
||
| 222 | ws["C7"].font = name_font |
||
| 223 | ws["C7"].alignment = c_c_alignment |
||
| 224 | ws["C7"] = ( |
||
| 225 | energy_category_name |
||
| 226 | + " (" |
||
| 227 | + report["energy_category"]["unit_of_measure"] |
||
| 228 | + ")" |
||
| 229 | ) |
||
| 230 | ws["C7"].border = f_border |
||
| 231 | |||
| 232 | ws["C8"].font = name_font |
||
| 233 | ws["C8"].alignment = c_c_alignment |
||
| 234 | ws["C8"] = round2(reporting_period_data1["total_in_category"], 2) |
||
| 235 | ws["C8"].border = f_border |
||
| 236 | |||
| 237 | if ( |
||
| 238 | "values" not in report["reporting_period2"].keys() |
||
| 239 | or len(report["reporting_period2"]["values"]) == 0 |
||
| 240 | ): |
||
| 241 | for i in range(11, 14 + 1): |
||
| 242 | ws.row_dimensions[i].height = 0.1 |
||
| 243 | else: |
||
| 244 | reporting_period_data2 = report["reporting_period2"] |
||
| 245 | |||
| 246 | ws.row_dimensions[12].height = 60 |
||
| 247 | ws["B11"].font = title_font |
||
| 248 | ws["B11"].alignment = c_c_alignment |
||
| 249 | ws["B11"].fill = table_fill |
||
| 250 | ws["B11"].border = f_border |
||
| 251 | ws["B11"] = shopfloor2_name |
||
| 252 | |||
| 253 | ws["B12"].font = title_font |
||
| 254 | ws["B12"].alignment = c_c_alignment |
||
| 255 | ws["B12"] = _("Consumption") |
||
| 256 | ws["B12"].border = f_border |
||
| 257 | |||
| 258 | ws["C11"].fill = table_fill |
||
| 259 | ws["C11"].font = name_font |
||
| 260 | ws["C11"].alignment = c_c_alignment |
||
| 261 | ws["C11"] = ( |
||
| 262 | energy_category_name |
||
| 263 | + " (" |
||
| 264 | + report["energy_category"]["unit_of_measure"] |
||
| 265 | + ")" |
||
| 266 | ) |
||
| 267 | ws["C11"].border = f_border |
||
| 268 | |||
| 269 | ws["C12"].font = name_font |
||
| 270 | ws["C12"].alignment = c_c_alignment |
||
| 271 | ws["C12"] = round2(reporting_period_data2["total_in_category"], 2) |
||
| 272 | ws["C12"].border = f_border |
||
| 273 | |||
| 274 | #################################################################################################################### |
||
| 275 | # Second: Detailed Data |
||
| 276 | # 15: title |
||
| 277 | # 12 ~ 16: chart |
||
| 278 | # 18 + 6 * parameterlen + : table title |
||
| 279 | # 19 + 6 * parameterlen~18 + 6 * parameterlen + timestamps_len: table_data |
||
| 280 | # parameter_len: len(report['parameters1']['names']) + len(report['parameters1']['names']) |
||
| 281 | # timestamps_len: reporting_period_data1['timestamps'] |
||
| 282 | #################################################################################################################### |
||
| 283 | times = report["reporting_period1"]["timestamps"] |
||
| 284 | |||
| 285 | if ( |
||
| 286 | "values" not in report["reporting_period1"].keys() |
||
| 287 | or len(report["reporting_period1"]["values"]) == 0 |
||
| 288 | or "values" not in report["reporting_period2"].keys() |
||
| 289 | or len(report["reporting_period2"]["values"]) == 0 |
||
| 290 | ): |
||
| 291 | for i in range(11, 43 + 1): |
||
| 292 | ws.row_dimensions[i].height = 0.0 |
||
| 293 | else: |
||
| 294 | reporting_period_data1 = report["reporting_period1"] |
||
| 295 | reporting_period_data2 = report["reporting_period2"] |
||
| 296 | diff_data = report["diff"] |
||
| 297 | parameters_parameters_datas_len = 0 |
||
| 298 | start_detail_data_row_num = 15 + (parameters_parameters_datas_len + 1 + 1) * 6 -4 |
||
| 299 | ws["B14"].font = title_font |
||
| 300 | ws["B14"] = shopfloor1_name + " and " + shopfloor2_name + _("Detailed Data") |
||
| 301 | |||
| 302 | ws.row_dimensions[start_detail_data_row_num].height = 60 |
||
| 303 | |||
| 304 | ws["B" + str(start_detail_data_row_num)].fill = table_fill |
||
| 305 | ws["B" + str(start_detail_data_row_num)].font = title_font |
||
| 306 | ws["B" + str(start_detail_data_row_num)].border = f_border |
||
| 307 | ws["B" + str(start_detail_data_row_num)].alignment = c_c_alignment |
||
| 308 | ws["B" + str(start_detail_data_row_num)] = _("Datetime") |
||
| 309 | time = times |
||
| 310 | has_data = False |
||
| 311 | max_row = 0 |
||
| 312 | if len(time) > 0: |
||
| 313 | has_data = True |
||
| 314 | max_row = start_detail_data_row_num + len(time) |
||
| 315 | |||
| 316 | if has_data: |
||
| 317 | for i in range(0, len(time)): |
||
| 318 | col = "B" |
||
| 319 | row = str(start_detail_data_row_num + 1 + i) |
||
| 320 | # col = chr(ord('B') + i) |
||
| 321 | ws[col + row].font = title_font |
||
| 322 | ws[col + row].alignment = c_c_alignment |
||
| 323 | ws[col + row] = time[i] |
||
| 324 | ws[col + row].border = f_border |
||
| 325 | |||
| 326 | # table_title |
||
| 327 | col = chr(ord(col) + 1) |
||
| 328 | |||
| 329 | ws[col + str(start_detail_data_row_num)].fill = table_fill |
||
| 330 | ws[col + str(start_detail_data_row_num)].font = title_font |
||
| 331 | ws[col + str(start_detail_data_row_num)].alignment = c_c_alignment |
||
| 332 | ws[col + str(start_detail_data_row_num)] = ( |
||
| 333 | shopfloor1_name |
||
| 334 | + " " |
||
| 335 | + energy_category_name |
||
| 336 | + " (" |
||
| 337 | + report["energy_category"]["unit_of_measure"] |
||
| 338 | + ")" |
||
| 339 | ) |
||
| 340 | ws[col + str(start_detail_data_row_num)].border = f_border |
||
| 341 | |||
| 342 | # table_data |
||
| 343 | time = times |
||
| 344 | time_len = len(time) |
||
| 345 | |||
| 346 | for j in range(0, time_len): |
||
| 347 | row = str(start_detail_data_row_num + 1 + j) |
||
| 348 | # col = chr(ord('B') + i) |
||
| 349 | ws[col + row].font = title_font |
||
| 350 | ws[col + row].alignment = c_c_alignment |
||
| 351 | ws[col + row] = round2(reporting_period_data1["values"][j], 2) |
||
| 352 | ws[col + row].border = f_border |
||
| 353 | |||
| 354 | # table_title |
||
| 355 | col = chr(ord(col) + 1) |
||
| 356 | |||
| 357 | ws[col + str(start_detail_data_row_num)].fill = table_fill |
||
| 358 | ws[col + str(start_detail_data_row_num)].font = title_font |
||
| 359 | ws[col + str(start_detail_data_row_num)].alignment = c_c_alignment |
||
| 360 | ws[col + str(start_detail_data_row_num)] = ( |
||
| 361 | shopfloor2_name |
||
| 362 | + " " |
||
| 363 | + energy_category_name |
||
| 364 | + " (" |
||
| 365 | + report["energy_category"]["unit_of_measure"] |
||
| 366 | + ")" |
||
| 367 | ) |
||
| 368 | ws[col + str(start_detail_data_row_num)].border = f_border |
||
| 369 | |||
| 370 | # table_data |
||
| 371 | time = times |
||
| 372 | time_len = len(time) |
||
| 373 | |||
| 374 | for j in range(0, time_len): |
||
| 375 | row = str(start_detail_data_row_num + 1 + j) |
||
| 376 | # col = chr(ord('B') + i) |
||
| 377 | ws[col + row].font = title_font |
||
| 378 | ws[col + row].alignment = c_c_alignment |
||
| 379 | ws[col + row] = round2(reporting_period_data2["values"][j], 2) |
||
| 380 | ws[col + row].border = f_border |
||
| 381 | |||
| 382 | # table_title |
||
| 383 | col = chr(ord(col) + 1) |
||
| 384 | |||
| 385 | ws[col + str(start_detail_data_row_num)].fill = table_fill |
||
| 386 | ws[col + str(start_detail_data_row_num)].font = title_font |
||
| 387 | ws[col + str(start_detail_data_row_num)].alignment = c_c_alignment |
||
| 388 | ws[col + str(start_detail_data_row_num)] = _("Difference") |
||
| 389 | ws[col + str(start_detail_data_row_num)].border = f_border |
||
| 390 | |||
| 391 | # table_data |
||
| 392 | time = times |
||
| 393 | time_len = len(time) |
||
| 394 | |||
| 395 | for j in range(0, time_len): |
||
| 396 | row = str(start_detail_data_row_num + 1 + j) |
||
| 397 | # col = chr(ord('B') + i) |
||
| 398 | ws[col + row].font = title_font |
||
| 399 | ws[col + row].alignment = c_c_alignment |
||
| 400 | ws[col + row] = round2(diff_data["values"][j], 2) |
||
| 401 | ws[col + row].border = f_border |
||
| 402 | # line |
||
| 403 | # 15~: line |
||
| 404 | line = LineChart() |
||
| 405 | line.title = _("Reporting Period Consumption") |
||
| 406 | labels = Reference( |
||
| 407 | ws, min_col=2, min_row=start_detail_data_row_num + 1, max_row=max_row |
||
| 408 | ) |
||
| 409 | line_data = Reference( |
||
| 410 | ws, |
||
| 411 | min_col=3, |
||
| 412 | max_col=2 + 1 + 1, |
||
| 413 | min_row=start_detail_data_row_num, |
||
| 414 | max_row=max_row, |
||
| 415 | ) |
||
| 416 | line.add_data(line_data, titles_from_data=True) |
||
| 417 | line.set_categories(labels) |
||
| 418 | for j in range(0, len(line.series)): |
||
| 419 | line.series[j].marker.symbol = "auto" |
||
| 420 | line.series[j].smooth = True |
||
| 421 | line.x_axis.crosses = "min" |
||
| 422 | line.height = 8.25 |
||
| 423 | line.width = 24 |
||
| 424 | ws.add_chart(line, "B15") |
||
| 425 | |||
| 426 | col = "B" |
||
| 427 | row = str(start_detail_data_row_num + 1 + len(time)) |
||
| 428 | |||
| 429 | ws[col + row].font = title_font |
||
| 430 | ws[col + row].alignment = c_c_alignment |
||
| 431 | ws[col + row] = _("Total") |
||
| 432 | ws[col + row].border = f_border |
||
| 433 | |||
| 434 | col = chr(ord(col) + 1) |
||
| 435 | ws[col + row].font = title_font |
||
| 436 | ws[col + row].alignment = c_c_alignment |
||
| 437 | ws[col + row] = round2(reporting_period_data1["total_in_category"], 2) |
||
| 438 | ws[col + row].border = f_border |
||
| 439 | |||
| 440 | col = chr(ord(col) + 1) |
||
| 441 | ws[col + row].font = title_font |
||
| 442 | ws[col + row].alignment = c_c_alignment |
||
| 443 | ws[col + row] = round2(reporting_period_data2["total_in_category"], 2) |
||
| 444 | ws[col + row].border = f_border |
||
| 445 | |||
| 446 | col = chr(ord(col) + 1) |
||
| 447 | ws[col + row].font = title_font |
||
| 448 | ws[col + row].alignment = c_c_alignment |
||
| 449 | ws[col + row] = round2(diff_data["total_in_category"], 2) |
||
| 450 | ws[col + row].border = f_border |
||
| 451 | |||
| 452 | #################################################################################################################### |
||
| 453 | has_parameters_names_and_timestamps_and_values_data = True |
||
| 454 | # 12 is the starting line number of the last line chart in the report period |
||
| 455 | current_sheet_parameters_row_number = 10 + (1 + 1) * 6 |
||
| 456 | if ( |
||
| 457 | "parameters1" not in report.keys() |
||
| 458 | or report["parameters1"] is None |
||
| 459 | or "names" not in report["parameters1"].keys() |
||
| 460 | or report["parameters1"]["names"] is None |
||
| 461 | or len(report["parameters1"]["names"]) == 0 |
||
| 462 | or "timestamps" not in report["parameters1"].keys() |
||
| 463 | or report["parameters1"]["timestamps"] is None |
||
| 464 | or len(report["parameters1"]["timestamps"]) == 0 |
||
| 465 | or "values" not in report["parameters1"].keys() |
||
| 466 | or report["parameters1"]["values"] is None |
||
| 467 | or len(report["parameters1"]["values"]) == 0 |
||
| 468 | or timestamps_data_all_equal_0(report["parameters1"]["timestamps"]) |
||
| 469 | ): |
||
| 470 | has_parameters_names_and_timestamps_and_values_data = False |
||
| 471 | |||
| 472 | if ( |
||
| 473 | "parameters2" not in report.keys() |
||
| 474 | or report["parameters2"] is None |
||
| 475 | or "names" not in report["parameters2"].keys() |
||
| 476 | or report["parameters2"]["names"] is None |
||
| 477 | or len(report["parameters2"]["names"]) == 0 |
||
| 478 | or "timestamps" not in report["parameters2"].keys() |
||
| 479 | or report["parameters2"]["timestamps"] is None |
||
| 480 | or len(report["parameters2"]["timestamps"]) == 0 |
||
| 481 | or "values" not in report["parameters2"].keys() |
||
| 482 | or report["parameters2"]["values"] is None |
||
| 483 | or len(report["parameters2"]["values"]) == 0 |
||
| 484 | or timestamps_data_all_equal_0(report["parameters2"]["timestamps"]) |
||
| 485 | ): |
||
| 486 | has_parameters_names_and_timestamps_and_values_data = False |
||
| 487 | |||
| 488 | if has_parameters_names_and_timestamps_and_values_data: |
||
| 489 | |||
| 490 | parameters_data1 = report["parameters1"] |
||
| 491 | |||
| 492 | parameters_names_len = len(parameters_data1["names"]) |
||
| 493 | |||
| 494 | file_name = (re.sub(r"[^A-Z]", "", ws.title)) + "_" |
||
| 495 | parameters_ws = wb.create_sheet(file_name + "Parameters1") |
||
| 496 | |||
| 497 | parameters_timestamps_data_max_len = get_parameters_timestamps_lists_max_len( |
||
| 498 | list(parameters_data1["timestamps"]) |
||
| 499 | ) |
||
| 500 | |||
| 501 | # Row height |
||
| 502 | parameters_ws.row_dimensions[1].height = 102 |
||
| 503 | for i in range(2, 7 + 1): |
||
| 504 | parameters_ws.row_dimensions[i].height = 42 |
||
| 505 | |||
| 506 | for i in range(8, parameters_timestamps_data_max_len + 10): |
||
| 507 | parameters_ws.row_dimensions[i].height = 60 |
||
| 508 | |||
| 509 | # Col width |
||
| 510 | parameters_ws.column_dimensions["A"].width = 1.5 |
||
| 511 | |||
| 512 | parameters_ws.column_dimensions["B"].width = 25.0 |
||
| 513 | |||
| 514 | for i in range(3, 12 + parameters_names_len * 3): |
||
| 515 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = ( |
||
| 516 | 15.0 |
||
| 517 | ) |
||
| 518 | |||
| 519 | # Img |
||
| 520 | img = Image("excelexporters/myems.png") |
||
| 521 | parameters_ws.add_image(img, "A1") |
||
| 522 | |||
| 523 | # Title |
||
| 524 | parameters_ws["B3"].alignment = b_r_alignment |
||
| 525 | parameters_ws["B3"] = _("Shopfloor") + "1:" |
||
| 526 | parameters_ws["C3"].border = b_border |
||
| 527 | parameters_ws["C3"].alignment = b_c_alignment |
||
| 528 | parameters_ws["C3"] = shopfloor1_name |
||
| 529 | |||
| 530 | parameters_ws["D3"].alignment = b_r_alignment |
||
| 531 | parameters_ws["D3"] = _("Energy Category") + ":" |
||
| 532 | parameters_ws["E3"].border = b_border |
||
| 533 | parameters_ws["E3"].alignment = b_c_alignment |
||
| 534 | parameters_ws["E3"] = energy_category_name |
||
| 535 | |||
| 536 | parameters_ws["B4"].alignment = b_r_alignment |
||
| 537 | parameters_ws["B4"] = _("Period Type") + ":" |
||
| 538 | parameters_ws["C4"].border = b_border |
||
| 539 | parameters_ws["C4"].alignment = b_c_alignment |
||
| 540 | parameters_ws["C4"] = period_type |
||
| 541 | |||
| 542 | parameters_ws["D4"].alignment = b_r_alignment |
||
| 543 | parameters_ws["D4"] = _("Reporting Start Datetime") + ":" |
||
| 544 | parameters_ws["E4"].border = b_border |
||
| 545 | parameters_ws["E4"].alignment = b_c_alignment |
||
| 546 | parameters_ws["E4"] = reporting_start_datetime_local |
||
| 547 | |||
| 548 | parameters_ws["F4"].alignment = b_r_alignment |
||
| 549 | parameters_ws["F4"] = _("Reporting End Datetime") + ":" |
||
| 550 | parameters_ws["G4"].border = b_border |
||
| 551 | parameters_ws["G4"].alignment = b_c_alignment |
||
| 552 | parameters_ws["G4"] = reporting_end_datetime_local |
||
| 553 | |||
| 554 | parameters_ws_current_row_number = 6 |
||
| 555 | |||
| 556 | parameters_ws["B" + str(parameters_ws_current_row_number)].font = title_font |
||
| 557 | parameters_ws["B" + str(parameters_ws_current_row_number)] = ( |
||
| 558 | shopfloor1_name + " " + _("Parameters") |
||
| 559 | ) |
||
| 560 | |||
| 561 | parameters_ws_current_row_number += 1 |
||
| 562 | |||
| 563 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
||
| 564 | |||
| 565 | parameters_ws_current_row_number += 1 |
||
| 566 | |||
| 567 | table_current_col_number = 2 |
||
| 568 | |||
| 569 | for i in range(0, parameters_names_len): |
||
| 570 | |||
| 571 | if len(parameters_data1["timestamps"][i]) == 0: |
||
| 572 | continue |
||
| 573 | |||
| 574 | col = format_cell.get_column_letter(table_current_col_number) |
||
| 575 | |||
| 576 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = ( |
||
| 577 | table_fill |
||
| 578 | ) |
||
| 579 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = ( |
||
| 580 | f_border |
||
| 581 | ) |
||
| 582 | |||
| 583 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
| 584 | |||
| 585 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = ( |
||
| 586 | table_fill |
||
| 587 | ) |
||
| 588 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = ( |
||
| 589 | f_border |
||
| 590 | ) |
||
| 591 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = ( |
||
| 592 | name_font |
||
| 593 | ) |
||
| 594 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = ( |
||
| 595 | c_c_alignment |
||
| 596 | ) |
||
| 597 | parameters_ws[col + str(parameters_ws_current_row_number - 1)] = ( |
||
| 598 | parameters_data1["names"][i] |
||
| 599 | ) |
||
| 600 | |||
| 601 | table_current_row_number = parameters_ws_current_row_number |
||
| 602 | |||
| 603 | for j, value in enumerate(list(parameters_data1["timestamps"][i])): |
||
| 604 | col = format_cell.get_column_letter(table_current_col_number) |
||
| 605 | |||
| 606 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
| 607 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
| 608 | parameters_ws[col + str(table_current_row_number)].alignment = ( |
||
| 609 | c_c_alignment |
||
| 610 | ) |
||
| 611 | parameters_ws[col + str(table_current_row_number)] = value |
||
| 612 | |||
| 613 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
| 614 | |||
| 615 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
| 616 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
| 617 | parameters_ws[col + str(table_current_row_number)].alignment = ( |
||
| 618 | c_c_alignment |
||
| 619 | ) |
||
| 620 | try: |
||
| 621 | parameters_ws[col + str(table_current_row_number)] = round2( |
||
| 622 | parameters_data1["values"][i][j], 2 |
||
| 623 | ) |
||
| 624 | except Exception as e: |
||
| 625 | print("error 1 in excelexporters\\shopfloorcomparison: " + str(e)) |
||
| 626 | |||
| 627 | table_current_row_number += 1 |
||
| 628 | |||
| 629 | table_current_col_number = table_current_col_number + 3 |
||
| 630 | |||
| 631 | parameters_data2 = report["parameters2"] |
||
| 632 | |||
| 633 | parameters_names_len = len(parameters_data2["names"]) |
||
| 634 | |||
| 635 | file_name = (re.sub(r"[^A-Z]", "", ws.title)) + "_" |
||
| 636 | parameters_ws = wb.create_sheet(file_name + "Parameters2") |
||
| 637 | |||
| 638 | parameters_timestamps_data_max_len = get_parameters_timestamps_lists_max_len( |
||
| 639 | list(parameters_data2["timestamps"]) |
||
| 640 | ) |
||
| 641 | |||
| 642 | # Row height |
||
| 643 | parameters_ws.row_dimensions[1].height = 102 |
||
| 644 | for i in range(2, 7 + 1): |
||
| 645 | parameters_ws.row_dimensions[i].height = 42 |
||
| 646 | |||
| 647 | for i in range(8, parameters_timestamps_data_max_len + 10): |
||
| 648 | parameters_ws.row_dimensions[i].height = 60 |
||
| 649 | |||
| 650 | # Col width |
||
| 651 | parameters_ws.column_dimensions["A"].width = 1.5 |
||
| 652 | |||
| 653 | parameters_ws.column_dimensions["B"].width = 25.0 |
||
| 654 | |||
| 655 | for i in range(3, 12 + parameters_names_len * 3): |
||
| 656 | parameters_ws.column_dimensions[format_cell.get_column_letter(i)].width = ( |
||
| 657 | 15.0 |
||
| 658 | ) |
||
| 659 | |||
| 660 | # Img |
||
| 661 | img = Image("excelexporters/myems.png") |
||
| 662 | parameters_ws.add_image(img, "A1") |
||
| 663 | |||
| 664 | # Title |
||
| 665 | parameters_ws["B3"].alignment = b_r_alignment |
||
| 666 | parameters_ws["B3"] = _("Shopfloor") + "2:" |
||
| 667 | parameters_ws["C3"].border = b_border |
||
| 668 | parameters_ws["C3"].alignment = b_c_alignment |
||
| 669 | parameters_ws["C3"] = shopfloor2_name |
||
| 670 | |||
| 671 | parameters_ws["D3"].alignment = b_r_alignment |
||
| 672 | parameters_ws["D3"] = _("Energy Category") + ":" |
||
| 673 | parameters_ws["E3"].border = b_border |
||
| 674 | parameters_ws["E3"].alignment = b_c_alignment |
||
| 675 | parameters_ws["E3"] = energy_category_name |
||
| 676 | |||
| 677 | parameters_ws["B4"].alignment = b_r_alignment |
||
| 678 | parameters_ws["B4"] = _("Period Type") + ":" |
||
| 679 | parameters_ws["C4"].border = b_border |
||
| 680 | parameters_ws["C4"].alignment = b_c_alignment |
||
| 681 | parameters_ws["C4"] = period_type |
||
| 682 | |||
| 683 | parameters_ws["D4"].alignment = b_r_alignment |
||
| 684 | parameters_ws["D4"] = _("Reporting Start Datetime") + ":" |
||
| 685 | parameters_ws["E4"].border = b_border |
||
| 686 | parameters_ws["E4"].alignment = b_c_alignment |
||
| 687 | parameters_ws["E4"] = reporting_start_datetime_local |
||
| 688 | |||
| 689 | parameters_ws["F4"].alignment = b_r_alignment |
||
| 690 | parameters_ws["F4"] = _("Reporting End Datetime") + ":" |
||
| 691 | parameters_ws["G4"].border = b_border |
||
| 692 | parameters_ws["G4"].alignment = b_c_alignment |
||
| 693 | parameters_ws["G4"] = reporting_end_datetime_local |
||
| 694 | |||
| 695 | parameters_ws_current_row_number = 6 |
||
| 696 | |||
| 697 | parameters_ws["B" + str(parameters_ws_current_row_number)].font = title_font |
||
| 698 | parameters_ws["B" + str(parameters_ws_current_row_number)] = ( |
||
| 699 | shopfloor2_name + " " + _("Parameters") |
||
| 700 | ) |
||
| 701 | |||
| 702 | parameters_ws_current_row_number += 1 |
||
| 703 | |||
| 704 | parameters_table_start_row_number = parameters_ws_current_row_number |
||
| 705 | |||
| 706 | parameters_ws.row_dimensions[parameters_ws_current_row_number].height = 80 |
||
| 707 | |||
| 708 | parameters_ws_current_row_number += 1 |
||
| 709 | |||
| 710 | table_current_col_number = 2 |
||
| 711 | |||
| 712 | for i in range(0, parameters_names_len): |
||
| 713 | |||
| 714 | if len(parameters_data2["timestamps"][i]) == 0: |
||
| 715 | continue |
||
| 716 | |||
| 717 | col = format_cell.get_column_letter(table_current_col_number) |
||
| 718 | |||
| 719 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = ( |
||
| 720 | table_fill |
||
| 721 | ) |
||
| 722 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = ( |
||
| 723 | f_border |
||
| 724 | ) |
||
| 725 | |||
| 726 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
| 727 | |||
| 728 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].fill = ( |
||
| 729 | table_fill |
||
| 730 | ) |
||
| 731 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].border = ( |
||
| 732 | f_border |
||
| 733 | ) |
||
| 734 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].font = ( |
||
| 735 | name_font |
||
| 736 | ) |
||
| 737 | parameters_ws[col + str(parameters_ws_current_row_number - 1)].alignment = ( |
||
| 738 | c_c_alignment |
||
| 739 | ) |
||
| 740 | parameters_ws[col + str(parameters_ws_current_row_number - 1)] = ( |
||
| 741 | parameters_data2["names"][i] |
||
| 742 | ) |
||
| 743 | |||
| 744 | table_current_row_number = parameters_ws_current_row_number |
||
| 745 | |||
| 746 | for j, value in enumerate(list(parameters_data2["timestamps"][i])): |
||
| 747 | col = format_cell.get_column_letter(table_current_col_number) |
||
| 748 | |||
| 749 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
| 750 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
| 751 | parameters_ws[col + str(table_current_row_number)].alignment = ( |
||
| 752 | c_c_alignment |
||
| 753 | ) |
||
| 754 | parameters_ws[col + str(table_current_row_number)] = value |
||
| 755 | |||
| 756 | col = format_cell.get_column_letter(table_current_col_number + 1) |
||
| 757 | |||
| 758 | parameters_ws[col + str(table_current_row_number)].border = f_border |
||
| 759 | parameters_ws[col + str(table_current_row_number)].font = title_font |
||
| 760 | parameters_ws[col + str(table_current_row_number)].alignment = ( |
||
| 761 | c_c_alignment |
||
| 762 | ) |
||
| 763 | try: |
||
| 764 | parameters_ws[col + str(table_current_row_number)] = round2( |
||
| 765 | parameters_data2["values"][i][j], 2 |
||
| 766 | ) |
||
| 767 | except Exception as e: |
||
| 768 | print("error 1 in excelexporters\\shopfloorcomparison: " + str(e)) |
||
| 769 | |||
| 770 | table_current_row_number += 1 |
||
| 771 | |||
| 772 | table_current_col_number = table_current_col_number + 3 |
||
| 773 | |||
| 774 | ################################################################################################################ |
||
| 775 | # parameters chart and parameters table |
||
| 776 | ################################################################################################################ |
||
| 777 | |||
| 778 | ws["B" + str(current_sheet_parameters_row_number)].font = title_font |
||
| 779 | ws["B" + str(current_sheet_parameters_row_number)] = ( |
||
| 780 | shopfloor1_name + " " + _("Parameters") |
||
| 781 | ) |
||
| 782 | parameters_names_len = len(report["parameters1"]["names"]) |
||
| 783 | parameters_ws = wb[file_name + "Parameters1"] |
||
| 784 | |||
| 785 | current_sheet_parameters_row_number += 1 |
||
| 786 | |||
| 787 | chart_start_row_number = current_sheet_parameters_row_number |
||
| 788 | |||
| 789 | col_index = 0 |
||
| 790 | |||
| 791 | for i in range(0, parameters_names_len): |
||
| 792 | |||
| 793 | if len(parameters_data1["timestamps"][i]) == 0: |
||
| 794 | continue |
||
| 795 | |||
| 796 | line = LineChart() |
||
| 797 | data_col = 3 + col_index * 3 |
||
| 798 | labels_col = 2 + col_index * 3 |
||
| 799 | col_index += 1 |
||
| 800 | line.title = ( |
||
| 801 | _("Parameters") |
||
| 802 | + " - " |
||
| 803 | + parameters_ws.cell( |
||
| 804 | row=parameters_table_start_row_number, column=data_col |
||
| 805 | ).value |
||
| 806 | ) |
||
| 807 | labels = Reference( |
||
| 808 | parameters_ws, |
||
| 809 | min_col=labels_col, |
||
| 810 | min_row=parameters_table_start_row_number + 1, |
||
| 811 | max_row=( |
||
| 812 | len(parameters_data1["timestamps"][i]) |
||
| 813 | + parameters_table_start_row_number |
||
| 814 | ), |
||
| 815 | ) |
||
| 816 | line_data = Reference( |
||
| 817 | parameters_ws, |
||
| 818 | min_col=data_col, |
||
| 819 | min_row=parameters_table_start_row_number, |
||
| 820 | max_row=( |
||
| 821 | len(parameters_data1["timestamps"][i]) |
||
| 822 | + parameters_table_start_row_number |
||
| 823 | ), |
||
| 824 | ) |
||
| 825 | line.add_data(line_data, titles_from_data=True) |
||
| 826 | line.set_categories(labels) |
||
| 827 | line_data = line.series[0] |
||
| 828 | line_data.marker.symbol = "auto" |
||
| 829 | line_data.smooth = True |
||
| 830 | line.x_axis.crosses = "min" |
||
| 831 | line.height = 8.25 |
||
| 832 | line.width = 24 |
||
| 833 | chart_col = "B" |
||
| 834 | chart_cell = chart_col + str(chart_start_row_number) |
||
| 835 | chart_start_row_number += 6 |
||
| 836 | ws.add_chart(line, chart_cell) |
||
| 837 | |||
| 838 | current_sheet_parameters_row_number = chart_start_row_number |
||
| 839 | |||
| 840 | current_sheet_parameters_row_number += 1 |
||
| 841 | |||
| 842 | parameters_ws = wb[file_name + "Parameters2"] |
||
| 843 | ws["B" + str(current_sheet_parameters_row_number)].font = title_font |
||
| 844 | ws["B" + str(current_sheet_parameters_row_number)] = ( |
||
| 845 | shopfloor2_name + " " + _("Parameters") |
||
| 846 | ) |
||
| 847 | |||
| 848 | current_sheet_parameters_row_number += 1 |
||
| 849 | |||
| 850 | chart_start_row_number = current_sheet_parameters_row_number |
||
| 851 | |||
| 852 | col_index = 0 |
||
| 853 | |||
| 854 | parameters_names_len = len(report["parameters2"]["names"]) |
||
| 855 | |||
| 856 | for i in range(0, parameters_names_len): |
||
| 857 | |||
| 858 | if len(parameters_data2["timestamps"][i]) == 0: |
||
| 859 | continue |
||
| 860 | |||
| 861 | line = LineChart() |
||
| 862 | data_col = 3 + col_index * 3 |
||
| 863 | labels_col = 2 + col_index * 3 |
||
| 864 | col_index += 1 |
||
| 865 | line.title = ( |
||
| 866 | _("Parameters") |
||
| 867 | + " - " |
||
| 868 | + parameters_ws.cell( |
||
| 869 | row=parameters_table_start_row_number, column=data_col |
||
| 870 | ).value |
||
| 871 | ) |
||
| 872 | labels = Reference( |
||
| 873 | parameters_ws, |
||
| 874 | min_col=labels_col, |
||
| 875 | min_row=parameters_table_start_row_number + 1, |
||
| 876 | max_row=( |
||
| 877 | len(parameters_data2["timestamps"][i]) |
||
| 878 | + parameters_table_start_row_number |
||
| 879 | ), |
||
| 880 | ) |
||
| 881 | line_data = Reference( |
||
| 882 | parameters_ws, |
||
| 883 | min_col=data_col, |
||
| 884 | min_row=parameters_table_start_row_number, |
||
| 885 | max_row=( |
||
| 886 | len(parameters_data2["timestamps"][i]) |
||
| 887 | + parameters_table_start_row_number |
||
| 888 | ), |
||
| 889 | ) |
||
| 890 | line.add_data(line_data, titles_from_data=True) |
||
| 891 | line.set_categories(labels) |
||
| 892 | line_data = line.series[0] |
||
| 893 | line_data.marker.symbol = "auto" |
||
| 894 | line_data.smooth = True |
||
| 895 | line.x_axis.crosses = "min" |
||
| 896 | line.height = 8.25 |
||
| 897 | line.width = 24 |
||
| 898 | chart_col = "B" |
||
| 899 | chart_cell = chart_col + str(chart_start_row_number) |
||
| 900 | chart_start_row_number += 6 |
||
| 901 | ws.add_chart(line, chart_cell) |
||
| 902 | |||
| 903 | current_sheet_parameters_row_number = chart_start_row_number |
||
| 904 | |||
| 905 | current_sheet_parameters_row_number += 1 |
||
| 906 | |||
| 907 | filename = str(uuid.uuid4()) + ".xlsx" |
||
| 908 | wb.save(filename) |
||
| 909 | |||
| 910 | return filename |
||
| 911 | |||
| 912 | |||
| 913 | def timestamps_data_all_equal_0(lists): |
||
| 914 | for i, value in enumerate(list(lists)): |
||
| 915 | if len(value) > 0: |
||
| 916 | return False |
||
| 917 | |||
| 918 | return True |
||
| 919 | |||
| 920 | |||
| 921 | def get_parameters_timestamps_lists_max_len(parameters_timestamps_lists): |
||
| 922 | max_len = 0 |
||
| 923 | for i, value in enumerate(list(parameters_timestamps_lists)): |
||
| 924 | if len(value) > max_len: |
||
| 925 | max_len = len(value) |
||
| 926 | |||
| 927 | return max_len |
||
| 928 | |||
| 929 | |||
| 930 | def timestamps_data_not_equal_0(lists): |
||
| 931 | number = 0 |
||
| 932 | for i, value in enumerate(list(lists)): |
||
| 933 | if len(value) > 0: |
||
| 934 | number += 1 |
||
| 935 | return number |
||
| 936 |