Class: PackStats::Private::Metrics::Packages

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/pack_stats/private/metrics/packages.rb

Class Method Summary collapse

Class Method Details

.get_package_metrics(packages, app_name) ⇒ Object



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
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pack_stats/private/metrics/packages.rb', line 16

def self.get_package_metrics(packages, app_name)
  all_metrics = []
  app_level_tag = Tag.for('app', app_name)
  package_tags = T.let([app_level_tag], T::Array[Tag])

  all_metrics << GaugeMetric.for('all_packages.count', packages.count, package_tags)
  all_metrics << GaugeMetric.for('all_packages.dependencies.count', packages.sum { |package| package.dependencies.count }, package_tags)

  PackwerkCheckerUsage::CHECKERS.each do |checker|
    violation_count = packages.sum { |package| Metrics.file_count(package.violations.select{|v| v.type == checker.violation_type}) }
    tags = package_tags + [checker.violation_type_tag]
    all_metrics << GaugeMetric.for("all_packages.violations.count", violation_count, tags)
  end

  all_metrics += Metrics::PublicUsage.get_public_usage_metrics('all_packages', packages, package_tags)
  all_metrics << GaugeMetric.for('all_packages.has_readme.count', packages.count { |package| Metrics.has_readme?(package) }, package_tags)

  all_metrics += Metrics::PackwerkCheckerUsage.get_checker_metrics('all_packages', packages, package_tags)
  all_metrics << GaugeMetric.for('all_packages.package_based_file_ownership.count', packages.count { |package| !package.['owner'].nil? }, package_tags)

  inbound_violations_by_package = packages.flat_map(&:violations).group_by(&:to_package_name)

  packages.each do |package|
    package_tags = Metrics.tags_for_package(package, app_name)
    all_metrics += Metrics::PublicUsage.get_public_usage_metrics('by_package', [package], package_tags)

    outbound_violations = package.violations
    inbound_violations = inbound_violations_by_package[package.name] || []

    PackwerkCheckerUsage::CHECKERS.each do |checker|
      direction = checker.direction

      case direction
      when PackwerkCheckerUsage::Direction::Outbound
        all_violations_of_type = outbound_violations.select { |v| v.type == checker.violation_type } 

        packages.each do |other_package|
          violations = package.violations.select{|v| v.to_package_name == other_package.name && v.type == checker.violation_type }

          tags = package_tags + [
            Tag.for('other_package', Metrics.humanized_package_name(other_package.name)),
            *Metrics.tags_for_other_team(Private.package_owner(other_package)),
            checker.violation_type_tag
          ]

          count = Metrics.file_count(violations)
          if count > 0
            all_metrics << GaugeMetric.for("by_package.violations.by_other_package.count", Metrics.file_count(violations), tags)
          end
        end
      when PackwerkCheckerUsage::Direction::Inbound
        all_violations_of_type = inbound_violations.select { |v| v.type == checker.violation_type } 

        packages.each do |other_package|
          violations = other_package.violations.select{|v| v.to_package_name == package.name && v.type == checker.violation_type }
          tags = package_tags + [
            Tag.for('other_package', Metrics.humanized_package_name(other_package.name)),
            *Metrics.tags_for_other_team(Private.package_owner(other_package)),
            checker.violation_type_tag
          ]

          count = Metrics.file_count(violations)
          if count > 0
            all_metrics << GaugeMetric.for("by_package.violations.by_other_package.count", count, tags)
          end
        end
      else
        T.absurd(direction)
      end

      tags = package_tags + [checker.violation_type_tag]
      all_metrics << GaugeMetric.for("by_package.violations.count", Metrics.file_count(all_violations_of_type), tags)
    end
  end

  all_metrics += Metrics::Dependencies.get_metrics('by_package', packages, app_name)

  all_metrics
end