Skip to content

Commit

Permalink
feat(metrics): add counts for environment, deployed version, released…
Browse files Browse the repository at this point in the history
… version, pacticipants and versions with branch set
  • Loading branch information
bethesque committed Jul 29, 2021
1 parent 8de7bf2 commit 8272b08
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
3 changes: 3 additions & 0 deletions lib/pact_broker/db/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "pact_broker/webhooks/webhook"
require "pact_broker/pacts/latest_pact_publication_id_for_consumer_version"
require "pact_broker/verifications/latest_verification_id_for_pact_version_and_provider_version"
require "pact_broker/integrations/integration"
require "pact_broker/pacts/pact_publication"
require "pact_broker/pacts/pact_version"
require "pact_broker/domain/verification"
Expand All @@ -13,6 +14,8 @@
require "pact_broker/deployments/environment"
require "pact_broker/deployments/deployed_version"
require "pact_broker/deployments/released_version"
require "pact_broker/matrix/row"
require "pact_broker/matrix/head_row"

module PactBroker
INTEGRATIONS_TABLES = [
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/domain/pacticipant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class Pacticipant < Sequel::Model
dataset_module do
include PactBroker::Repositories::Helpers

def with_main_branch_set
exclude(main_branch: nil)
end

def label label_name
filter = name_like(Sequel[:labels][:name], label_name)
join(:labels, {pacticipant_id: :id}).where(filter)
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/domain/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class Version < Sequel::Model
dataset_module do
include PactBroker::Repositories::Helpers

def with_branch_set
exclude(branch: nil)
end

def latest_version_for_pacticipant(pacticipant)
where(pacticipant: pacticipant)
.order(Sequel.desc(:order))
Expand Down
29 changes: 16 additions & 13 deletions lib/pact_broker/metrics/service.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
require "pact_broker/configuration"
require "pact_broker/pacts/pact_publication"
require "pact_broker/pacts/pact_version"
require "pact_broker/domain/pacticipant"
require "pact_broker/integrations/integration"
require "pact_broker/domain/verification"
require "pact_broker/domain/version"
require "pact_broker/db/models"
require "pact_broker/api/decorators/format_date_time"
require "pact_broker/webhooks/webhook"
require "pact_broker/webhooks/triggered_webhook"
require "pact_broker/webhooks/execution"
require "pact_broker/matrix/row"
require "pact_broker/matrix/head_row"

module PactBroker
module Metrics
Expand All @@ -24,7 +14,8 @@ def metrics
{
interactions: interactions_counts,
pacticipants: {
count: PactBroker::Domain::Pacticipant.count
count: PactBroker::Domain::Pacticipant.count,
withMainBranchSetCount: PactBroker::Domain::Pacticipant.with_main_branch_set.count
},
integrations: {
count: PactBroker::Integrations::Integration.count
Expand Down Expand Up @@ -52,7 +43,8 @@ def metrics
distribution: verification_distribution
},
pacticipantVersions: {
count: PactBroker::Domain::Version.count
count: PactBroker::Domain::Version.count,
withBranchSetCount: PactBroker::Domain::Version.with_branch_set.count
},
webhooks: {
count: PactBroker::Webhooks::Webhook.count
Expand All @@ -70,6 +62,17 @@ def metrics
},
matrix: {
count: matrix_count
},
environments: {
count: PactBroker::Deployments::Environment.count
},
deployedVersions: {
count: PactBroker::Deployments::DeployedVersion.count,
currentlyDeployedCount: PactBroker::Deployments::DeployedVersion.currently_deployed.count
},
releasedVersions: {
count: PactBroker::Deployments::ReleasedVersion.count,
currentlySupportedCount: PactBroker::Deployments::ReleasedVersion.currently_supported.count
}
}
end
Expand Down
44 changes: 44 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,50 @@ module Service
describe "#metrics" do
subject { Service.metrics }

describe "pacticipants" do
before do
td.create_consumer("Foo")
.create_consumer("Bar", main_branch: "foo")
end

its([:pacticipants, :count]) { is_expected.to eq 2 }
its([:pacticipants, :withMainBranchSetCount]) { is_expected.to eq 1 }
end

describe "pacticipant versions" do
before do
td.create_consumer("Foo")
.create_consumer_version("2")
.create_consumer_version("3", branch: "main")
end

its([:pacticipantVersions, :count]) { is_expected.to eq 2 }
its([:pacticipantVersions, :withBranchSetCount]) { is_expected.to eq 1 }
end

describe "environments, deployed versions, released versions" do
before do
td.create_environment("test")
.create_consumer("Foo")
.create_consumer_version("2")
.create_deployed_version_for_consumer_version(currently_deployed: false)
.create_consumer_version("3")
.create_deployed_version_for_consumer_version
.create_consumer_version("4")
.create_released_version_for_consumer_version(currently_supported: false)
.create_consumer_version("5")
.create_released_version_for_consumer_version
.create_consumer_version("6")
.create_released_version_for_consumer_version
end

its([:environments, :count]) { is_expected.to eq 1 }
its([:deployedVersions, :count]) { is_expected.to eq 2 }
its([:deployedVersions, :currentlyDeployedCount]) { is_expected.to eq 1 }
its([:releasedVersions, :count]) { is_expected.to eq 3 }
its([:releasedVersions, :currentlySupportedCount]) { is_expected.to eq 2 }
end

describe "interactions latestCount" do
before do
td.create_consumer
Expand Down

0 comments on commit 8272b08

Please sign in to comment.