-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathmain.rb
99 lines (85 loc) · 3.97 KB
/
main.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
58
59
60
61
62
63
64
65
66
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
require 'progressbar'
class Main
PARTIAL_VOLUME_FILE_NAME = "dataN.txt"
def initialize(options: {})
@source_file_name = options[:source_file_name] || 'data_large.txt'
@count_lines = options[:count_lines]
@with_gc = options[:with_gc] || true
@process_file_progress_bar = options[:with_progress_bar] ? ProgressBar.create(title: "Process file") : nil
@collect_report_progress_bar = options[:with_progress_bar] ? ProgressBar.create(title: "Collect report") : nil
@write_to_result_file_progress_bar = options[:with_progress_bar] ? ProgressBar.create(title: "Write to result file") : nil
end
def call
GC.disable unless with_gc
work
GC.start
end
private
attr_reader :source_file_name, :count_lines, :with_gc, :process_file_progress_bar, :collect_report_progress_bar, :write_to_result_file_progress_bar
def work
`head -n #{count_lines} #{source_file_name} > #{PARTIAL_VOLUME_FILE_NAME}` if count_lines
user_id_users = {}
user_id_stats = {}
total_sessions = 0
uniq_browsers = Set.new
with_progress_bar(process_file_progress_bar) do
File.foreach(count_lines ? PARTIAL_VOLUME_FILE_NAME : source_file_name).each do |line|
fields = line.split(',')
case fields[0]
when 'user'
user = { 'id' => fields[1], 'first_name' => fields[2], 'last_name' => fields[3], 'age' => fields[4] }
user_id_users[user['id']] = user
when 'session'
session = { 'user_id' => fields[1], 'session_id' => fields[2], 'browser' => fields[3], 'time' => fields[4], 'date' => fields[5] }
user_stat = user_id_stats[session['user_id']]
sessions_count = user_stat&.dig('sessions_count') || 0
total_time = user_stat&.dig('total_time') || 0
longest_session = user_stat&.dig('longest_session') || 0
browsers = user_stat&.dig('browsers') || []
dates = user_stat&.dig('dates') || []
is_used_ie = user_stat&.dig('used_ie') || false
is_always_used_chrome = user_stat&.dig('is_used_chrome') || false
user_id_stats[session['user_id']] = (user_id_stats[session['user_id']] || {})
user_id_stats[session['user_id']] = {
'sessions_count' => sessions_count + 1,
'total_time' => total_time + session['time'].to_i,
'longest_session' => session['time'].to_i > longest_session ? session['time'].to_i : longest_session,
'browsers' => browsers << session['browser'].upcase,
'used_ie' => is_used_ie ? true : !!(session['browser'].upcase =~ /INTERNET EXPLORER/),
'always_used_chrome' => is_always_used_chrome ? !!(session['browser'].upcase =~ /CHROME/) : false,
'dates' => dates << Date.strptime(session['date'], '%Y-%m-%d').iso8601
}
total_sessions += 1
uniq_browsers.add(session['browser'].upcase)
end
end
end
report = {}
with_progress_bar(collect_report_progress_bar) do
report['totalUsers'] = user_id_users.keys.count
report['uniqueBrowsersCount'] = uniq_browsers.count
report['totalSessions'] = total_sessions
report['allBrowsers'] = uniq_browsers.sort.join(',')
report['usersStats'] = {}
user_id_stats.each do |user_id, stat|
user = user_id_users[user_id]
user_key = "#{user['first_name']} #{user['last_name']}"
report['usersStats'][user_key] = {
'sessionsCount' => stat['sessions_count'],
'totalTime' => "#{stat['total_time']} min.",
'longestSession' => "#{stat['longest_session']} min.",
'browsers' => stat['browsers'].sort.join(', '),
'usedIE' => stat['used_ie'],
'alwaysUsedChrome' => stat['always_used_chrome'],
'dates' => stat['dates'].sort.reverse
}
end
end
with_progress_bar(write_to_result_file_progress_bar) { File.write('result.json', "#{report.to_json}\n") }
end
def with_progress_bar(bar)
bar&.progress = 0
yield
bar&.progress = 100
end
end