Skip to content

Commit

Permalink
feat(dashboard api): speed up loading of verification tags
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jan 25, 2018
1 parent a0d0e94 commit a3aea48
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 143 deletions.
2 changes: 1 addition & 1 deletion db/migrations/20180119_update_latest_triggered_webhooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)

create_or_replace_view(:latest_triggered_webhook_ids,
"select tw.webhook_uuid, tw.consumer_id, tw.provider_id, max(tw.id) as latest_triggered_webhook_id
"select tw.webhook_uuid, tw.consumer_id, tw.provider_id, ltwcd.latest_triggered_webhook_created_at, max(tw.id) as latest_triggered_webhook_id
from latest_triggered_webhook_creation_dates ltwcd
inner join triggered_webhooks tw
on tw.consumer_id = ltwcd.consumer_id
Expand Down
43 changes: 0 additions & 43 deletions db/migrations/20180123_create_latest_verification_tags.rb

This file was deleted.

28 changes: 28 additions & 0 deletions db/migrations/20180123_create_tags_with_latest_flag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require_relative 'migration_helper'

Sequel.migration do
change do
create_view(:latest_tagged_version_orders,
PactBroker::MigrationHelper.sqlite_safe("
select v.pacticipant_id, t.name as tag_name, max(v.order) as latest_version_order, 1 as latest
from tags t
inner join versions v
on v.id = t.version_id
group by v.pacticipant_id, t.name
")
)

create_view(:tags_with_latest_flag,
PactBroker::MigrationHelper.sqlite_safe("
select t.*, ltvo.latest
from tags t
inner join versions v
on v.id = t.version_id
left outer join latest_tagged_version_orders ltvo
on t.name = ltvo.tag_name
and v.pacticipant_id = ltvo.pacticipant_id
and v.order = ltvo.latest_version_order
")
)
end
end
8 changes: 8 additions & 0 deletions db/migrations/migration_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@ def with_mysql
def adapter
Sequel::Model.db.adapter_scheme.to_s
end

def sqlite_safe string
if adapter == 'sqlite'
string.gsub(/\border\b/, '`order`')
else
string
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pact_broker/api/decorators/dashboard_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def verification_tags(index_item, base_url)
fake_tag = OpenStruct.new(name: tag.name, version: index_item.provider_version)
{
name: tag.name,
latest: true,
latest: tag.latest?,
_links: {
self: {
href: tag_url(base_url, fake_tag)
Expand Down
5 changes: 3 additions & 2 deletions lib/pact_broker/index/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def self.find_index_items options = {}
.eager(:webhooks)
.order(:consumer_name, :provider_name)
.eager(:consumer_version_tags)
.eager(:provider_version_tags)
.all
end

Expand All @@ -35,7 +36,7 @@ def self.find_index_items options = {}
.eager(:webhooks)
.order(:consumer_name, :provider_name)
.eager(:consumer_version_tags)
.eager(:latest_verification_tags)
.eager(:provider_version_tags)

if options[:tags].is_a?(Array)
tagged_rows = tagged_rows.where(Sequel[:head_pact_publications][:tag_name] => options[:tags]).or(Sequel[:head_pact_publications][:tag_name] => nil)
Expand Down Expand Up @@ -67,7 +68,7 @@ def self.find_index_items options = {}
row.webhooks,
row.latest_triggered_webhooks,
tag_names,
row.latest_verification_tags
row.provider_version_tags.select(&:latest?)
)
end

Expand Down
16 changes: 6 additions & 10 deletions lib/pact_broker/matrix/row.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
require 'pact_broker/repositories/helpers'
require 'pact_broker/webhooks/latest_triggered_webhook'
require 'pact_broker/tags/latest_verification_tag'
require 'pact_broker/tags/tag_with_latest_flag'

module PactBroker
module Matrix
class Row < Sequel::Model(:matrix)


associate(:one_to_many, :latest_triggered_webhooks, :class => "PactBroker::Webhooks::LatestTriggeredWebhook", primary_key: :pact_publication_id, key: :pact_publication_id)

# already have this
# associate(:many_to_one, :latest_verification, :class => "PactBroker::Verifications::Repository::LatestVerificationsByConsumerVersion", primary_key: :pact_version_id, key: :pact_version_id)
# TODO modify this to work with single pacticipant webhooks
associate(:one_to_many, :webhooks, :class => "PactBroker::Webhooks::Webhook", primary_key: [:consumer_id, :provider_id], key: [:consumer_id, :provider_id])
associate(:one_to_many, :consumer_version_tags, :class => "PactBroker::Domain::Tag", primary_key: :consumer_version_id, key: :version_id)
associate(:one_to_many, :latest_verification_tags, :class => "PactBroker::Tags::LatestVerificationTag", primary_key: :verification_id, key: :verification_id)
associate(:one_to_many, :consumer_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :consumer_version_id, key: :version_id)
associate(:one_to_many, :provider_version_tags, :class => "PactBroker::Tags::TagWithLatestFlag", primary_key: :provider_version_id, key: :version_id)

dataset_module do
include PactBroker::Repositories::Helpers
Expand Down Expand Up @@ -71,9 +67,9 @@ def consumer_head_tag_names= consumer_head_tag_names
@consumer_head_tag_names = consumer_head_tag_names
end

def latest_triggered_webhooks
@latest_triggered_webhooks ||= []
end
# def latest_triggered_webhooks
# @latest_triggered_webhooks ||= []
# end

def summary
"#{consumer_name}#{consumer_version_number} #{provider_name}#{provider_version_number || '?'} (r#{pact_revision_number}n#{verification_number || '?'})"
Expand Down
18 changes: 18 additions & 0 deletions lib/pact_broker/tags/tag_with_latest_flag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'pact_broker/db'
require 'pact_broker/repositories/helpers'

module PactBroker
module Tags
# The tag associated with the latest verification for a given tag
class TagWithLatestFlag < Sequel::Model(:tags_with_latest_flag)

dataset_module do
include PactBroker::Repositories::Helpers
end

def latest?
!values[:latest].nil?
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Decorators
provider_version_number: provider_version.number,
consumer_version_number: consumer_version.number,
tag_names: ['prod'],
latest_verification_latest_tags: [double('tag', name: 'dev')]
latest_verification_latest_tags: [double('tag', name: 'dev', latest?: true)]
)
end
let(:consumer) { instance_double('PactBroker::Domain::Pacticipant', name: 'Foo') }
Expand Down
85 changes: 0 additions & 85 deletions spec/migrations/20180123_create_latest_verification_tags_spec.rb

This file was deleted.

0 comments on commit a3aea48

Please sign in to comment.