debian-mirror-gitlab/lib/peek/views/detailed_view.rb

77 lines
1.7 KiB
Ruby
Raw Permalink Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
module Peek
module Views
class DetailedView < View
2019-12-04 20:38:33 +05:30
def self.thresholds
{}
end
2019-10-12 21:52:04 +05:30
def results
{
2019-12-04 20:38:33 +05:30
duration: format_duration(duration),
2019-10-12 21:52:04 +05:30
calls: calls,
2019-12-04 20:38:33 +05:30
details: details,
warnings: warnings
2019-10-12 21:52:04 +05:30
}
end
def detail_store
2020-04-22 19:07:51 +05:30
::Gitlab::SafeRequestStore["#{key}_call_details".to_sym] ||= []
2019-10-12 21:52:04 +05:30
end
private
def duration
2019-12-04 20:38:33 +05:30
detail_store.map { |entry| entry[:duration] }.sum * 1000 # rubocop:disable CodeReuse/ActiveRecord
2019-10-12 21:52:04 +05:30
end
def calls
detail_store.count
end
2019-12-04 20:38:33 +05:30
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-10-12 21:52:04 +05:30
def call_details
detail_store
end
def format_call_details(call)
2019-12-04 20:38:33 +05:30
duration = (call[:duration] * 1000).round(3)
2019-10-12 21:52:04 +05:30
2019-12-04 20:38:33 +05:30
call.merge(duration: duration,
warnings: warning_for(duration, self.class.thresholds[:individual_call]))
2019-10-12 21:52:04 +05:30
end
2019-12-04 20:38:33 +05:30
def warning_for(actual, threshold, label: nil)
if threshold && actual > threshold
prefix = "#{label}: " if label
["#{prefix}#{actual} over #{threshold}"]
else
[]
end
end
2019-10-12 21:52:04 +05:30
2019-12-04 20:38:33 +05:30
def format_duration(ms)
2019-10-12 21:52:04 +05:30
if ms >= 1000
"%.2fms" % ms
else
"%.0fms" % ms
end
end
end
end
end