|
1 | | -from typing import Optional |
| 1 | +from typing import Literal, Optional |
2 | 2 | from django.db import connection |
3 | 3 | from django.db.models import Q |
4 | 4 |
|
@@ -465,6 +465,174 @@ def get_tree_details_data( |
465 | 465 | return rows |
466 | 466 |
|
467 | 467 |
|
| 468 | +def get_tree_data( |
| 469 | + *, |
| 470 | + data_type: Literal["builds", "boots", "tests"], |
| 471 | + origin_param: str, |
| 472 | + git_url_param: Optional[str], |
| 473 | + git_branch_param: Optional[str], |
| 474 | + commit_hash: Optional[str], |
| 475 | + tree_name: Optional[str] = None, |
| 476 | +) -> Optional[list[tuple]]: |
| 477 | + """Fetch build, boot, or test rows for a given tree commit.""" |
| 478 | + cache_key = f"treeDetails{data_type.capitalize()}" |
| 479 | + |
| 480 | + params = { |
| 481 | + "commit_hash": commit_hash, |
| 482 | + "tree_name": tree_name, |
| 483 | + "origin_param": origin_param, |
| 484 | + "git_url_param": git_url_param, |
| 485 | + "git_branch_param": git_branch_param, |
| 486 | + } |
| 487 | + |
| 488 | + rows = get_query_cache(cache_key, params) |
| 489 | + if rows is None: |
| 490 | + checkout_clauses = create_checkouts_where_clauses( |
| 491 | + git_url=git_url_param, |
| 492 | + git_branch=git_branch_param, |
| 493 | + tree_name=tree_name, |
| 494 | + ) |
| 495 | + |
| 496 | + git_branch_clause = checkout_clauses.get("git_branch_clause") |
| 497 | + tree_name_clause = checkout_clauses.get("tree_name_clause") |
| 498 | + git_url_clause = checkout_clauses.get("git_url_clause") |
| 499 | + tree_name_full_clause = "AND " + tree_name_clause if tree_name_clause else "" |
| 500 | + git_url_full_clause = "AND " + git_url_clause if git_url_clause else "" |
| 501 | + |
| 502 | + is_boots = data_type == "boots" |
| 503 | + is_tests = data_type == "tests" |
| 504 | + include_test_cols = is_boots or is_tests |
| 505 | + |
| 506 | + tests_select = ( |
| 507 | + """ |
| 508 | + tests.id AS tests_id, |
| 509 | + tests.origin, |
| 510 | + tests.environment_comment AS tests_environment_comment, |
| 511 | + tests.environment_misc AS tests_environment_misc, |
| 512 | + tests.path AS tests_path, |
| 513 | + tests.comment AS tests_comment, |
| 514 | + tests.log_url AS tests_log_url, |
| 515 | + tests.status AS tests_status, |
| 516 | + tests.start_time AS tests_start_time, |
| 517 | + tests.duration AS tests_duration, |
| 518 | + tests.number_value AS tests_number_value, |
| 519 | + tests.misc AS tests_misc, |
| 520 | + tests.environment_compatible AS tests_environment_compatible,""" |
| 521 | + if include_test_cols |
| 522 | + else """ |
| 523 | + NULL AS tests_id, |
| 524 | + NULL AS tests_origin, |
| 525 | + NULL AS tests_environment_comment, |
| 526 | + NULL AS tests_environment_misc, |
| 527 | + NULL AS tests_path, |
| 528 | + NULL AS tests_comment, |
| 529 | + NULL AS tests_log_url, |
| 530 | + NULL AS tests_status, |
| 531 | + NULL AS tests_start_time, |
| 532 | + NULL AS tests_duration, |
| 533 | + NULL AS tests_number_value, |
| 534 | + NULL AS tests_misc, |
| 535 | + NULL AS tests_environment_compatible,""" |
| 536 | + ) |
| 537 | + |
| 538 | + tests_join = "" |
| 539 | + if is_boots: |
| 540 | + tests_join = ( |
| 541 | + "LEFT JOIN tests ON builds_filter.builds_id = tests.build_id" |
| 542 | + " AND (tests.path = 'boot' OR tests.path LIKE 'boot.%%')" |
| 543 | + ) |
| 544 | + elif is_tests: |
| 545 | + tests_join = ( |
| 546 | + "LEFT JOIN tests ON builds_filter.builds_id = tests.build_id" |
| 547 | + " AND tests.path <> 'boot' AND tests.path NOT LIKE 'boot.%%'" |
| 548 | + ) |
| 549 | + |
| 550 | + incidents_on = ( |
| 551 | + "tests.id = incidents.test_id" |
| 552 | + if include_test_cols |
| 553 | + else "builds_filter.builds_id = incidents.build_id" |
| 554 | + ) |
| 555 | + |
| 556 | + query = f""" |
| 557 | + WITH RELEVANT_HASH AS ( |
| 558 | + SELECT |
| 559 | + c.git_commit_hash |
| 560 | + FROM |
| 561 | + checkouts c |
| 562 | + WHERE |
| 563 | + c.git_commit_hash = %(commit_hash)s |
| 564 | + OR %(commit_hash)s = ANY (c.git_commit_tags) |
| 565 | + ORDER BY |
| 566 | + c._timestamp DESC |
| 567 | + LIMIT 1 |
| 568 | + ) |
| 569 | + SELECT |
| 570 | + {tests_select} |
| 571 | + builds_filter.*, |
| 572 | + incidents.id AS incidents_id, |
| 573 | + incidents.test_id AS incidents_test_id, |
| 574 | + incidents.present AS incidents_present, |
| 575 | + issues.id AS issues_id, |
| 576 | + issues.version AS issues_version, |
| 577 | + issues.comment AS issues_comment, |
| 578 | + issues.report_url AS issues_report_url |
| 579 | + FROM |
| 580 | + ( |
| 581 | + SELECT |
| 582 | + builds.id AS builds_id, |
| 583 | + builds.origin, |
| 584 | + builds.comment AS builds_comment, |
| 585 | + builds.start_time AS builds_start_time, |
| 586 | + builds.duration AS builds_duration, |
| 587 | + builds.architecture AS builds_architecture, |
| 588 | + builds.command AS builds_command, |
| 589 | + builds.compiler AS builds_compiler, |
| 590 | + builds.config_name AS builds_config_name, |
| 591 | + builds.config_url AS builds_config_url, |
| 592 | + builds.log_url AS builds_log_url, |
| 593 | + builds.status AS builds_valid, |
| 594 | + builds.misc AS builds_misc, |
| 595 | + tree_head.* |
| 596 | + FROM |
| 597 | + ( |
| 598 | + SELECT |
| 599 | + checkouts.id AS checkout_id, |
| 600 | + checkouts.git_repository_url AS checkouts_git_repository_url, |
| 601 | + checkouts.git_repository_branch AS checkouts_git_repository_branch, |
| 602 | + checkouts.git_commit_tags AS checkout_git_commit_tags, |
| 603 | + checkouts.origin AS checkouts_origin |
| 604 | + FROM |
| 605 | + checkouts |
| 606 | + WHERE |
| 607 | + checkouts.git_commit_hash = ( |
| 608 | + SELECT git_commit_hash FROM RELEVANT_HASH |
| 609 | + ) |
| 610 | + {git_url_full_clause} |
| 611 | + {tree_name_full_clause} |
| 612 | + AND {git_branch_clause} |
| 613 | + AND checkouts.origin = %(origin_param)s |
| 614 | + ) AS tree_head |
| 615 | + LEFT JOIN builds |
| 616 | + ON tree_head.checkout_id = builds.checkout_id |
| 617 | + ) AS builds_filter |
| 618 | + {tests_join} |
| 619 | + LEFT JOIN incidents |
| 620 | + ON {incidents_on} |
| 621 | + LEFT JOIN issues |
| 622 | + ON incidents.issue_id = issues.id |
| 623 | + AND incidents.issue_version = issues.version |
| 624 | + ORDER BY |
| 625 | + issues."_timestamp" DESC |
| 626 | + """ |
| 627 | + |
| 628 | + with connection.cursor() as cursor: |
| 629 | + cursor.execute(query, params) |
| 630 | + rows = cursor.fetchall() |
| 631 | + set_query_cache(key=cache_key, params=params, rows=rows) |
| 632 | + |
| 633 | + return rows |
| 634 | + |
| 635 | + |
468 | 636 | GIT_BRANCH_FIELD = "git_repository_branch" |
469 | 637 | GIT_URL_FIELD = "git_repository_url" |
470 | 638 |
|
|
0 commit comments