Module: Spree::Diagnostics::Stack

Defined in:
app/services/spree/diagnostics/stack.rb

Overview

The installation's stack and rough size, shared by the update check's heartbeat and the diagnostics report so the two never describe the same installation differently.

Constant Summary collapse

BUCKETS =

Order-of-magnitude buckets: enough to tell a hobby install from a large merchant, never an exact figure.

{
  0..0 => '0',
  1..9 => '1-9',
  10..99 => '10-99',
  100..999 => '100-999',
  1_000..9_999 => '1k-9k',
  10_000..99_999 => '10k-99k'
}.freeze
LARGEST_BUCKET =
'100k+'.freeze
SPREE_URL =
%r{\Ahttps?://(www\.)?(spreecommerce\.org|github\.com/spree/)}

Class Method Summary collapse

Class Method Details

.bucket(count) ⇒ String

Parameters:

  • count (Integer)

Returns:

  • (String)


62
63
64
# File 'app/services/spree/diagnostics/stack.rb', line 62

def bucket(count)
  BUCKETS.find { |range, _| range.cover?(count) }&.last || LARGEST_BUCKET
end

.dashboard_served? ⇒ Boolean

Returns whether this installation serves an admin dashboard.

Returns:

  • (Boolean) —

    whether this installation serves an admin dashboard



56
57
58
# File 'app/services/spree/diagnostics/stack.rb', line 56

def dashboard_served?
  (defined?(Spree::Dashboard) && Spree::Dashboard.dist_path.present?) || Spree::Config.dashboard_url.present?
end

.database ⇒ String

Returns the database adapter, e.g. postgresql.

Returns:

  • (String) —

    the database adapter, e.g. postgresql



23
24
25
# File 'app/services/spree/diagnostics/stack.rb', line 23

def database
  ActiveRecord::Base.connection_db_config.adapter
end

.public_gem?(spec) ⇒ Boolean

Whether a gem's name can appear in a public bug report: one of Spree's own, or one installed from rubygems.org. Gems from a path, git or a private gem server are often the merchant's own code.

Parameters:

  • spec (Gem::Specification)

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'app/services/spree/diagnostics/stack.rb', line 48

def public_gem?(spec)
  return true if [spec.homepage, *spec..values].any? { |url| url.to_s.match?(SPREE_URL) }

  source = spec.respond_to?(:source) ? spec.source : nil
  source.is_a?(Bundler::Source::Rubygems) && source.remotes.all? { |remote| remote.host == 'rubygems.org' }
end

.seller_bucket ⇒ String?

Returns nil when the installation has no sellers.

Returns:

  • (String, nil) —

    nil when the installation has no sellers



67
68
69
70
# File 'app/services/spree/diagnostics/stack.rb', line 67

def seller_bucket
  count = Spree::Seller.count
  bucket(count) if count.positive?
end

.spree_gem?(spec) ⇒ Boolean

Parameters:

  • spec (Gem::Specification)

Returns:

  • (Boolean)


38
39
40
# File 'app/services/spree/diagnostics/stack.rb', line 38

def spree_gem?(spec)
  spec.name == 'spree' || spec.name.start_with?('spree_')
end

.spree_gems ⇒ Hash{String => String}

Spree's own gems and published Spree extensions, by name. A private gem named spree_* is left out: its name is the merchant's business.

Returns:

  • (Hash{String => String}) —

    gem name => version



31
32
33
34
# File 'app/services/spree/diagnostics/stack.rb', line 31

def spree_gems
  Gem.loaded_specs.values.select { |spec| spree_gem?(spec) && public_gem?(spec) }.
    sort_by(&:name).to_h { |spec| [spec.name, spec.version.to_s] }
end