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
|
# frozen_string_literal: true
module Peek
module Views
class DetailedView < View
def self.thresholds
{}
end
def results
{
duration: format_duration(duration),
calls: calls,
details: details,
warnings: warnings
}
end
def detail_store
::Gitlab::SafeRequestStore["#{key}_call_details"] ||= []
end
private
def duration
detail_store.map { |entry| entry[:duration] }.sum * 1000 # rubocop:disable CodeReuse/ActiveRecord
end
def calls
detail_store.count
end
def details
call_details
.sort { |a, b| b[:duration] <=> a[:duration] }
.map(&method(:format_call_details))
end
def warnings
[
warning_for(calls, self.class.thresholds[:calls], label: "#{key} calls"),
warning_for(duration, self.class.thresholds[:duration], label: "#{key} duration")
].flatten.compact
end
def call_details
detail_store
end
def format_call_details(call)
duration = (call[:duration] * 1000).round(3)
call.merge(duration: duration,
warnings: warning_for(duration, self.class.thresholds[:individual_call]))
end
def warning_for(actual, threshold, label: nil)
if threshold && actual > threshold
prefix = "#{label}: " if label
["#{prefix}#{actual} over #{threshold}"]
else
[]
end
end
def format_duration(ms)
if ms >= 1000
"%.2fms" % ms
else
"%.0fms" % ms
end
end
end
end
end
|