From c765afe1390aab80c05d40737fc8628919f7ebb7 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 15 Jul 2021 13:10:57 +1000 Subject: [PATCH] feat: add interactions counts to metrics endpoint --- lib/pact_broker/metrics/service.rb | 17 +++++++++++++ spec/lib/pact_broker/metrics/service_spec.rb | 25 ++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/pact_broker/metrics/service.rb b/lib/pact_broker/metrics/service.rb index 6af0f883c..428095c68 100644 --- a/lib/pact_broker/metrics/service.rb +++ b/lib/pact_broker/metrics/service.rb @@ -22,6 +22,7 @@ module Service # rubocop: disable Metrics/MethodLength def metrics { + interactions: interactions_counts, pacticipants: { count: PactBroker::Domain::Pacticipant.count }, @@ -74,6 +75,22 @@ def metrics end # rubocop: enable Metrics/MethodLength + def interactions_counts + latest_pact_versions = PactBroker::Pacts::PactVersion.where( + id: PactBroker::Pacts::PactPublication.overall_latest.from_self.select(:pact_version_id) + ) + counts = latest_pact_versions + .select( + Sequel.function(:sum, :interactions_count).as(:interactions_count), + Sequel.function(:sum, :messages_count).as(:messages_count) + ).first + { + latestInteractionsCount: counts[:interactions_count] || 0, + latestMessagesCount: counts[:messages_count] || 0, + latestInteractionsAndMessagesCount: (counts[:interactions_count] || 0) + (counts[:messages_count] || 0) + } + end + 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 diff --git a/spec/lib/pact_broker/metrics/service_spec.rb b/spec/lib/pact_broker/metrics/service_spec.rb index bc759f932..3074f3618 100644 --- a/spec/lib/pact_broker/metrics/service_spec.rb +++ b/spec/lib/pact_broker/metrics/service_spec.rb @@ -6,6 +6,31 @@ module Service describe "#metrics" do subject { Service.metrics } + describe "interactions latestCount" do + before do + td.create_consumer + .create_provider + .create_consumer_version + .create_pact(json_content: { interactions: [1, 2], messages: [1] }.to_json) + .create_consumer + .create_provider + .create_consumer_version + .create_pact(json_content: { interactions: [1, 2], messages: [1] }.to_json) + .create_consumer + .create_provider + .create_consumer_version + .create_pact(json_content: { foo: "bar" }.to_json) + end + + it "includes a count of the number of interactions in the overall latest pacts" do + expect(subject[:interactions]).to eq({ + latestInteractionsCount: 4, + latestMessagesCount: 2, + latestInteractionsAndMessagesCount: 6 + }) + end + end + describe "verificationResultsPerPactVersion" do before do td.create_pact_with_hierarchy