Skip to content

Commit

Permalink
feat: add revisions per consumer version and verifications per pact v…
Browse files Browse the repository at this point in the history
…ersion to metrics
  • Loading branch information
bethesque committed Oct 9, 2019
1 parent bea5951 commit f764fbc
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
26 changes: 21 additions & 5 deletions lib/pact_broker/metrics/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ def metrics
pactVersions: {
count: PactBroker::Pacts::PactVersion.count
},
pactRevisions: {
counts: pact_revision_counts
pactRevisionsPerConsumerVersion: {
distribution: pact_revision_counts
},
verificationResults: {
count: PactBroker::Domain::Verification.count,
first: format_date_time(PactBroker::Domain::Verification.order(:id).first.created_at),
last: format_date_time(PactBroker::Domain::Verification.order(:id).last.created_at)
successCount: PactBroker::Domain::Verification.where(success: true).count,
failureCount: PactBroker::Domain::Verification.where(success: false).count,
distinctCount: PactBroker::Domain::Verification.distinct.select(:provider_version_id, :pact_version_id, :success).count,
first: format_date_time(PactBroker::Domain::Verification.order(:id).first&.created_at),
last: format_date_time(PactBroker::Domain::Verification.order(:id).last&.created_at),
},
verificationResultsPerPactVersion: {
distribution: verification_distribution
},
pacticipantVersions: {
count: PactBroker::Domain::Version.count
Expand All @@ -66,9 +72,19 @@ def metrics
def pact_revision_counts
query = "select revision_count as number_of_revisions, count(consumer_version_id) as consumer_version_count
from (select consumer_version_id, count(*) as revision_count from pact_publications group by consumer_version_id) foo
group by revision_count"
group by revision_count
order by 1"
PactBroker::Pacts::PactPublication.db[query].all.each_with_object({}) { |row, hash| hash[row[:number_of_revisions]] = row[:consumer_version_count] }
end

#
def verification_distribution
query = "select verification_count as number_of_verifications, count(*) as pact_version_count
from (select pact_version_id, count(*) as verification_count from verifications group by pact_version_id) foo
group by verification_count
order by 1"
PactBroker::Pacts::PactPublication.db[query].all.each_with_object({}) { |row, hash| hash[row[:number_of_verifications]] = row[:pact_version_count] }
end
end
end
end
56 changes: 56 additions & 0 deletions spec/lib/pact_broker/metrics/service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'pact_broker/metrics/service'

module PactBroker
module Metrics
module Service
describe "#metrics" do
subject { Service.metrics }

describe "verificationResultsPerPactVersion" do
before do
td.create_pact_with_hierarchy
.comment("this pact version will have 2 verifications")
.create_verification
.create_verification(number: 2)
.revise_pact
.comment("this pact version will have 1 verification")
.create_verification
.create_consumer_version
.create_pact
.comment("this pact will have 1 verification")
.create_verification
.create_consumer_version
.create_pact
.comment("this pact will have 1 verification")
.create_verification
end

let(:distribution) { subject[:verificationResultsPerPactVersion][:distribution] }

it "returns a distribution of verifications per pact version" do
expect(distribution).to eq(1 => 3, 2 => 1)
end
end

describe "pactRevisionsPerPactPublication" do
before do
td.create_pact_with_hierarchy
.comment("this consumer version will have 3 revisions")
.revise_pact
.revise_pact
.create_consumer_version
.create_pact
.comment("this consumer version will have 1 revision")
.revise_pact
end

let(:distribution) { subject[:pactRevisionsPerConsumerVersion][:distribution] }

it "returns a distribution of pact revisions per consumer version" do
expect(distribution).to eq(2 => 1, 3 => 1)
end
end
end
end
end
end

0 comments on commit f764fbc

Please sign in to comment.