Skip to content

Commit

Permalink
Improve submitted analytics data
Browse files Browse the repository at this point in the history
- Use default `custom-prefix` label on macOS ARM (as `/usr/local` is
  not the default).
- Add architecture (or Rosetta) to analytics event label.
- Don't send minor versions on Big Sur.
- Remove defunct `HOMEBREW_OSX_VERSION` reference.
  • Loading branch information
MikeMcQuaid committed Jan 7, 2021
1 parent dad7dc6 commit 8af4895
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
9 changes: 8 additions & 1 deletion Library/Homebrew/brew.sh
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,21 @@ then
HOMEBREW_SYSTEM="Macintosh"
[[ "$HOMEBREW_PROCESSOR" = "x86_64" ]] && HOMEBREW_PROCESSOR="Intel"
HOMEBREW_MACOS_VERSION="$(/usr/bin/sw_vers -productVersion)"
HOMEBREW_OS_VERSION="macOS $HOMEBREW_MACOS_VERSION"
# Don't change this from Mac OS X to match what macOS itself does in Safari on 10.12
HOMEBREW_OS_USER_AGENT_VERSION="Mac OS X $HOMEBREW_MACOS_VERSION"

# Intentionally set this variable by exploding another.
# shellcheck disable=SC2086,SC2183
printf -v HOMEBREW_MACOS_VERSION_NUMERIC "%02d%02d%02d" ${HOMEBREW_MACOS_VERSION//./ }

# Don't include minor versions for Big Sur and later.
if [[ "$HOMEBREW_MACOS_VERSION_NUMERIC" -gt "110000" ]]
then
HOMEBREW_OS_VERSION="macOS ${HOMEBREW_MACOS_VERSION%.*}"
else
HOMEBREW_OS_VERSION="macOS $HOMEBREW_MACOS_VERSION"
fi

# Refuse to run on pre-Yosemite
if [[ "$HOMEBREW_MACOS_VERSION_NUMERIC" -lt "101000" ]]
then
Expand Down
14 changes: 14 additions & 0 deletions Library/Homebrew/extend/os/mac/utils/analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@ module Utils
module Analytics
class << self
extend T::Sig

sig { returns(String) }
def custom_prefix_label
return generic_custom_prefix_label if Hardware::CPU.arm?

"non-/usr/local"
end

sig { returns(String) }
def arch_label
if Hardware::CPU.arm?
"ARM"
elsif Hardware::CPU.in_rosetta2?
"Rosetta"
else
""
end
end
end
end
end
2 changes: 1 addition & 1 deletion Library/Homebrew/os/mac.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def version
# This can be compared to numerics, strings, or symbols
# using the standard Ruby Comparable methods.
def full_version
@full_version ||= Version.new((ENV["HOMEBREW_MACOS_VERSION"] || ENV["HOMEBREW_OSX_VERSION"]).chomp)
@full_version ||= Version.new((ENV["HOMEBREW_MACOS_VERSION"]).chomp)
end

def full_version=(version)
Expand Down
12 changes: 6 additions & 6 deletions Library/Homebrew/test/utils/analytics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
require "formula_installer"

describe Utils::Analytics do
describe "::os_prefix_ci" do
context "when os_prefix_ci is not set" do
describe "::os_arch_prefix_ci" do
context "when os_arch_prefix_ci is not set" do
before do
described_class.clear_os_prefix_ci
described_class.clear_os_arch_prefix_ci
end

it "returns OS_VERSION and prefix when HOMEBREW_PREFIX is a custom prefix" do
allow(Homebrew).to receive(:default_prefix?).and_return(false)
expect(described_class.os_prefix_ci).to include("#{OS_VERSION}, #{described_class.custom_prefix_label}")
expect(described_class.os_arch_prefix_ci).to include("#{OS_VERSION}, #{described_class.custom_prefix_label}")
end

it "does not include prefix when HOMEBREW_PREFIX is the default prefix" do
expect(described_class.os_prefix_ci).not_to include(described_class.custom_prefix_label)
expect(described_class.os_arch_prefix_ci).not_to include(described_class.custom_prefix_label)
end

it "includes CI when ENV['CI'] is set" do
ENV["CI"] = "true"
expect(described_class.os_prefix_ci).to include("CI")
expect(described_class.os_arch_prefix_ci).to include("CI")
end
end
end
Expand Down
25 changes: 18 additions & 7 deletions Library/Homebrew/utils/analytics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def report(type, metadata = {})
end
end

def report_event(category, action, label = os_prefix_ci, value = nil)
def report_event(category, action, label = os_arch_prefix_ci, value = nil)
report(:event,
ec: category,
ea: action,
Expand Down Expand Up @@ -198,19 +198,30 @@ def cask_output(cask, args:)
def custom_prefix_label
"custom-prefix"
end
alias generic_custom_prefix_label custom_prefix_label

def clear_os_prefix_ci
return unless instance_variable_defined?(:@os_prefix_ci)
sig { returns(String) }
def arch_label
if Hardware::CPU.arm?
"ARM"
else
""
end
end

def clear_os_arch_prefix_ci
return unless instance_variable_defined?(:@os_arch_prefix_ci)

remove_instance_variable(:@os_prefix_ci)
remove_instance_variable(:@os_arch_prefix_ci)
end

def os_prefix_ci
@os_prefix_ci ||= begin
def os_arch_prefix_ci
@os_arch_prefix_ci ||= begin
os = OS_VERSION
arch = ", #{arch_label}" if arch_label.present?
prefix = ", #{custom_prefix_label}" unless Homebrew.default_prefix?
ci = ", CI" if ENV["CI"]
"#{os}#{prefix}#{ci}"
"#{os}#{arch}#{prefix}#{ci}"
end
end

Expand Down

0 comments on commit 8af4895

Please sign in to comment.