analytics: move to a class.

Global namespaces are good to avoid when possible.
This commit is contained in:
Mike McQuaid 2016-05-03 14:21:08 +01:00
parent 798c342f4e
commit 0ef21ddf87
3 changed files with 71 additions and 65 deletions

View File

@ -204,7 +204,7 @@ class FormulaInstaller
if formula.tap && !formula.tap.private?
options = effective_build_options_for(formula).used_options.to_a.join(" ")
report_analytics_event("install", "#{formula.full_name} #{options}".strip)
Utils::Analytics.report_event("install", "#{formula.full_name} #{options}".strip)
end
@@attempted << formula

View File

@ -1,5 +1,8 @@
def analytics_label
@analytics_anonymous_prefix_and_os ||= begin
module Utils
module Analytics
class << self
def os_prefix_ci
@anonymous_os_prefix_ci ||= begin
os = OS_VERSION
prefix = ", non-/usr/local" if HOMEBREW_PREFIX.to_s != "/usr/local"
ci = ", CI" if ENV["CI"]
@ -7,7 +10,7 @@ def analytics_label
end
end
def report_analytics(type, metadata = {})
def report(type, metadata = {})
return if ENV["HOMEBREW_NO_ANALYTICS"] || ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"]
args = %W[
@ -42,26 +45,29 @@ def report_analytics(type, metadata = {})
end
end
def report_analytics_event(category, action, label = analytics_label, value = nil)
report_analytics(:event,
def report_event(category, action, label = os_prefix_ci, value = nil)
report(:event,
:ec => category,
:ea => action,
:el => label,
:ev => value)
end
def report_analytics_exception(exception, options = {})
def report_exception(exception, options = {})
if exception.is_a?(BuildError) &&
exception.formula.tap && !exception.formula.tap.private?
report_analytics_event("BuildError", exception.formula.full_name)
report_event("BuildError", exception.formula.full_name)
end
fatal = options.fetch(:fatal, true) ? "1" : "0"
report_analytics(:exception,
report(:exception,
:exd => exception.class.name,
:exf => fatal)
end
def report_analytics_screenview(screen_name)
report_analytics(:screenview, :cd => screen_name)
def report_screenview(screen_name)
report(:screenview, :cd => screen_name)
end
end
end
end

View File

@ -129,17 +129,17 @@ rescue Interrupt => e
$stderr.puts # seemingly a newline is typical
exit 130
rescue BuildError => e
report_analytics_exception(e)
Utils::Analytics.report_exception(e)
e.dump
exit 1
rescue RuntimeError, SystemCallError => e
report_analytics_exception(e)
Utils::Analytics.report_exception(e)
raise if e.message.empty?
onoe e
$stderr.puts e.backtrace if ARGV.debug?
exit 1
rescue Exception => e
report_analytics_exception(e)
Utils::Analytics.report_exception(e)
onoe e
if internal_cmd
$stderr.puts "#{Tty.white}Please report this bug:"