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
feat: add contract_data_updated_at to integrations table to speed up dashboard query #617
Merged
bethesque
merged 16 commits into
master
from
PACT-1070-dashboard-performance-network-diagram-3
Jun 16, 2023
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8b0ce4d
feat: add contract_data_updated_at to integrations table to speed up …
bethesque 3b90aa6
chore: add missing method
bethesque 41cadfc
chore: commit missing end
bethesque d02e2f3
chore: commit include
bethesque 4ee694b
chore: populate the new contract_data_updated_at field for existing i…
bethesque 6bf7437
test: update to use methods already committed
bethesque 4ca27e4
refactor: move subscription into separate file
bethesque 06ac966
tests: include updated dataset module
bethesque ed0a0de
docs: comment
bethesque d62a214
docs: update
bethesque 42e1f2f
style: whitespace
bethesque ed07cff
test: update for MySQL
bethesque 791a03f
chore: add data migration
bethesque d2a7312
test: use fancy date matching logic for mysql
bethesque 209f862
test: update for mysql
bethesque 175bad3
test: update for mysql
bethesque File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
db/migrations/20230615_add_integrations_contract_data_updated_at.rb
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,13 @@ | ||
Sequel.migration do | ||
up do | ||
alter_table(:integrations) do | ||
add_column(:contract_data_updated_at, DateTime) | ||
end | ||
end | ||
|
||
down do | ||
alter_table(:integrations) do | ||
drop_column(:contract_data_updated_at) | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
db/migrations/20230616_set_integrations_contract_data_updated_at.rb
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,11 @@ | ||
require "pact_broker/db/data_migrations/set_contract_data_updated_at_for_integrations" | ||
|
||
Sequel.migration do | ||
up do | ||
PactBroker::DB::DataMigrations::SetContractDataUpdatedAtForIntegrations.call(self) | ||
end | ||
|
||
down do | ||
|
||
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
47 changes: 47 additions & 0 deletions
47
lib/pact_broker/db/data_migrations/set_contract_data_updated_at_for_integrations.rb
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,47 @@ | ||
# Populate the newly created contract_data_updated_at date in the integrations table | ||
# using the latest created_at date from the pact_publications or verifications tables. | ||
module PactBroker | ||
module DB | ||
module DataMigrations | ||
class SetContractDataUpdatedAtForIntegrations | ||
def self.call(connection) | ||
join = { | ||
Sequel[:integrations][:consumer_id] => Sequel[:target][:consumer_id], | ||
Sequel[:integrations][:provider_id] => Sequel[:target][:provider_id] | ||
} | ||
|
||
max_created_at_for_each_integration = integrations_max_created_at(connection).from_self(alias: :target).select(:created_at).where(join) | ||
|
||
connection[:integrations] | ||
.where(contract_data_updated_at: nil) | ||
.update(contract_data_updated_at: max_created_at_for_each_integration) | ||
end | ||
|
||
# @return [Sequel::Dataset] the overall max created_at from the union of the pact_publications and verifications tables, | ||
# for each integration keyed by consumer_id/provider_id | ||
def self.integrations_max_created_at(connection) | ||
pact_publication_max_created_at(connection) | ||
.union(verification_max_created_at(connection)) | ||
.select_group(:consumer_id, :provider_id) | ||
.select_append{ max(:created_at).as(:created_at) } | ||
end | ||
|
||
# @return [Sequel::Dataset] the max created_at from the pact_publications table | ||
# for each integration keyed by consumer_id/provider_id | ||
def self.pact_publication_max_created_at(connection) | ||
connection[:pact_publications] | ||
.select_group(:consumer_id, :provider_id) | ||
.select_append{ max(:created_at).as(:created_at) } | ||
end | ||
|
||
# @return [Sequel::Dataset] the max created_at from the verifications table | ||
# for each integration keyed by consumer_id/provider_id | ||
def self.verification_max_created_at(connection) | ||
connection[:verifications] | ||
.select_group(:consumer_id, :provider_id) | ||
.select_append{ max(:created_at).as(:created_at) } | ||
end | ||
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,4 @@ | ||
require "pact_broker/events/subscriber" | ||
require "pact_broker/integrations/event_listener" | ||
|
||
PactBroker::Events.subscribe(PactBroker::Integrations::EventListener.new) |
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,23 @@ | ||
require "pact_broker/services" | ||
|
||
# Listens for events that happen in the Pact Broker that are relevant to the Integrations objects. | ||
|
||
module PactBroker | ||
module Integrations | ||
class EventListener | ||
include PactBroker::Services | ||
|
||
# @param [Hash] params the params from the broadcast event | ||
# @option params [PactBroker::Domain::Pact] :pact the newly published pact | ||
def contract_published(params) | ||
integration_service.handle_contract_data_published(params.fetch(:pact).consumer, params.fetch(:pact).provider) | ||
end | ||
|
||
# @param [Hash] params the params from the broadcast event | ||
# @option params [PactBroker::Domain::Verification] :verification the newly published verification | ||
def provider_verification_published(params) | ||
integration_service.handle_contract_data_published(params.fetch(:verification).consumer, params.fetch(:verification).provider) | ||
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
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
52 changes: 52 additions & 0 deletions
52
spec/integration/endpoints/publish_verification_results_spec.rb
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,52 @@ | ||
require "pact_broker/domain/verification" | ||
require "timecop" | ||
|
||
describe "Publishing a pact verification" do | ||
let(:path) { "/pacts/provider/Provider/consumer/Consumer/pact-version/#{pact.pact_version_sha}/verification-results" } | ||
let(:verification_content) { load_fixture("verification.json") } | ||
let(:parsed_response_body) { JSON.parse(subject.body) } | ||
let(:pact) { td.pact } | ||
let(:rack_env) do | ||
{ | ||
"CONTENT_TYPE" => "application/json", | ||
"HTTP_ACCEPT" => "application/hal+json", | ||
"pactbroker.database_connector" => lambda { |&block| block.call } | ||
} | ||
end | ||
|
||
subject { post(path, verification_content, rack_env) } | ||
|
||
before do | ||
Timecop.freeze(Date.today - 2) do | ||
td.create_provider("Provider") | ||
.create_consumer("Consumer") | ||
.create_consumer_version("1.0.0") | ||
.create_pact | ||
.create_consumer_version("1.2.3") | ||
.create_pact | ||
.revise_pact | ||
end | ||
end | ||
|
||
it "updates the contract_data_updated_at on the integration" do | ||
expect { subject }.to change { PactBroker::Integrations::Integration.last.contract_data_updated_at } | ||
end | ||
|
||
context "with a webhook configured", job: true do | ||
before do | ||
td.create_webhook( | ||
method: "POST", | ||
url: "http://example.org", | ||
events: [{ name: PactBroker::Webhooks::WebhookEvent::VERIFICATION_PUBLISHED }] | ||
) | ||
end | ||
let!(:request) do | ||
stub_request(:post, "http://example.org").to_return(:status => 200) | ||
end | ||
|
||
it "executes the webhook" do | ||
subject | ||
expect(request).to have_been_made | ||
end | ||
end | ||
end |
49 changes: 49 additions & 0 deletions
49
.../lib/pact_broker/db/data_migrations/set_contract_data_updated_at_for_integrations_spec.rb
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,49 @@ | ||
require "pact_broker/db/data_migrations/set_contract_data_updated_at_for_integrations" | ||
require "timecop" | ||
require "tzinfo" | ||
|
||
module PactBroker | ||
module DB | ||
module DataMigrations | ||
describe SetContractDataUpdatedAtForIntegrations do | ||
before do | ||
td.clear_now # use timecop instead of the TestDataBuilder @now | ||
|
||
Timecop.freeze(day_1) do | ||
td.publish_pact(consumer_name: "Foo", provider_name: "Bar", consumer_version_number: "1") | ||
end | ||
|
||
Timecop.freeze(day_2) do | ||
td.publish_pact(consumer_name: "Foo", provider_name: "Bar", consumer_version_number: "2") | ||
end | ||
|
||
Timecop.freeze(day_3) do | ||
td.create_verification(provider_version: "2") | ||
end | ||
|
||
Timecop.freeze(day_4) do | ||
td.publish_pact(consumer_name: "Cat", provider_name: "Dog", consumer_version_number: "2") | ||
end | ||
|
||
db[:integrations].update(contract_data_updated_at: nil) | ||
end | ||
|
||
let(:day_1) { td.in_utc{ DateTime.new(2023, 6, 11) } } | ||
let(:day_2) { td.in_utc{ DateTime.new(2023, 6, 12) } } | ||
let(:day_3) { td.in_utc{ DateTime.new(2023, 6, 13) } } | ||
let(:day_4) { td.in_utc{ DateTime.new(2023, 6, 14) } } | ||
|
||
let(:db) { PactBroker::Domain::Version.db } | ||
|
||
subject { SetContractDataUpdatedAtForIntegrations.call(db) } | ||
|
||
it "sets the contract_data_updated_at to the latest of the pact publication and verification publication dates for that integration" do | ||
subject | ||
integrations = db[:integrations].order(:id) | ||
expect(integrations.first[:contract_data_updated_at].to_s).to eq day_3.to_s # using to_s because dates are loaded as strings when running with MySQL | ||
expect(integrations.last[:contract_data_updated_at].to_s).to eq day_4.to_s | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Have moved this spec out of the feature directory, into the integration test directory.