11from datetime import datetime
22from typing import Optional , TypedDict
33
4- from django .db import connection
4+ from django .db import connection , connections
55
66from kernelCI_app .cache import get_query_cache , set_query_cache
77from kernelCI_app .helpers .database import dict_fetchall
1212from kernelCI_app .typeModels .hardwareDetails import CommitHead , Tree
1313
1414
15- def _get_hardware_tree_heads_clause (* , id_only : bool ) -> str :
16- """Returns the tree_heads for the hardware queries,
17- where the checkout is not filtered by origin.
15+ def _get_hardware_trees_from_status_query (* , fields : str ) -> str :
16+ """Latest checkout per tree in the window for this hardware via hardware_status.
1817
19- This is done because tests from a origin can be
20- related to checkouts from another origin."""
21- if id_only is True :
22- fields = "C.id"
23- else :
24- fields = """C.id,
25- C.origin,
26- C.tree_name,
27- C.start_time,
28- C.git_repository_branch,
29- C.git_repository_url,
30- C.git_commit_name,
31- C.git_commit_hash,
32- C.git_commit_tags"""
33-
34- return f"""SELECT DISTINCT
35- ON (
36- C.tree_name,
37- C.git_repository_branch,
38- C.git_repository_url,
39- C.origin
40- )
41- { fields }
42- FROM
43- checkouts C
44- WHERE
45- C.start_time >= %(start_date)s
46- AND C.start_time <= %(end_date)s
47- ORDER BY
48- C.tree_name ASC,
49- C.git_repository_branch ASC,
50- C.git_repository_url ASC,
51- C.origin ASC,
52- C.start_time DESC"""
18+ Checkout origin is not filtered: tests from one origin can be related to
19+ checkouts from another origin.
20+ """
21+ return f"""
22+ SELECT DISTINCT ON (
23+ C.tree_name,
24+ C.git_repository_branch,
25+ C.git_repository_url,
26+ C.origin
27+ )
28+ { fields }
29+ FROM
30+ hardware_status HS
31+ INNER JOIN checkouts C ON C.id = HS.checkout_id
32+ WHERE
33+ HS.test_origin = %(origin)s
34+ AND (
35+ HS.platform = %(hardware)s
36+ OR HS.compatibles @> ARRAY[%(hardware)s]::TEXT[]
37+ )
38+ AND HS.start_time >= %(start_date)s
39+ AND HS.start_time <= %(end_date)s
40+ ORDER BY
41+ C.tree_name ASC,
42+ C.git_repository_branch ASC,
43+ C.git_repository_url ASC,
44+ C.origin ASC,
45+ C.start_time DESC
46+ """
5347
5448
5549# TODO: unify with get_tree_listing_count_clause
@@ -756,11 +750,7 @@ def get_hardware_trees_head_commits(
756750 start_datetime : datetime ,
757751 end_datetime : datetime ,
758752) -> list [tuple [str , str ]]:
759-
760- # similar to the get_hardware_trees_data, except we limit the information
761- # being brought to the commit hash
762-
763- cache_key = "hardwareTreesHeadCommits"
753+ cache_key = "hardwareTreesHeadCommitsFromStatus"
764754
765755 cache_params = {
766756 "hardware" : hardware_id ,
@@ -774,54 +764,20 @@ def get_hardware_trees_head_commits(
774764 if trees :
775765 return trees
776766
777- tree_head_clause = _get_hardware_tree_heads_clause (id_only = False )
778-
779- # We need a subquery because if we filter by any hardware, it will get the
780- # last head that has that hardware, but not the real head of the trees
781- query = f"""
782- WITH
783- -- Selects the data of the latest checkout of all trees in the given period
784- tree_heads AS (
785- { tree_head_clause }
786- )
787- SELECT DISTINCT
788- ON (
789- TH.tree_name,
790- TH.git_repository_branch,
791- TH.git_repository_url,
792- TH.git_commit_hash
793- ) TH.tree_name,
794- TH.git_commit_hash
795- FROM
796- tests
797- INNER JOIN builds ON tests.build_id = builds.id
798- INNER JOIN tree_heads TH ON builds.checkout_id = TH.id
799- WHERE
800- (
801- (
802- tests.environment_compatible @> ARRAY[%(hardware)s]::TEXT[]
803- OR tests.environment_misc ->> 'platform' = %(hardware)s
804- )
805- AND tests.origin = %(origin)s
806- AND TH.start_time >= %(start_date)s
807- AND TH.start_time <= %(end_date)s
808- )
809- ORDER BY
810- TH.tree_name ASC,
811- TH.git_repository_branch ASC,
812- TH.git_repository_url ASC,
813- TH.git_commit_hash ASC,
814- TH.start_time DESC
815- """
767+ query = _get_hardware_trees_from_status_query (
768+ fields = """
769+ C.tree_name,
770+ C.git_commit_hash
771+ """
772+ )
816773
817774 params = {
818775 "hardware" : hardware_id ,
819776 "origin" : origin ,
820777 "start_date" : start_datetime ,
821778 "end_date" : end_datetime ,
822779 }
823- trees = []
824- with connection .cursor () as cursor :
780+ with connections ["default" ].cursor () as cursor :
825781 cursor .execute (query , params )
826782 tree_records = dict_fetchall (cursor )
827783 trees = [
@@ -840,7 +796,7 @@ def get_hardware_trees_data(
840796 start_datetime : datetime ,
841797 end_datetime : datetime ,
842798) -> list [Tree ]:
843- cache_key = "hardwareDetailsTreeData "
799+ cache_key = "hardwareDetailsTreeDataFromStatus "
844800
845801 params = {
846802 "hardware" : hardware_id ,
@@ -851,52 +807,19 @@ def get_hardware_trees_data(
851807
852808 trees : list [Tree ] = get_query_cache (cache_key , params )
853809
854- tree_head_clause = _get_hardware_tree_heads_clause (id_only = False )
855-
856810 if not trees :
857- # We need a subquery because if we filter by any hardware, it will get the
858- # last head that has that hardware, but not the real head of the trees
859- query = f"""
860- WITH
861- -- Selects the data of the latest checkout of all trees in the given period
862- tree_heads AS (
863- { tree_head_clause }
864- )
865- SELECT DISTINCT
866- ON (
867- TH.tree_name,
868- TH.git_repository_branch,
869- TH.git_repository_url,
870- TH.git_commit_hash
871- ) TH.tree_name,
872- TH.origin,
873- TH.git_repository_branch,
874- TH.git_repository_url,
875- TH.git_commit_name,
876- TH.git_commit_hash,
877- TH.git_commit_tags
878- FROM
879- tests
880- INNER JOIN builds ON tests.build_id = builds.id
881- INNER JOIN tree_heads TH ON builds.checkout_id = TH.id
882- WHERE
883- (
884- (
885- tests.environment_compatible @> ARRAY[%(hardware)s]::TEXT[]
886- OR tests.environment_misc ->> 'platform' = %(hardware)s
887- )
888- AND tests.origin = %(origin)s
889- AND TH.start_time >= %(start_date)s
890- AND TH.start_time <= %(end_date)s
891- )
892- ORDER BY
893- TH.tree_name ASC,
894- TH.git_repository_branch ASC,
895- TH.git_repository_url ASC,
896- TH.git_commit_hash ASC,
897- TH.start_time DESC
898- """
899- with connection .cursor () as cursor :
811+ query = _get_hardware_trees_from_status_query (
812+ fields = """
813+ C.tree_name,
814+ C.origin,
815+ C.git_repository_branch,
816+ C.git_repository_url,
817+ C.git_commit_name,
818+ C.git_commit_hash,
819+ C.git_commit_tags
820+ """
821+ )
822+ with connections ["default" ].cursor () as cursor :
900823 cursor .execute (query , params )
901824 tree_records = dict_fetchall (cursor )
902825
0 commit comments