Module: PactBroker::Metrics::Service

Extended by:
Service
Includes:
Api::Decorators::FormatDateTime
Included in:
Service
Defined in:
lib/pact_broker/metrics/service.rb

Constant Summary

Constants included from Api::Decorators::FormatDateTime

Api::Decorators::FormatDateTime::DATE_TIME_CLASS

Instance Method Summary collapse

Methods included from Api::Decorators::FormatDateTime

call, #format_date_time

Instance Method Details

#interactions_countsObject

rubocop: enable Metrics/MethodLength



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pact_broker/metrics/service.rb', line 84

def interactions_counts
  latest_pact_versions = PactBroker::Pacts::PactVersion.where(
    id: PactBroker::Pacts::PactPublication.overall_latest.from_self.select(:pact_version_id)
  )

  latest_pact_versions.all.each(&:set_interactions_and_messages_counts!)

  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

#matrix_countObject



119
120
121
122
123
124
125
126
127
# File 'lib/pact_broker/metrics/service.rb', line 119

def matrix_count
  begin
    PactBroker::Matrix::EveryRow.db.with_statement_timeout(PactBroker.configuration.metrics_sql_statement_timeout) do
      PactBroker::Matrix::EveryRow.default_scope.count
    end
  rescue Sequel::DatabaseError => _ex
    -1
  end
end

#metricsObject

rubocop: disable Metrics/MethodLength



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pact_broker/metrics/service.rb', line 13

def metrics
  {
    interactions: interactions_counts,
    pacticipants: {
      count: PactBroker::Domain::Pacticipant.count,
      withMainBranchSetCount: PactBroker::Domain::Pacticipant.with_main_branch_set.count
    },
    integrations: {
      count: PactBroker::Pacts::PactPublication.select(:consumer_id, :provider_id).distinct.count
    },
    pactPublications: {
      count: PactBroker::Pacts::PactPublication.count,
      first: format_date_time(PactBroker::Pacts::PactPublication.order(:id).first&.created_at),
      last: format_date_time(PactBroker::Pacts::PactPublication.order(:id).last&.created_at)
    },
    pactVersions: {
      count: PactBroker::Pacts::PactVersion.count
    },
    pactRevisionsPerConsumerVersion: {
      distribution: pact_revision_counts
    },
    verificationResults: {
      count: PactBroker::Domain::Verification.count,
      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,
      withUserCreatedBranchCount: PactBroker::Domain::Version.with_user_created_branch.count,
      withBranchCount: PactBroker::Domain::Version.with_branch.count,
      withBranchSetCount: PactBroker::Domain::Version.with_branch.count # todo remove when checked it's not used
    },
    webhooks: {
      count: PactBroker::Webhooks::Webhook.count
    },
    tags: {
      count: PactBroker::Domain::Tag.count,
      distinctCount: PactBroker::Domain::Tag.select(:name).distinct.count,
      distinctWithPacticipantCount: PactBroker::Domain::Tag.join(:versions, { id: :version_id }).select_group(:name, Sequel[:versions][:pacticipant_id]).count
    },
    triggeredWebhooks: {
      count: PactBroker::Webhooks::TriggeredWebhook.count
    },
    webhookExecutions: {
      count: PactBroker::Webhooks::Execution.count
    },
    matrix: {
      count: matrix_count
    },
    environments: {
      count: PactBroker::Deployments::Environment.count
    },
    deployedVersions: {
      count: PactBroker::Deployments::DeployedVersion.count,
      userCreatedCount: PactBroker::Deployments::DeployedVersion.user_created.count,
      currentlyDeployedCount: PactBroker::Deployments::DeployedVersion.currently_deployed.count
    },
    releasedVersions: {
      count: PactBroker::Deployments::ReleasedVersion.count,
      currentlySupportedCount: PactBroker::Deployments::ReleasedVersion.currently_supported.count
    }
  }
end

#pact_revision_countsObject



103
104
105
106
107
108
109
# File 'lib/pact_broker/metrics/service.rb', line 103

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
    order by 1"
  PactBroker::Pacts::PactPublication.db[query].all.each_with_object({}) { |row, hash| hash[row[:number_of_revisions]] = row[:consumer_version_count] }
end

#verification_distributionObject



111
112
113
114
115
116
117
# File 'lib/pact_broker/metrics/service.rb', line 111

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