2019-07-24 10:42:45 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Peek
|
|
|
|
module Views
|
|
|
|
class DetailedView < View
|
2019-08-06 11:04:43 -04:00
|
|
|
def self.thresholds
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
2019-07-24 10:42:45 -04:00
|
|
|
def results
|
|
|
|
{
|
2019-08-06 11:04:43 -04:00
|
|
|
duration: format_duration(duration),
|
2019-07-24 10:42:45 -04:00
|
|
|
calls: calls,
|
2019-08-06 11:04:43 -04:00
|
|
|
details: details,
|
|
|
|
warnings: warnings
|
2019-07-24 10:42:45 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-07-26 09:03:00 -04:00
|
|
|
def detail_store
|
2020-03-24 14:07:55 -04:00
|
|
|
::Gitlab::SafeRequestStore["#{key}_call_details".to_sym] ||= []
|
2019-07-26 09:03:00 -04:00
|
|
|
end
|
|
|
|
|
2019-07-24 10:42:45 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def duration
|
2022-02-24 07:12:57 -05:00
|
|
|
detail_store.sum { |entry| entry[:duration] } * 1000
|
2019-07-24 10:42:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def calls
|
2019-07-26 09:03:00 -04:00
|
|
|
detail_store.count
|
2019-07-24 10:42:45 -04:00
|
|
|
end
|
|
|
|
|
2019-08-06 11:04:43 -04:00
|
|
|
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
|
|
|
|
|
2019-07-24 10:42:45 -04:00
|
|
|
def call_details
|
2019-07-26 09:03:00 -04:00
|
|
|
detail_store
|
2019-07-24 10:42:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def format_call_details(call)
|
2019-08-06 11:04:43 -04:00
|
|
|
duration = (call[:duration] * 1000).round(3)
|
2019-07-24 10:42:45 -04:00
|
|
|
|
2019-08-06 11:04:43 -04:00
|
|
|
call.merge(duration: duration,
|
|
|
|
warnings: warning_for(duration, self.class.thresholds[:individual_call]))
|
2019-07-24 10:42:45 -04:00
|
|
|
end
|
|
|
|
|
2019-08-06 11:04:43 -04:00
|
|
|
def warning_for(actual, threshold, label: nil)
|
|
|
|
if threshold && actual > threshold
|
|
|
|
prefix = "#{label}: " if label
|
|
|
|
|
|
|
|
["#{prefix}#{actual} over #{threshold}"]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
2019-07-24 10:42:45 -04:00
|
|
|
|
2019-08-06 11:04:43 -04:00
|
|
|
def format_duration(ms)
|
2019-07-24 10:42:45 -04:00
|
|
|
if ms >= 1000
|
|
|
|
"%.2fms" % ms
|
|
|
|
else
|
|
|
|
"%.0fms" % ms
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|