-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add distro and table selection to excel report page #413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| import logging | ||
| import re | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from django.core.management import call_command | ||
| from django.http import FileResponse, Http404, HttpRequest, JsonResponse | ||
|
|
@@ -13,11 +13,12 @@ | |
| from django.views.decorators.csrf import csrf_exempt | ||
| from django.views.decorators.http import require_http_methods | ||
| from pydantic_core._pydantic_core import ValidationError | ||
| from sqlalchemy import create_engine | ||
| from sqlalchemy import Engine, create_engine, text | ||
|
|
||
| from imgtests.constant import CONFIG_DIR, EXCEL_REPORTS_DIR, REPORTS_DIR | ||
| from imgtests.constant import CONFIG_DIR, DISTRIBUTION_DESCRIPTIONS, EXCEL_REPORTS_DIR, REPORTS_DIR | ||
| from imgtests.database.database import ImgtestsDatabase, PostgresCreds | ||
| from imgtests.reporting.excel_export import export_database_to_excel | ||
| from imgtests.reporting.cli import EXPORT_TABLES | ||
| from imgtests.reporting.excel_export import distribution_name_from_os, export_database_to_excel | ||
| from imgtests.reporting.html_report import ReportGenerator | ||
| from imgtests.runner import get_test_name | ||
| from imgtests.suites.map import ALL_SUITES | ||
|
|
@@ -375,9 +376,21 @@ def __safe_relative_path(*parts: str) -> Path: | |
|
|
||
| @csrf_exempt | ||
| @require_http_methods(["POST"]) | ||
| def api_export_excel(request: HttpRequest) -> JsonResponse: # noqa: ARG001 | ||
| def api_export_excel(request: HttpRequest) -> JsonResponse: | ||
| try: | ||
| body = json.loads(request.body) if request.body else {} | ||
| except json.JSONDecodeError: | ||
| return JsonResponse({"error": "Invalid request body"}, status=400) | ||
|
|
||
| tables = body.get("tables", list(EXPORT_TABLES)) | ||
| selected_distributions = body.get("distributions") | ||
|
|
||
| if not tables: | ||
| return JsonResponse({"error": "At least one table must be selected"}, status=400) | ||
|
|
||
| timestamp = timezone.now().strftime("%Y%m%d_%H%M%S") | ||
| output_path = EXCEL_REPORTS_DIR / f"export_{timestamp}.xlsx" | ||
|
|
||
| try: | ||
| db_creds = PostgresCreds() | ||
| except ValidationError: | ||
|
|
@@ -391,10 +404,12 @@ def api_export_excel(request: HttpRequest) -> JsonResponse: # noqa: ARG001 | |
|
|
||
| try: | ||
| engine = create_engine(db_url) | ||
| configuration_ids = _resolve_configuration_ids(engine, selected_distributions) | ||
| export_database_to_excel( | ||
| engine=engine, | ||
| output_path=output_path, | ||
| configuration_ids={"poky": 1, "suse": 2}, | ||
| configuration_ids=configuration_ids, | ||
| tables=tables, | ||
| ) | ||
| except Exception as err: # noqa: BLE001 | ||
| return JsonResponse({"error": f"Export failed: {err}"}, status=500) | ||
|
|
@@ -410,10 +425,37 @@ def api_export_excel(request: HttpRequest) -> JsonResponse: # noqa: ARG001 | |
| ) | ||
|
|
||
|
|
||
| def _resolve_configuration_ids( | ||
| engine: Engine, | ||
| selected_distributions: list[str] | None = None, | ||
| ) -> dict[str, int]: | ||
|
|
||
| with engine.connect() as conn: | ||
| result = conn.execute(text("SELECT config_id, os FROM configuration")) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачем так, а не средствами ORM? |
||
| configs = result.all() | ||
|
|
||
| all_distros: dict[str, int] = {} | ||
| for config_id, os_name in configs: | ||
| distro = distribution_name_from_os(os_name) | ||
| if distro and distro not in all_distros: | ||
| all_distros[distro] = config_id | ||
|
Comment on lines
+438
to
+441
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут бы комментарий на будещее оставить вроде |
||
|
|
||
| if selected_distributions is not None: | ||
| missing = [d for d in selected_distributions if d not in all_distros] | ||
| if missing: | ||
| msg = f"No configuration found for: {', '.join(missing)}." | ||
| raise ValueError(msg) | ||
| return {d: all_distros[d] for d in selected_distributions if d in all_distros} | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| return all_distros | ||
|
|
||
|
|
||
| def excel_report_list(request: HttpRequest) -> HttpResponse: | ||
| context: dict[str, Any] = {"distributions": DISTRIBUTION_DESCRIPTIONS} | ||
| reports: list[dict[str, str | float]] = [] | ||
| if not EXCEL_REPORTS_DIR.exists(): | ||
| return render(request, "tests_interface/excel_reports.html", {"reports": reports}) | ||
| context["reports"] = reports | ||
| return render(request, "tests_interface/excel_reports.html", context) | ||
|
|
||
| for file_path in sorted(EXCEL_REPORTS_DIR.glob("*.xlsx"), reverse=True): | ||
| stat = file_path.stat() | ||
|
|
@@ -425,7 +467,8 @@ def excel_report_list(request: HttpRequest) -> HttpResponse: | |
| }, | ||
| ) | ||
|
|
||
| return render(request, "tests_interface/excel_reports.html", {"reports": reports}) | ||
| context["reports"] = reports | ||
| return render(request, "tests_interface/excel_reports.html", context) | ||
|
|
||
|
|
||
| def download_excel_report(request: HttpRequest, filename: str) -> FileResponse: # noqa: ARG001 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это условие не нужно, уже проверено на 259-270 строках.