Module: Utils::Analytics Private
- Extended by:
- Cachable, Context, T::Generic, Output::Mixin
- Defined in:
- utils/analytics.rb
Overview
This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.
Helper module for fetching and reporting analytics data.
Constant Summary collapse
- INFLUX_BUCKET =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
"analytics"- INFLUX_TOKEN =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
"iVdsgJ_OjvTYGAA79gOfWlA_fX0QCuj4eYUNdb-qVUTrC3tp3JTWCADVNE9HxV0kp2ZjIK9tuthy_teX4szr9A=="- INFLUX_HOST =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
"https://eu-central-1-1.aws.cloud2.influxdata.com"- INFLUX_ORG =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
"d81a3e6d582d485f"- WSL_SUFFIX =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
" [WSL]"- Cache =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
type_template { { fixed: T::Hash[Symbol, T.untyped] } }
Class Method Summary collapse
- .cask_output(cask, args:) ⇒ void private
- .config_true?(key) ⇒ Boolean private
-
.default_package_fields ⇒ Hash{Symbol => String}
private
remove os_version starting with " or number remove macOS patch release.
- .default_package_tags ⇒ Hash{Symbol => Boolean, String} private
- .deferred_curl(url, args) ⇒ void private
- .delete_uuid! ⇒ void private
- .disable! ⇒ void private
- .disabled? ⇒ Boolean private
- .enable! ⇒ void private
- .format_count(count) ⇒ String private
- .format_percent(percent) ⇒ String private
- .formula_output(formula, args:) ⇒ void private
- .influx_message_displayed? ⇒ Boolean private
- .messages_displayed! ⇒ void private
- .messages_displayed? ⇒ Boolean private
- .no_message_output? ⇒ Boolean private
- .not_this_run? ⇒ Boolean private
- .output(args:, filter: nil) ⇒ void private
- .output_analytics(json, args:) ⇒ void private
-
.output_github_packages_downloads(formula, args:) ⇒ void
private
This method is undocumented because it is not intended for general use.
- .report_build_error(exception) ⇒ void private
- .report_command_run(command_instance) ⇒ void private
- .report_influx(measurement, tags, fields) ⇒ void private
- .report_package_event(measurement, package_name:, tap_name:, on_request: false, options: "") ⇒ void private
- .report_test_bot_test(step_command_short, passed) ⇒ void private
- .table_output(category, days, results, os_version: false, cask_install: false) ⇒ void private
- .with_wsl_suffix_if_needed(value, wsl: OS.wsl?) ⇒ String private
Methods included from Context
current, current=, debug?, quiet?, verbose?, with_context
Methods included from Output::Mixin
issue_reporting_message, odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_outside_github_actions, opoo_without_github_actions_annotation, pretty_deprecated, pretty_disabled, pretty_duration, pretty_install_status, pretty_installed, pretty_uninstalled, pretty_upgradable
Methods included from Cachable
Class Method Details
.cask_output(cask, args:) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'utils/analytics.rb', line 354 def cask_output(cask, args:) return if Homebrew::EnvConfig.no_analytics? || Homebrew::EnvConfig.no_github_api? require "api" return unless Homebrew::API.cask_tokens.include? cask.token json = Homebrew::API::Cask.cask_json cask.token return if json.blank? || json["analytics"].blank? output_analytics(json, args:) rescue ArgumentError # Ignore failed API requests nil end |
.config_true?(key) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
514 515 516 |
# File 'utils/analytics.rb', line 514 def config_true?(key) Homebrew::Settings.read(key) == "true" end |
.default_package_fields ⇒ Hash{Symbol => String}
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
remove os_version starting with " or number remove macOS patch release
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'utils/analytics.rb', line 399 def default_package_fields cache[:default_package_fields] ||= begin version = if (match_data = HOMEBREW_VERSION.match(/^[\d.]+/)) suffix = "-dev" if HOMEBREW_VERSION.include?("-") T.must(match_data[0]) + suffix.to_s else ">=4.1.22" end # Only include OS versions with an actual name. os_name_and_version = if (os_version = OS_VERSION.presence) && os_version.downcase.match?(/^[a-z]/) with_wsl_suffix_if_needed(os_version) end { version:, os_name_and_version:, } end end |
.default_package_tags ⇒ Hash{Symbol => Boolean, String}
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'utils/analytics.rb', line 378 def cache[:default_package_tags] ||= begin # Only display default prefixes to reduce cardinality and improve privacy prefix = Homebrew.default_prefix? ? HOMEBREW_PREFIX.to_s : "custom-prefix" # Tags are always strings and must have low cardinality. { ci: ENV["CI"].present?, prefix:, default_prefix: Homebrew.default_prefix?, developer: Homebrew::EnvConfig.developer?, devcmdrun: Homebrew::EnvConfig.devcmdrun?, arch: HOMEBREW_PHYSICAL_PROCESSOR, os: with_wsl_suffix_if_needed(HOMEBREW_SYSTEM), } end end |
.deferred_curl(url, args) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'utils/analytics.rb', line 62 def deferred_curl(url, args) require "utils/curl" curl = Utils::Curl.curl_executable args = Utils::Curl.curl_args(*args, "--silent", "--output", File::NULL, show_error: false) if ENV["HOMEBREW_ANALYTICS_DEBUG"] puts "#{curl} #{args.join(" ")} \"#{url}\"" puts Utils.popen_read(curl, *args, url) else pid = spawn curl, *args, url Process.detach(pid) end end |
.delete_uuid! ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
219 220 221 |
# File 'utils/analytics.rb', line 219 def delete_uuid! Homebrew::Settings.delete :analyticsuuid end |
.disable! ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
213 214 215 216 |
# File 'utils/analytics.rb', line 213 def disable! Homebrew::Settings.write :analyticsdisabled, true delete_uuid! end |
.disabled? ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
181 182 183 184 185 |
# File 'utils/analytics.rb', line 181 def disabled? return true if Homebrew::EnvConfig.no_analytics? config_true?(:analyticsdisabled) end |
.enable! ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
206 207 208 209 210 |
# File 'utils/analytics.rb', line 206 def enable! Homebrew::Settings.write :analyticsdisabled, false delete_uuid! end |
.format_count(count) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
519 520 521 |
# File 'utils/analytics.rb', line 519 def format_count(count) count.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse end |
.format_percent(percent) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
524 525 526 |
# File 'utils/analytics.rb', line 524 def format_percent(percent) format("%<percent>.2f", percent:) end |
.formula_output(formula, args:) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'utils/analytics.rb', line 336 def formula_output(formula, args:) return if Homebrew::EnvConfig.no_analytics? || Homebrew::EnvConfig.no_github_api? require "api" return unless Homebrew::API.formula_names.include? formula.name json = Homebrew::API::Formula.formula_json formula.name return if json.blank? || json["analytics"].blank? output_analytics(json, args:) output_github_packages_downloads(formula, args:) rescue ArgumentError # Ignore failed API requests nil end |
.influx_message_displayed? ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
168 169 170 |
# File 'utils/analytics.rb', line 168 def config_true?(:influxanalyticsmessage) end |
.messages_displayed! ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
199 200 201 202 203 |
# File 'utils/analytics.rb', line 199 def Homebrew::Settings.write :analyticsmessage, true Homebrew::Settings.write :caskanalyticsmessage, true Homebrew::Settings.write :influxanalyticsmessage, true end |
.messages_displayed? ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
173 174 175 176 177 178 |
# File 'utils/analytics.rb', line 173 def return false unless config_true?(:analyticsmessage) return false unless config_true?(:caskanalyticsmessage) end |
.no_message_output? ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
193 194 195 196 |
# File 'utils/analytics.rb', line 193 def # Used by Homebrew/install ENV["HOMEBREW_NO_ANALYTICS_MESSAGE_OUTPUT"].present? end |
.not_this_run? ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
188 189 190 |
# File 'utils/analytics.rb', line 188 def not_this_run? ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"].present? end |
.output(args:, filter: nil) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'utils/analytics.rb', line 224 def output(args:, filter: nil) require "api" days = args.days || "30" category = args.category || "install" begin json = Homebrew::API::Analytics.fetch category, days rescue ArgumentError # Ignore failed API requests return end return if json.blank? || json["items"].blank? os_version = category == "os-version" cask_install = category == "cask-install" results = {} json["items"].each do |item| key = if os_version item["os_version"] elsif cask_install item["cask"] else item["formula"] end next if filter.present? && key != filter && !key.start_with?("#{filter} ") results[key] = item["count"].tr(",", "").to_i end if filter.present? && results.blank? onoe "No results matching `#{filter}` found!" return end table_output(category, days, results, os_version:, cask_install:) end |
.output_analytics(json, args:) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'utils/analytics.rb', line 262 def output_analytics(json, args:) full_analytics = args.analytics? || verbose? ohai "Analytics" json["analytics"].each do |category, value| category = category.tr("_", "-") analytics = [] value.each do |days, results| days = days.to_i if full_analytics next if args.days.present? && args.days&.to_i != days next if args.category.present? && args.category != category table_output(category, days.to_s, results) else total_count = results.values.sum analytics << "#{Formatter.number_readable(total_count)} (#{days} days)" end end puts "#{category}: #{analytics.join(", ")}" unless full_analytics end end |
.output_github_packages_downloads(formula, args:) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
This method is undocumented because it is not intended for general use. It relies on screen scraping some GitHub HTML that's not available as an API. This seems very likely to break in the future. That said, it's the only way to get the data we want right now.
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'utils/analytics.rb', line 292 def output_github_packages_downloads(formula, args:) return unless args.github_packages_downloads? return unless formula.core_formula? require "utils/curl" escaped_formula_name = GitHubPackages.image_formula_name(formula.name) .gsub("/", "%2F") formula_url_suffix = "container/core%2F#{escaped_formula_name}/" formula_url = "https://github.com/Homebrew/homebrew-core/pkgs/#{formula_url_suffix}" output = Utils::Curl.curl_output("--fail", formula_url) return unless output.success? formula_version_urls = output.stdout .scan(%r{/orgs/Homebrew/packages/#{formula_url_suffix}\d+\?tag=[^"]+}) .map do |url| T.cast(url, String).sub("/orgs/Homebrew/packages/", "/Homebrew/homebrew-core/pkgs/") end return if formula_version_urls.empty? thirty_day_download_count = 0 formula_version_urls.each do |formula_version_url_suffix| formula_version_url = "https://github.com#{formula_version_url_suffix}" output = Utils::Curl.curl_output("--fail", formula_version_url) next unless output.success? last_thirty_days_match = output.stdout.match( %r{<span class="[\s\-a-z]*">Last 30 days</span>\s*<span class="[\s\-a-z]*">([\d.M,]+)</span>}m, ) next if last_thirty_days_match.blank? last_thirty_days_downloads = last_thirty_days_match.captures.fetch(0).tr(",", "") thirty_day_download_count += if (millions_match = last_thirty_days_downloads.match(/(\d+\.\d+)M/).presence) (millions_match.captures.first.to_f * 1_000_000).to_i else last_thirty_days_downloads.to_i end end ohai "GitHub Packages Downloads" puts "#{Formatter.number_readable(thirty_day_download_count)} (30 days)" end |
.report_build_error(exception) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'utils/analytics.rb', line 97 def report_build_error(exception) return if not_this_run? || disabled? formula = exception.formula return unless formula tap = formula.tap return unless tap return unless tap.should_report_analytics? = exception..to_a.compact.map(&:to_s).sort.uniq.join(" ") report_package_event(:build_error, package_name: formula.name, tap_name: tap.name, options:) end |
.report_command_run(command_instance) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'utils/analytics.rb', line 112 def report_command_run(command_instance) return if not_this_run? || disabled? command = command_instance.class.command_name = command_instance.args..to_a.compact # Strip out any flag values to reduce cardinality and preserve privacy. .map! { |option| option.sub(/=.*/m, "=") } # Strip out --with-* and --without-* options .reject! { |option| option.match(/^--with(out)?-/) } = .sort.uniq.join(" ") # Tags must have low cardinality. = { command:, ci: ENV["CI"].present?, devcmdrun: Homebrew::EnvConfig.devcmdrun?, developer: Homebrew::EnvConfig.developer?, } # Fields can have high cardinality. fields = { options: } report_influx(:command_run, , fields) end |
.report_influx(measurement, tags, fields) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'utils/analytics.rb', line 36 def report_influx(measurement, , fields) return if not_this_run? || disabled? # Tags are always implicitly strings and must have low cardinality. = .map { |k, v| "#{k}=#{v}" } .join(",") # Fields need explicitly wrapped with quotes and can have high cardinality. fields_string = fields.compact .map { |k, v| %Q(#{k}="#{v}") } .join(",") args = [ "--max-time", "3", "--header", "Authorization: Token #{INFLUX_TOKEN}", "--header", "Content-Type: text/plain; charset=utf-8", "--header", "Accept: application/json", "--data-binary", "#{measurement},#{} #{fields_string} #{Time.now.to_i}" ] # Second precision is highest we can do and has the lowest performance cost. url = "#{INFLUX_HOST}/api/v2/write?bucket=#{INFLUX_BUCKET}&precision=s" deferred_curl(url, args) end |
.report_package_event(measurement, package_name:, tap_name:, on_request: false, options: "") ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'utils/analytics.rb', line 80 def report_package_event(measurement, package_name:, tap_name:, on_request: false, options: "") return if not_this_run? || disabled? # ensure options are removed (by `.compact` below) if empty = nil if .blank? # Tags must have low cardinality. = .merge(on_request:) # Fields can have high cardinality. fields = default_package_fields.merge(package: package_name, tap_name:, options:) .compact report_influx(measurement, , fields) end |
.report_test_bot_test(step_command_short, passed) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'utils/analytics.rb', line 142 def report_test_bot_test(step_command_short, passed) return if not_this_run? || disabled? return if ENV["HOMEBREW_TEST_BOT_ANALYTICS"].blank? # Tags must have low cardinality. = { passed:, arch: HOMEBREW_PHYSICAL_PROCESSOR, os: HOMEBREW_SYSTEM, } # Strip out any flag values to reduce cardinality and preserve privacy. # Sort options to ensure consistent ordering and improve readability. command_and_package, = step_command_short.split .map { |arg| arg.sub(/=.*/, "=") } .partition { |arg| !arg.start_with?("-") } command = (command_and_package + .sort).join(" ") # Fields can have high cardinality. fields = { command: } report_influx(:test_bot_test, , fields) end |
.table_output(category, days, results, os_version: false, cask_install: false) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'utils/analytics.rb', line 426 def table_output(category, days, results, os_version: false, cask_install: false) oh1 "#{category} (#{days} days)" total_count = results.values.sum formatted_total_count = format_count(total_count) formatted_total_percent = format_percent(100) index_header = "Index" count_header = "Count" percent_header = "Percent" = if os_version "macOS Version" elsif cask_install "Token" else "Name (with options)" end = "Total" max_index_width = results.length.to_s.length index_width = [ index_header.length, .length, max_index_width, ].max count_width = [ count_header.length, formatted_total_count.length, ].max percent_width = [ percent_header.length, formatted_total_percent.length, ].max = Tty.width - index_width - count_width - percent_width - 10 # spacing and lines formatted_index_header = format "%#{index_width}s", index_header = format "%-#{}s", [0..(-1)] formatted_count_header = format "%#{count_width}s", count_header formatted_percent_header = format "%#{percent_width}s", percent_header puts "#{formatted_index_header} | #{} | " \ "#{formatted_count_header} | #{formatted_percent_header}" columns_line = "#{"-"*index_width}:|-#{"-"*}-|-" \ "#{"-"*count_width}:|-#{"-"*percent_width}:" puts columns_line index = 0 results.each do |, count| index += 1 formatted_index = format "%0#{max_index_width}d", index formatted_index = format "%-#{index_width}s", formatted_index = format "%-#{}s", [0..(-1)] formatted_count = format "%#{count_width}s", format_count(count) formatted_percent = if total_count.zero? format "%#{percent_width}s", format_percent(0) else format "%#{percent_width}s", format_percent((count.to_i * 100) / total_count.to_f) end puts "#{formatted_index} | #{} | " \ "#{formatted_count} | #{formatted_percent}%" next if index > 10 end return if results.length <= 1 = format "%-#{index_width}s", = format "%-#{}s", "" = format "%#{count_width}s", formatted_total_count = format "%#{percent_width}s", formatted_total_percent puts "#{} | #{} | " \ "#{} | #{}%" end |
.with_wsl_suffix_if_needed(value, wsl: OS.wsl?) ⇒ String
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
371 372 373 374 375 |
# File 'utils/analytics.rb', line 371 def with_wsl_suffix_if_needed(value, wsl: OS.wsl?) return value if !wsl || value.end_with?(WSL_SUFFIX) "#{value}#{WSL_SUFFIX}" end |