-
Notifications
You must be signed in to change notification settings - Fork 0
/
crunch-android-devices.rb
executable file
·57 lines (45 loc) · 1.47 KB
/
crunch-android-devices.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env ruby -w
require 'csv'
HEADERS = %w[device_name sessions].freeze
DEVICES_PATH = File.join(__dir__, 'supported_devices.csv')
# Maps device model to name using Google's giant CSV that lives alongside this file.
DEVICE_MAP = CSV.foreach(DEVICES_PATH).each_with_object({}) do |row, map|
# skip the first header row
next if row[0] == 'Retail Branding'
# columns: Retail Branding (maker), Marketing Name, Device (unused), Model
maker = row[0]
name = row[1]
model = row[3]
map[model] = "#{maker} #{name}"
end
def main
in_csv = CSV.new(ARGF)
sessions_by_device = count_devices(in_csv)
render_csv(sessions_by_device)
end
def zero_hash
Hash.new { |_k, _v| 0 }
end
def count_devices(in_csv)
# skip the first header row
in_csv.drop(1).each_with_object(zero_hash) do |row, h|
# devices come in a raw model and we have to look up the marketing name for each one
# e.g. SM-S908N and SM-S908U are 2 of 9 models of the Galaxy S22 Ultra line
device_model = row[0]
device_name = DEVICE_MAP[device_model] || device_model
# Skip things that are obviously not Android
next if device_name =~ /iphone|ipad/i
h[device_name] += 1
end
end
def render_csv(sessions_by_device)
puts CSV.generate_line(HEADERS)
sessions_by_device
.sort_by { |_device, sessions| sessions }
.reverse
.each do |device_name, sessions|
out_row = [device_name, sessions]
puts CSV.generate_line(out_row)
end
end
main if $PROGRAM_NAME == __FILE__