Class: Homebrew::DevCmd::FormulaAnalytics Private
- Inherits:
-
AbstractCommand
- Object
- AbstractCommand
- Homebrew::DevCmd::FormulaAnalytics
- Defined in:
- dev-cmd/formula-analytics.rb,
sorbet/rbi/dsl/homebrew/dev_cmd/formula_analytics.rbi
This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.
Defined Under Namespace
Classes: Args
Constant Summary collapse
- FIRST_INFLUXDB_ANALYTICS_DATE =
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.
T.let(Date.new(2023, 03, 27).freeze, Date)
Instance Method Summary collapse
- #args ⇒ Homebrew::DevCmd::FormulaAnalytics::Args private
- #format_count(count) ⇒ String private
- #format_os_version_dimension(dimension) ⇒ String? private
- #format_percent(percent) ⇒ String private
- #influx_analytics(args) ⇒ void private
- #run ⇒ void private
- #setup_python ⇒ void private
Methods inherited from AbstractCommand
command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?
Constructor Details
This class inherits a constructor from Homebrew::AbstractCommand
Instance Method Details
#args ⇒ Homebrew::DevCmd::FormulaAnalytics::Args
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.
10 |
# File 'sorbet/rbi/dsl/homebrew/dev_cmd/formula_analytics.rbi', line 10 def args; 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.
352 353 354 |
# File 'dev-cmd/formula-analytics.rb', line 352 def format_count(count) count.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse end |
#format_os_version_dimension(dimension) ⇒ 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.
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'dev-cmd/formula-analytics.rb', line 362 def format_os_version_dimension(dimension) return if dimension.blank? dimension = dimension.gsub(/^Intel ?/, "") .gsub(/^macOS ?/, "") .gsub(/ \(.+\)$/, "") case dimension when "10.11", /^10\.11\.?/ then "OS X El Capitan (10.11)" when "10.12", /^10\.12\.?/ then "macOS Sierra (10.12)" when "10.13", /^10\.13\.?/ then "macOS High Sierra (10.13)" when "10.14", /^10\.14\.?/ then "macOS Mojave (10.14)" when "10.15", /^10\.15\.?/ then "macOS Catalina (10.15)" when "10.16", /^11\.?/ then "macOS Big Sur (11)" when /^12\.?/ then "macOS Monterey (12)" when /^13\.?/ then "macOS Ventura (13)" when /^14\.?/ then "macOS Sonoma (14)" when /^15\.?/ then "macOS Sequoia (15)" when /Ubuntu(-Server)? (14|16|18|20|22)\.04/ then "Ubuntu #{Regexp.last_match(2)}.04 LTS" when /Ubuntu(-Server)? (\d+\.\d+).\d ?(LTS)?/ "Ubuntu #{Regexp.last_match(2)} #{Regexp.last_match(3)}".strip when %r{Debian GNU/Linux (\d+)\.\d+} then "Debian #{Regexp.last_match(1)} #{Regexp.last_match(2)}" when /CentOS (\w+) (\d+)/ then "CentOS #{Regexp.last_match(1)} #{Regexp.last_match(2)}" when /Fedora Linux (\d+)[.\d]*/ then "Fedora Linux #{Regexp.last_match(1)}" when /KDE neon .*?([\d.]+)/ then "KDE neon #{Regexp.last_match(1)}" when /Amazon Linux (\d+)\.[.\d]*/ then "Amazon Linux #{Regexp.last_match(1)}" else dimension end 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.
357 358 359 |
# File 'dev-cmd/formula-analytics.rb', line 357 def format_percent(percent) format("%<percent>.2f", percent:).gsub(/\.00$/, "") end |
#influx_analytics(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.
103 104 105 106 107 108 109 110 111 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 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 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'dev-cmd/formula-analytics.rb', line 103 def influx_analytics(args) require "utils/analytics" require "json" return if args.setup? odie "HOMEBREW_NO_ANALYTICS is set!" if ENV["HOMEBREW_NO_ANALYTICS"] token = ENV.fetch("HOMEBREW_INFLUXDB_TOKEN", nil) odie "No InfluxDB credentials found in HOMEBREW_INFLUXDB_TOKEN!" unless token client = InfluxDBClient3.new( token:, host: URI.parse(Utils::Analytics::INFLUX_HOST).host, org: Utils::Analytics::INFLUX_ORG, database: Utils::Analytics::INFLUX_BUCKET, ) max_days_ago = (Date.today - FIRST_INFLUXDB_ANALYTICS_DATE).to_s.to_i days_ago = (args.days_ago || 30).to_i if days_ago > max_days_ago opoo "Analytics started #{FIRST_INFLUXDB_ANALYTICS_DATE}. `--days-ago` set to maximum value." days_ago = max_days_ago end if days_ago > 365 opoo "Analytics are only retained for 1 year, setting `--days-ago=365`." days_ago = 365 end all_core_formulae_json = args.all_core_formulae_json? categories = [] categories << :build_error if args.build_error? categories << :cask_install if args.cask_install? categories << :formula_install if args.install? categories << :formula_install_on_request if args.install_on_request? categories << :homebrew_devcmdrun_developer if args.homebrew_devcmdrun_developer? categories << :homebrew_os_arch_ci if args.homebrew_os_arch_ci? categories << :homebrew_prefixes if args.homebrew_prefixes? categories << :homebrew_versions if args.homebrew_versions? categories << :os_versions if args.os_version? categories << :command_run if args.brew_command_run? categories << :command_run_options if args. categories << :test_bot_test if args.brew_test_bot_test? category_matching_buckets = [:build_error, :cask_install, :command_run, :test_bot_test] categories.each do |category| additional_where = all_core_formulae_json ? " AND tap_name ~ '^homebrew/(core|cask)$'" : "" bucket = if category_matching_buckets.include?(category) category elsif category == :command_run_options :command_run else :formula_install end case category when :homebrew_devcmdrun_developer dimension_key = "devcmdrun_developer" groups = [:devcmdrun, :developer] when :homebrew_os_arch_ci dimension_key = "os_arch_ci" groups = [:os, :arch, :ci] when :homebrew_prefixes dimension_key = "prefix" groups = [:prefix, :os, :arch] when :homebrew_versions dimension_key = "version" groups = [:version] when :os_versions dimension_key = :os_version groups = [:os_name_and_version] when :command_run dimension_key = "command_run" groups = [:command] when :command_run_options dimension_key = "command_run_options" groups = [:command, :options, :devcmdrun, :developer] additional_where += " AND ci = 'false'" when :test_bot_test dimension_key = "test_bot_test" groups = [:command, :passed, :arch, :os] when :cask_install dimension_key = :cask groups = [:package, :tap_name] else dimension_key = :formula additional_where += " AND on_request = 'true'" if category == :formula_install_on_request groups = [:package, :tap_name, :options] end sql_groups = groups.map { |e| "\"#{e}\"" }.join(",") query = <<~EOS SELECT #{sql_groups}, COUNT(*) AS "count" FROM "#{bucket}" WHERE time >= now() - INTERVAL '#{days_ago} day'#{additional_where} GROUP BY #{sql_groups} EOS batches = begin client.query(query:, language: "sql").to_batches rescue PyCall::PyError => e if e..include?("message: unauthenticated") odie "Could not authenticate with InfluxDB! Please check your HOMEBREW_INFLUXDB_TOKEN!" end raise end json = T.let({ category:, total_items: 0, start_date: Date.today - days_ago.to_i, end_date: Date.today, total_count: 0, items: [], }, T::Hash[Symbol, T.untyped]) batches.each do |batch| batch.to_pylist.each do |record| dimension = case category when :homebrew_devcmdrun_developer "devcmdrun=#{record["devcmdrun"]} HOMEBREW_DEVELOPER=#{record["developer"]}" when :homebrew_os_arch_ci if record["ci"] == "true" "#{record["os"]} #{record["arch"]} (CI)" else "#{record["os"]} #{record["arch"]}" end when :homebrew_prefixes if record["prefix"] == "custom-prefix" "#{record["prefix"]} (#{record["os"]} #{record["arch"]})" else record["prefix"].to_s end when :os_versions format_os_version_dimension(record["os_name_and_version"]) when :command_run_options = record["options"].split # Cleanup bad data before TODO # Can delete this code after 18th July 2025. .reject! { |option| option.match?(/^--with(out)?-/) } next if .any? { |option| option.match?(/^TMPDIR=/) } "#{record["command"]} #{.sort.join(" ")}" when :test_bot_test command_and_package, = record["command"].split.partition { |arg| !arg.start_with?("-") } # Cleanup bad data before https://github.com/Homebrew/homebrew-test-bot/pull/1043 # Can delete this code after 27th April 2025. next if %w[audit install linkage style test].exclude?(command_and_package.first) next if command_and_package.last.include?("/") next if .include?("--tap=") next if .include?("--only-dependencies") next if .include?("--cached") = (command_and_package + .sort).join(" ") passed = (record["passed"] == "true") ? "PASSED" : "FAILED" "#{} (#{record["os"]} #{record["arch"]}) (#{passed})" else record[groups.first.to_s] end next if dimension.blank? if (tap_name = record["tap_name"].presence) && ((tap_name != "homebrew/cask" && dimension_key == :cask) || (tap_name != "homebrew/core" && dimension_key == :formula)) dimension = "#{tap_name}/#{dimension}" end if (all_core_formulae_json || category == :build_error) && ( = record["options"].presence) # homebrew/core formulae don't have non-HEAD options but they ended up in our analytics anyway. if all_core_formulae_json = .split.include?("--HEAD") ? "--HEAD" : "" end dimension = "#{dimension} #{}" end dimension = dimension.strip next if dimension.match?(/[<>]/) count = record["count"] json[:total_items] += 1 json[:total_count] += count json[:items] << { number: nil, dimension_key => dimension, count:, } end end odie "No data returned" if json[:total_count].zero? # Combine identical values deduped_items = {} json[:items].each do |item| key = item[dimension_key] if deduped_items.key?(key) deduped_items[key][:count] += item[:count] else deduped_items[key] = item end end json[:items] = deduped_items.values if all_core_formulae_json core_formula_items = {} json[:items].each do |item| item.delete(:number) formula_name, = item[dimension_key].split.first next if formula_name.include?("/") core_formula_items[formula_name] ||= [] core_formula_items[formula_name] << item end json.delete(:items) core_formula_items.each_value do |items| items.sort_by! { |item| -item[:count] } items.each do |item| item[:count] = format_count(item[:count]) end end json[:formulae] = core_formula_items.sort_by { |name, _| name }.to_h else json[:items].sort_by! do |item| -item[:count] end json[:items].each_with_index do |item, index| item[:number] = index + 1 percent = (item[:count].to_f / json[:total_count]) * 100 item[:percent] = format_percent(percent) item[:count] = format_count(item[:count]) end end puts JSON.pretty_generate json end end |
#run ⇒ 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.
59 60 61 62 63 64 |
# File 'dev-cmd/formula-analytics.rb', line 59 def run Homebrew.install_bundler_gems!(groups: ["formula_analytics"]) setup_python influx_analytics(args) end |
#setup_python ⇒ 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.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'dev-cmd/formula-analytics.rb', line 67 def setup_python formula_analytics_root = HOMEBREW_LIBRARY/"Homebrew/formula-analytics" vendor_python = Pathname.new("~/.brew-formula-analytics/vendor/python"). python_version = (formula_analytics_root/".python-version").read.chomp which_python = which("python#{python_version}", ORIGINAL_PATHS) odie <<~EOS if which_python.nil? Python #{python_version} is required. Try: brew install python@#{python_version} EOS venv_root = vendor_python/python_version vendor_python.children.reject { |path| path == venv_root }.each(&:rmtree) if vendor_python.exist? venv_python = venv_root/"bin/python" repo_requirements = HOMEBREW_LIBRARY/"Homebrew/formula-analytics/requirements.txt" venv_requirements = venv_root/"requirements.txt" if !venv_requirements.exist? || !FileUtils.identical?(repo_requirements, venv_requirements) safe_system which_python, "-I", "-m", "venv", "--clear", venv_root, out: :err safe_system venv_python, "-m", "pip", "install", "--disable-pip-version-check", "--require-hashes", "--requirement", repo_requirements, out: :err FileUtils.cp repo_requirements, venv_requirements end ENV["PATH"] = "#{venv_root}/bin:#{ENV.fetch("PATH")}" ENV["__PYVENV_LAUNCHER__"] = venv_python.to_s # support macOS framework Pythons require "pycall" PyCall.init(venv_python) require formula_analytics_root/"pycall-setup" end |