提交 d7eadcc0 编写于 作者: S Stan Hu

Use a base class for Peek views

Introduce a `DetailedView` base class, which is inherited by
the Gitaly, Redis, and Rugged views. This reduces code duplication.
上级 c02c83fa
# frozen_string_literal: true
module Peek
module Views
class DetailedView < View
def results
{
duration: formatted_duration,
calls: calls,
details: details
}
end
private
def duration
raise NotImplementedError
end
def calls
raise NotImplementedError
end
def call_details
raise NotImplementedError
end
def format_call_details(call)
raise NotImplementedError
end
def details
call_details
.sort { |a, b| b[:duration] <=> a[:duration] }
.map(&method(:format_call_details))
end
def formatted_duration
ms = duration * 1000
if ms >= 1000
"%.2fms" % ms
else
"%.0fms" % ms
end
end
end
end
end
......@@ -2,7 +2,9 @@
module Peek
module Views
class Gitaly < View
class Gitaly < DetailedView
private
def duration
::Gitlab::GitalyClient.query_time
end
......@@ -11,20 +13,8 @@ module Peek
::Gitlab::GitalyClient.get_request_count
end
def results
{
duration: formatted_duration,
calls: calls,
details: details
}
end
private
def details
def call_details
::Gitlab::GitalyClient.list_call_details
.sort { |a, b| b[:duration] <=> a[:duration] }
.map(&method(:format_call_details))
end
def format_call_details(call)
......@@ -34,15 +24,6 @@ module Peek
request: pretty_request || {})
end
def formatted_duration
ms = duration * 1000
if ms >= 1000
"%.2fms" % ms
else
"%.0fms" % ms
end
end
def setup_subscribers
subscribe 'start_processing.action_controller' do
::Gitlab::GitalyClient.query_time = 0
......
......@@ -35,36 +35,19 @@ end
module Peek
module Views
class RedisDetailed < View
class RedisDetailed < DetailedView
REDACTED_MARKER = "<redacted>"
def key
'redis'
end
def results
{
calls: calls,
duration: formatted_duration,
details: details
}
end
def detail_store
::Gitlab::SafeRequestStore['redis_call_details'] ||= []
end
private
def formatted_duration
ms = duration * 1000
if ms >= 1000
"%.2fms" % ms
else
"%.0fms" % ms
end
end
def duration
detail_store.map { |entry| entry[:duration] }.sum # rubocop:disable CodeReuse/ActiveRecord
end
......@@ -73,10 +56,8 @@ module Peek
detail_store.count
end
def details
def call_details
detail_store
.sort { |a, b| b[:duration] <=> a[:duration] }
.map(&method(:format_call_details))
end
def format_call_details(call)
......
......@@ -2,7 +2,15 @@
module Peek
module Views
class Rugged < View
class Rugged < DetailedView
def results
return {} unless calls > 0
super
end
private
def duration
::Gitlab::RuggedInstrumentation.query_time
end
......@@ -11,22 +19,8 @@ module Peek
::Gitlab::RuggedInstrumentation.query_count
end
def results
return {} unless calls > 0
{
duration: formatted_duration,
calls: calls,
details: details
}
end
private
def details
def call_details
::Gitlab::RuggedInstrumentation.list_call_details
.sort { |a, b| b[:duration] <=> a[:duration] }
.map(&method(:format_call_details))
end
def format_call_details(call)
......@@ -44,15 +38,6 @@ module Peek
end
end
end
def formatted_duration
ms = duration * 1000
if ms >= 1000
"%.2fms" % ms
else
"%.0fms" % ms
end
end
end
end
end
......@@ -27,9 +27,6 @@ describe Peek::Views::Rugged, :request_store do
args: [project.repository.raw, 'refs/heads/master'],
duration: 0.456)
expect(subject.duration).to be_within(0.00001).of(1.234)
expect(subject.calls).to eq(2)
results = subject.results
expect(results[:calls]).to eq(2)
expect(results[:duration]).to eq('1234.00ms')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册