Sha256: 5a0e57e7fad0adfc3ee7d8afeec3e1c682a692596905073033a713e90a36da8d

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require_relative 'advisories_health_score'
require_relative 'audit_database'
require_relative 'calculation_context'
require_relative 'dependency_context'
require_relative 'gem_health_score'

module Polariscope
  module Scanner
    class GemfileHealthScore
      def initialize(**opts)
        @dependency_context = DependencyContext.new(**opts)
        @calculation_context = CalculationContext.new(**opts)

        AuditDatabase.update_if_necessary if opts.fetch(:update_audit_database, true)
      end

      def health_score
        return nil if dependency_context.no_dependencies?

        (100.0 * weighted_major_version_score * weighted_dependency_health_score * advisories_score).round(2)
      end

      private

      attr_reader :dependency_context
      attr_reader :calculation_context

      def weighted_major_version_score
        1.0 - weighted_major_version_penalty
      end

      def weighted_major_version_penalty
        dependency_priorities.zip(major_version_penalties).sum { |a, b| a * b } / dependency_priorities.sum
      end

      def weighted_dependency_health_score
        dependency_priorities.zip(dependency_health_scores).sum { |a, b| a * b } / dependency_priorities.sum
      end

      def major_version_penalties
        gem_health_scores.map(&:major_version_penalty)
      end

      def dependency_health_scores
        gem_health_scores.map(&:health_score)
      end

      def gem_health_scores
        @gem_health_scores ||= dependencies.map do |dependency|
          GemHealthScore.new(dependency_context, calculation_context, dependency)
        end
      end

      def dependency_priorities
        @dependency_priorities ||= dependencies.map { |dependency| calculation_context.priority_for(dependency) }
      end

      def advisories_score
        AdvisoriesHealthScore.new(dependency_context, calculation_context).health_score
      end

      def dependencies
        dependency_context.dependencies
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
polariscope-0.6.0 lib/polariscope/scanner/gemfile_health_score.rb