From f764fbca26e91bd9e9c1c1d63f9a19e2b7744858 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 10 Oct 2019 09:22:16 +1100 Subject: [PATCH] feat: add revisions per consumer version and verifications per pact version to metrics --- lib/pact_broker/metrics/service.rb | 26 +++++++-- spec/lib/pact_broker/metrics/service_spec.rb | 56 ++++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 spec/lib/pact_broker/metrics/service_spec.rb diff --git a/lib/pact_broker/metrics/service.rb b/lib/pact_broker/metrics/service.rb index 3bb06b137..8296092ab 100644 --- a/lib/pact_broker/metrics/service.rb +++ b/lib/pact_broker/metrics/service.rb @@ -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 @@ -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 diff --git a/spec/lib/pact_broker/metrics/service_spec.rb b/spec/lib/pact_broker/metrics/service_spec.rb new file mode 100644 index 000000000..b8d13a223 --- /dev/null +++ b/spec/lib/pact_broker/metrics/service_spec.rb @@ -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