This repository has been archived by the owner on May 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_stats.rb
79 lines (67 loc) · 1.96 KB
/
time_stats.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
require 'array_ext'
class TimeStats
attr_accessor :logs, :times
def initialize(logs)
@times = []
@logs = logs
end
def self.generate(logs)
ts = new(logs)
ts.parse_files
ts.save_reports
end
def parse_files
logs.each do |log|
results = `grep "Processing" #{log}`
timestamps = results.map { |line| (line =~ /Processing.*\(for .* at (\d+-\d+-\d+ \d+:\d+:\d+)\)/) ? $1 : nil}.compact
times.concat(timestamps)
end
end
def save_reports
%w[day hour day_hour].each do |timeframe|
filename = 'log_times_%s.txt' % timeframe
save_hits_report(filename, timeframe)
end
%w[hour ten_min].each do |timeframe|
hits = self.send('per_%s' % timeframe).map {|frame, hits| hits.size}
offset = hits.min || 0
filename = 'log_times_%s_relative.txt' % timeframe
save_hits_report(filename, timeframe, offset)
end
end
protected
def save_hits_report(filename, timeframe, offset = 0)
collection = self.send('per_%s' % timeframe)
File.open(filename,'wb') do |f|
collection.each do |frame, hits|
f.puts '%s: %s' % [frame, hits.size - offset]
end
end
end
def per_day_hour
group_times_by {|d, h, m, s| '%s %sh' % [d, h]}
end
def per_day
group_times_by {|d, h, m, s| d}
end
def per_hour
group_times_by {|d, h, m, s| h}
end
def per_ten_min
group_times_by {|d, h, m, s| '%sh%s0' % [h, m[0,1]]}
end
# Group times by date/time-related data.
# Sorts by the yield return value
# Returns an Array of two-element Arrays:
# - The first element is the sort key
# - The second element is the hit times for that key
# The return value is sorted by key.
def group_times_by(&block) # :yields: date, hour, min, sec
grouped_times = times.group_by do |t|
date, time = t.split(" ")
hour, min, sec = time.split(":")
block.call(date, hour, min, sec)
end
grouped_times.to_a.sort {|a,b| a.first <=> b.first}
end
end