|
14 | 14 | # limitations under the License. |
15 | 15 | # |
16 | 16 | import json |
| 17 | +import os |
| 18 | +from datetime import datetime |
17 | 19 | from http import HTTPStatus |
18 | 20 | from typing import Any, Dict, List, Optional |
19 | 21 |
|
| 22 | +import requests |
20 | 23 | from fastapi import APIRouter, BackgroundTasks, Depends, File, HTTPException, UploadFile |
21 | 24 | from fastapi.encoders import jsonable_encoder |
22 | 25 | from fastapi.responses import JSONResponse, StreamingResponse |
|
37 | 40 | selected_tests_from_execution, |
38 | 41 | ) |
39 | 42 | from app.version import version_information |
| 43 | +from test_collections.matter.sdk_tests.support.performance_tests.utils import ( |
| 44 | + create_summary_report, |
| 45 | +) |
| 46 | +from test_collections.matter.test_environment_config import TestEnvironmentConfigMatter |
40 | 47 |
|
41 | 48 | router = APIRouter() |
42 | 49 |
|
@@ -479,3 +486,87 @@ def import_test_run_execution( |
479 | 486 | status_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
480 | 487 | detail=str(error), |
481 | 488 | ) |
| 489 | + |
| 490 | + |
| 491 | +date_pattern_out_file = "%Y_%m_%d_%H_%M_%S" |
| 492 | + |
| 493 | + |
| 494 | +@router.post("/{id}/performance_summary") |
| 495 | +def generate_summary_log( |
| 496 | + *, |
| 497 | + db: Session = Depends(get_db), |
| 498 | + id: int, |
| 499 | + project_id: int, |
| 500 | +) -> JSONResponse: |
| 501 | + """ |
| 502 | + Imports a test run execution to the the given project_id. |
| 503 | + """ |
| 504 | + |
| 505 | + project = crud.project.get(db=db, id=project_id) |
| 506 | + |
| 507 | + if not project: |
| 508 | + raise HTTPException( |
| 509 | + status_code=HTTPStatus.NOT_FOUND, detail="Project not found" |
| 510 | + ) |
| 511 | + |
| 512 | + project_config = TestEnvironmentConfigMatter(**project.config) |
| 513 | + matter_qa_url = None |
| 514 | + LOGS_FOLDER = "/test_collections/logs" |
| 515 | + HOST_BACKEND = os.getenv("BACKEND_FILEPATH_ON_HOST") or "" |
| 516 | + HOST_OUT_FOLDER = HOST_BACKEND + LOGS_FOLDER |
| 517 | + |
| 518 | + if ( |
| 519 | + project_config.test_parameters |
| 520 | + and "matter_qa_url" in project_config.test_parameters |
| 521 | + ): |
| 522 | + matter_qa_url = project_config.test_parameters["matter_qa_url"] |
| 523 | + else: |
| 524 | + raise HTTPException( |
| 525 | + status_code=HTTPStatus.UNPROCESSABLE_ENTITY, |
| 526 | + detail="matter_qa_url must be configured", |
| 527 | + ) |
| 528 | + |
| 529 | + page = requests.get(f"{matter_qa_url}/home") |
| 530 | + if page.status_code is not int(HTTPStatus.OK): |
| 531 | + raise HTTPException( |
| 532 | + status_code=page.status_code, |
| 533 | + detail=( |
| 534 | + "The LogDisplay server is not responding.\n" |
| 535 | + "Verify if the tool was installed, configured and initiated properly" |
| 536 | + ), |
| 537 | + ) |
| 538 | + |
| 539 | + commissioning_method = project_config.dut_config.pairing_mode |
| 540 | + |
| 541 | + test_run_execution = crud.test_run_execution.get(db=db, id=id) |
| 542 | + if not test_run_execution: |
| 543 | + raise HTTPException( |
| 544 | + status_code=HTTPStatus.NOT_FOUND, detail="Test Run Execution not found" |
| 545 | + ) |
| 546 | + |
| 547 | + log_lines_list = log_utils.convert_execution_log_to_list( |
| 548 | + log=test_run_execution.log, json_entries=False |
| 549 | + ) |
| 550 | + |
| 551 | + timestamp = "" |
| 552 | + if test_run_execution.started_at: |
| 553 | + timestamp = test_run_execution.started_at.strftime(date_pattern_out_file) |
| 554 | + else: |
| 555 | + timestamp = datetime.now().strftime(date_pattern_out_file) |
| 556 | + |
| 557 | + tc_name, execution_time_folder = create_summary_report( |
| 558 | + timestamp, log_lines_list, commissioning_method |
| 559 | + ) |
| 560 | + |
| 561 | + target_dir = f"{HOST_OUT_FOLDER}/{execution_time_folder}/{tc_name}" |
| 562 | + url_report = f"{matter_qa_url}/home/displayLogFolder?dir_path={target_dir}" |
| 563 | + |
| 564 | + summary_report: dict = {} |
| 565 | + summary_report["url"] = url_report |
| 566 | + |
| 567 | + options: dict = {"media_type": "application/json"} |
| 568 | + |
| 569 | + return JSONResponse( |
| 570 | + jsonable_encoder(summary_report), |
| 571 | + **options, |
| 572 | + ) |
0 commit comments