@@ -18,6 +18,14 @@ class Service
18
18
# supporting both
19
19
20
20
def self . find_index_items options = { }
21
+ if options [ :optimised ]
22
+ find_index_items_optimised ( options )
23
+ else
24
+ find_index_items_original ( options )
25
+ end
26
+ end
27
+
28
+ def self . find_index_items_original options = { }
21
29
rows = PactBroker ::Matrix ::HeadRow
22
30
. select_all_qualified
23
31
. eager ( :latest_triggered_webhooks )
@@ -56,6 +64,89 @@ def self.find_index_items options = {}
56
64
end
57
65
end
58
66
67
+ def self . find_index_items_optimised options = { }
68
+ pact_publication_ids = nil
69
+ latest_verifications_for_cv_tags = nil
70
+
71
+ if !options [ :tags ]
72
+ # server side rendered index page without tags
73
+ pact_publication_ids = latest_pact_publications . select ( :id )
74
+ else
75
+ # server side rendered index page with tags=true or tags[]=a&tags=[]b
76
+ if options [ :tags ] . is_a? ( Array )
77
+ # TODO test for this
78
+ pact_publication_ids = head_pact_publications_ids_for_tags ( options [ :tags ] )
79
+ latest_verifications_for_cv_tags = PactBroker ::Verifications ::LatestVerificationForConsumerVersionTag
80
+ . eager ( :provider_version )
81
+ . where ( consumer_version_tag_name : options [ :tags ] ) . all
82
+ else
83
+ pact_publication_ids = head_pact_publications_ids
84
+ latest_verifications_for_cv_tags = PactBroker ::Verifications ::LatestVerificationForConsumerVersionTag . eager ( :provider_version ) . all
85
+ end
86
+ end
87
+
88
+ latest_pact_publication_ids = latest_pact_publications . select ( :id ) . all . collect { |h | h [ :id ] }
89
+
90
+ # We only need to know if a webhook exists for an integration, not what its properties are
91
+ webhooks = PactBroker ::Webhooks ::Webhook . select ( :consumer_id , :provider_id ) . distinct . all
92
+
93
+ pact_publications = PactBroker ::Pacts ::PactPublication
94
+ . where ( id : pact_publication_ids )
95
+ . select_all_qualified
96
+ . eager ( :consumer )
97
+ . eager ( :provider )
98
+ . eager ( :pact_version )
99
+ . eager ( integration : [ { latest_verification : :provider_version } , :latest_triggered_webhooks ] )
100
+ . eager ( :consumer_version )
101
+ . eager ( latest_verification : { provider_version : :tags_with_latest_flag } )
102
+ . eager ( :head_pact_tags )
103
+
104
+ pact_publications . all . collect do | pact_publication |
105
+
106
+ is_overall_latest_for_integration = latest_pact_publication_ids . include? ( pact_publication . id )
107
+ latest_verification = latest_verification_for_pseudo_branch ( pact_publication , is_overall_latest_for_integration , latest_verifications_for_cv_tags , options [ :tags ] )
108
+ webhook = webhooks . find { |webhook | webhook . is_for? ( pact_publication . integration ) }
109
+
110
+ PactBroker ::Domain ::IndexItem . create (
111
+ pact_publication . consumer ,
112
+ pact_publication . provider ,
113
+ pact_publication . to_domain_lightweight ,
114
+ is_overall_latest_for_integration ,
115
+ latest_verification ,
116
+ webhook ? [ webhook ] : [ ] ,
117
+ pact_publication . integration . latest_triggered_webhooks ,
118
+ consumer_version_tags ( pact_publication , options [ :tags ] ) ,
119
+ options [ :tags ] && latest_verification ? latest_verification . provider_version . tags_with_latest_flag . select ( &:latest? ) : [ ]
120
+ )
121
+ end . sort
122
+ end
123
+
124
+ # Worst. Code. Ever.
125
+ #
126
+ def self . latest_verification_for_pseudo_branch ( pact_publication , is_overall_latest , latest_verifications_for_cv_tags , tags_option )
127
+ if tags_option == true
128
+ latest_verifications_for_cv_tags
129
+ . select { | v | v . consumer_id == pact_publication . consumer_id && v . provider_id == pact_publication . provider_id && pact_publication . head_pact_tags . collect ( &:name ) . include? ( v . consumer_version_tag_name ) }
130
+ . sort { |v1 , v2 | v1 . id <=> v2 . id } . last || ( is_overall_latest && pact_publication . integration . latest_verification )
131
+ elsif tags_option . is_a? ( Array )
132
+ latest_verifications_for_cv_tags
133
+ . select { | v | v . consumer_id == pact_publication . consumer_id && v . provider_id == pact_publication . provider_id && pact_publication . head_pact_tags . collect ( &:name ) . include? ( v . consumer_version_tag_name ) && tags_option . include? ( v . consumer_version_tag_name ) }
134
+ . sort { |v1 , v2 | v1 . id <=> v2 . id } . last || ( is_overall_latest && pact_publication . integration . latest_verification )
135
+ else
136
+ pact_publication . integration . latest_verification
137
+ end
138
+ end
139
+
140
+ def self . consumer_version_tags ( pact_publication , tags_option )
141
+ if tags_option == true
142
+ pact_publication . head_pact_tags . collect ( &:name )
143
+ elsif tags_option . is_a? ( Array )
144
+ pact_publication . head_pact_tags . collect ( &:name ) & tags_option
145
+ else
146
+ [ ]
147
+ end
148
+ end
149
+
59
150
def self . find_index_items_for_api ( consumer_name : nil , provider_name : nil , **ignored )
60
151
rows = PactBroker ::Matrix ::HeadRow
61
152
. eager ( :consumer_version_tags )
@@ -83,6 +174,22 @@ def self.find_index_items_for_api(consumer_name: nil, provider_name: nil, **igno
83
174
)
84
175
end
85
176
end
177
+
178
+ def self . latest_pact_publications
179
+ db [ :latest_pact_publications ]
180
+ end
181
+
182
+ def self . head_pact_publications_ids
183
+ db [ :head_pact_tags ] . select ( Sequel [ :pact_publication_id ] . as ( :id ) ) . union ( db [ :latest_pact_publications ] . select ( :id ) ) . limit ( 500 )
184
+ end
185
+
186
+ def self . head_pact_publications_ids_for_tags ( tag_names )
187
+ db [ :head_pact_tags ] . select ( Sequel [ :pact_publication_id ] . as ( :id ) ) . where ( name : tag_names ) . union ( db [ :latest_pact_publications ] . select ( :id ) ) . limit ( 500 )
188
+ end
189
+
190
+ def self . db
191
+ PactBroker ::Pacts ::PactPublication . db
192
+ end
86
193
end
87
194
end
88
195
end
0 commit comments