-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
segment_group.rb
134 lines (110 loc) · 4.41 KB
/
segment_group.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
100
101
102
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
class SegmentGroup
attr_accessor :run, :segments
def initialize(run, segments)
self.run = run
self.segments = segments
end
def id
"#{segments.first.id}-segment_group"
end
def display_name
match = /\{(.+?)}/.match(segments.last.name) || [nil, segments.last.name]
match[1]
end
def duration(timing)
segments.map do |segment|
segment.duration(timing)
end.sum
end
def end(timing)
segments.last.end(timing)
end
def gold?(timing)
shortest_duration(timing) == duration(timing)
end
def reduced?(timing)
segments.first.reduced?(timing)
end
def segment_group_parent?
true
end
def segment_number
segments.first.segment_number
end
def skipped?(timing)
segments.all? { |segment| segment.skipped?(timing) }
end
def shortest_duration(timing)
Duration.new(durations_by_attempt[timing].values.min)
end
def history_stats(timing)
values = durations_by_attempt[timing].values.sort
return {} if values.blank?
mean = values.sum / durations_by_attempt[timing].keys.length.to_f
variance_sum = values.inject(0) { |accum, i| accum + (i - mean)**2 }
sample_variance = values.length == 1 ? 0 : variance_sum / (durations_by_attempt[timing].keys.length - 1).to_f
{
standard_deviation: Math.sqrt(sample_variance),
mean: mean,
# Not actual median, but matches the DB query
median: values.length == 1 ? values[0] : values[values.length / 2 + 1],
percentiles: {
10 => percentile(values, 0.1),
90 => percentile(values, 0.9),
99 => percentile(values, 0.99)
}
}
end
def segment_group_durations
durations = {}
durations_by_attempt.keys.each do |timing|
durations_by_attempt[timing].keys.each do |attempt_number|
durations[attempt_number] = {
attempt_number: attempt_number,
'realtime_duration_ms' => 0,
'gametime_duration_ms' => 0
} unless durations[attempt_number]
durations[attempt_number]["#{timing}time_duration_ms"] = durations_by_attempt[timing][attempt_number]
end
end
durations.values.to_a
end
private
# durations_by_attempt returns a hash of realtime and gametime hashes each with a key of the attempt number and a
# value of the real/game time duration of the entire segment_group
def durations_by_attempt
return @durations_by_attempt if @durations_by_attempt
@durations_by_attempt = {
Run::REAL => Hash.new { |h, k| h[k] = [] },
Run::GAME => Hash.new { |h, k| h[k] = [] }
}
# First, collect all the durations for each segment in the group for each attempt
segments.each do |segment|
segment.histories.each do |history|
previous_segment = segments[segment.segment_number - 1]&.histories&.find { |attempt| attempt.attempt_number == history.attempt_number }
# Don't store a segment's duration if it is 0 or if the previous segment's duration was 0 (and thus skipped)
@durations_by_attempt[Run::REAL][history.attempt_number] << history.realtime_duration_ms unless (history.realtime_duration_ms || 0).zero? || (previous_segment && (previous_segment.realtime_duration_ms || 0).zero?)
@durations_by_attempt[Run::GAME][history.attempt_number] << history.gametime_duration_ms unless (history.gametime_duration_ms || 0).zero? || (previous_segment && (previous_segment.gametime_duration_ms || 0).zero?)
end
end
# Only keep attempts who have times for all segments
@durations_by_attempt.keys.each do |timing|
max_length = @durations_by_attempt[timing].values.map(&:length).max
@durations_by_attempt[timing].delete_if { |_key, value| value.length < max_length }
end
# Sum the segment times for each attempt and store those as opposed to the individual segment durations
@durations_by_attempt.keys.each do |timing|
@durations_by_attempt[timing].keys.each do |attempt_number|
@durations_by_attempt[timing][attempt_number] = @durations_by_attempt[timing][attempt_number].compact.sum
end
end
@durations_by_attempt
end
def percentile(values_sorted, percent)
return nil if values_sorted.blank?
return values_sorted[0] if values_sorted.length == 1
k = (percent * (values_sorted.length - 1) + 1).floor - 1
f = (percent * (values_sorted.length - 1) + 1).modulo(1)
values_sorted[k] + (f * (values_sorted[k + 1] - values_sorted[k]))
end
end