-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: optimise queries for index page with tags
- Loading branch information
Showing
10 changed files
with
253 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'pact_broker/verifications/repository' | ||
|
||
# A collection of matrix rows with the same pact publication id | ||
# It's basically a normalised view of a denormalised view :( | ||
|
||
module PactBroker | ||
module Matrix | ||
class AggregatedRow | ||
extend Forwardable | ||
|
||
delegate [:consumer, :consumer_name, :consumer_version, :consumer_version_number, :consumer_version_order, :consumer_version_tags] => :first_row | ||
delegate [:provider, :provider_name, :provider_version, :provider_version_number, :provider_version_order, :provider_version_tags] => :first_row | ||
delegate [:pact, :pact_version, :pact_revision_number, :webhooks, :latest_triggered_webhooks, :'<=>'] => :first_row | ||
delegate [:verification_id, :verification] => :first_row | ||
|
||
|
||
def initialize matrix_rows | ||
@matrix_rows = matrix_rows | ||
@first_row = matrix_rows.first | ||
end | ||
|
||
def overall_latest? | ||
!!matrix_rows.find{ | row| row.consumer_version_tag_name.nil? } | ||
end | ||
|
||
def latest_verification | ||
@latest_verification ||= begin | ||
verification = matrix_rows.collect do | row| | ||
row.verification || row.latest_verification_for_consumer_version_tag | ||
end.compact.sort{ |v1, v2| v1.id <=> v2.id}.last | ||
|
||
if !verification && overall_latest? | ||
PactBroker::Verifications::Repository.new.find_latest_verification_for(consumer_name, provider_name) | ||
else | ||
verification | ||
end | ||
end | ||
end | ||
|
||
def consumer_head_tag_names | ||
matrix_rows.collect(&:consumer_version_tag_name).compact | ||
end | ||
|
||
private | ||
|
||
attr_reader :matrix_rows | ||
|
||
def first_row | ||
@first_row | ||
end | ||
|
||
def consumer_version_tag_names | ||
matrix_rows.collect(&:consumer_version_tag_name) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require 'pact_broker/matrix/aggregated_row' | ||
|
||
module PactBroker | ||
module Matrix | ||
describe AggregatedRow do | ||
describe "latest_verification" do | ||
let(:row_1) do | ||
instance_double('PactBroker::Matrix::HeadRow', | ||
consumer_name: "Foo", | ||
provider_name: "Bar", | ||
verification: verification_1, | ||
latest_verification_for_consumer_version_tag: tag_verification_1, | ||
consumer_version_tag_name: consumer_version_tag_name_1) | ||
end | ||
let(:row_2) do | ||
instance_double('PactBroker::Matrix::HeadRow', | ||
verification: verification_2, | ||
latest_verification_for_consumer_version_tag: tag_verification_2, | ||
consumer_version_tag_name: consumer_version_tag_name_2) | ||
end | ||
let(:verification_1) { instance_double('PactBroker::Domain::Verification', id: 1) } | ||
let(:verification_2) { instance_double('PactBroker::Domain::Verification', id: 2) } | ||
let(:tag_verification_1) { instance_double('PactBroker::Domain::Verification', id: 3) } | ||
let(:tag_verification_2) { instance_double('PactBroker::Domain::Verification', id: 4) } | ||
let(:consumer_version_tag_name_1) { 'master' } | ||
let(:consumer_version_tag_name_2) { 'prod' } | ||
let(:rows) { [row_1, row_2] } | ||
let(:aggregated_row) { AggregatedRow.new(rows) } | ||
|
||
subject { aggregated_row.latest_verification } | ||
|
||
context "when the rows have verifications" do | ||
it "returns the verification with the largest id" do | ||
expect(subject).to be verification_2 | ||
end | ||
end | ||
|
||
context "when the rows do not have verifications, but there are a previous verifications for a pacts with the same tag" do | ||
let(:verification_1) { nil } | ||
let(:verification_2) { nil } | ||
|
||
it "returns the verification for the previous pact that has the largest id" do | ||
expect(subject).to be tag_verification_2 | ||
end | ||
end | ||
|
||
context "when there is no verification for any of the rows or any of the pacts with the same tag" do | ||
before do | ||
allow_any_instance_of(PactBroker::Verifications::Repository).to receive(:find_latest_verification_for).and_return(overall_latest_verification) | ||
end | ||
|
||
let(:overall_latest_verification) { instance_double('PactBroker::Domain::Verification', id: 5) } | ||
let(:verification_1) { nil } | ||
let(:verification_2) { nil } | ||
let(:tag_verification_1) { nil } | ||
let(:tag_verification_2) { nil } | ||
|
||
context "when one of the rows is the overall latest" do | ||
let(:consumer_version_tag_name_1) { nil } | ||
|
||
it "looks up the overall latest verification" do | ||
expect_any_instance_of(PactBroker::Verifications::Repository).to receive(:find_latest_verification_for).with("Foo", "Bar") | ||
subject | ||
end | ||
|
||
it "returns the overall latest verification" do | ||
expect(subject).to be overall_latest_verification | ||
end | ||
end | ||
|
||
context "when none of the rows is not the overall latest (they are all the latest with a tag)" do | ||
it "returns nil" do | ||
expect(subject).to be nil | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.