Skip to content

Commit

Permalink
feat: add interactions counts to metrics endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jul 15, 2021
1 parent 3369277 commit c765afe
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/pact_broker/metrics/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Service
# rubocop: disable Metrics/MethodLength
def metrics
{
interactions: interactions_counts,
pacticipants: {
count: PactBroker::Domain::Pacticipant.count
},
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions spec/lib/pact_broker/metrics/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c765afe

Please sign in to comment.