Commit 6a5d2df3 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'sh-peek-cleanup' into 'master'

Use a base class for Peek views

See merge request gitlab-org/gitlab-ce!31108
parents d86ef0b0 d7eadcc0
# 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 @@ ...@@ -2,7 +2,9 @@
module Peek module Peek
module Views module Views
class Gitaly < View class Gitaly < DetailedView
private
def duration def duration
::Gitlab::GitalyClient.query_time ::Gitlab::GitalyClient.query_time
end end
...@@ -11,20 +13,8 @@ module Peek ...@@ -11,20 +13,8 @@ module Peek
::Gitlab::GitalyClient.get_request_count ::Gitlab::GitalyClient.get_request_count
end end
def results def call_details
{
duration: formatted_duration,
calls: calls,
details: details
}
end
private
def details
::Gitlab::GitalyClient.list_call_details ::Gitlab::GitalyClient.list_call_details
.sort { |a, b| b[:duration] <=> a[:duration] }
.map(&method(:format_call_details))
end end
def format_call_details(call) def format_call_details(call)
...@@ -34,15 +24,6 @@ module Peek ...@@ -34,15 +24,6 @@ module Peek
request: pretty_request || {}) request: pretty_request || {})
end end
def formatted_duration
ms = duration * 1000
if ms >= 1000
"%.2fms" % ms
else
"%.0fms" % ms
end
end
def setup_subscribers def setup_subscribers
subscribe 'start_processing.action_controller' do subscribe 'start_processing.action_controller' do
::Gitlab::GitalyClient.query_time = 0 ::Gitlab::GitalyClient.query_time = 0
......
...@@ -35,36 +35,19 @@ end ...@@ -35,36 +35,19 @@ end
module Peek module Peek
module Views module Views
class RedisDetailed < View class RedisDetailed < DetailedView
REDACTED_MARKER = "<redacted>" REDACTED_MARKER = "<redacted>"
def key def key
'redis' 'redis'
end end
def results
{
calls: calls,
duration: formatted_duration,
details: details
}
end
def detail_store def detail_store
::Gitlab::SafeRequestStore['redis_call_details'] ||= [] ::Gitlab::SafeRequestStore['redis_call_details'] ||= []
end end
private private
def formatted_duration
ms = duration * 1000
if ms >= 1000
"%.2fms" % ms
else
"%.0fms" % ms
end
end
def duration def duration
detail_store.map { |entry| entry[:duration] }.sum # rubocop:disable CodeReuse/ActiveRecord detail_store.map { |entry| entry[:duration] }.sum # rubocop:disable CodeReuse/ActiveRecord
end end
...@@ -73,10 +56,8 @@ module Peek ...@@ -73,10 +56,8 @@ module Peek
detail_store.count detail_store.count
end end
def details def call_details
detail_store detail_store
.sort { |a, b| b[:duration] <=> a[:duration] }
.map(&method(:format_call_details))
end end
def format_call_details(call) def format_call_details(call)
......
...@@ -2,7 +2,15 @@ ...@@ -2,7 +2,15 @@
module Peek module Peek
module Views module Views
class Rugged < View class Rugged < DetailedView
def results
return {} unless calls > 0
super
end
private
def duration def duration
::Gitlab::RuggedInstrumentation.query_time ::Gitlab::RuggedInstrumentation.query_time
end end
...@@ -11,22 +19,8 @@ module Peek ...@@ -11,22 +19,8 @@ module Peek
::Gitlab::RuggedInstrumentation.query_count ::Gitlab::RuggedInstrumentation.query_count
end end
def results def call_details
return {} unless calls > 0
{
duration: formatted_duration,
calls: calls,
details: details
}
end
private
def details
::Gitlab::RuggedInstrumentation.list_call_details ::Gitlab::RuggedInstrumentation.list_call_details
.sort { |a, b| b[:duration] <=> a[:duration] }
.map(&method(:format_call_details))
end end
def format_call_details(call) def format_call_details(call)
...@@ -44,15 +38,6 @@ module Peek ...@@ -44,15 +38,6 @@ module Peek
end end
end end
end end
def formatted_duration
ms = duration * 1000
if ms >= 1000
"%.2fms" % ms
else
"%.0fms" % ms
end
end
end end
end end
end end
...@@ -27,9 +27,6 @@ describe Peek::Views::Rugged, :request_store do ...@@ -27,9 +27,6 @@ describe Peek::Views::Rugged, :request_store do
args: [project.repository.raw, 'refs/heads/master'], args: [project.repository.raw, 'refs/heads/master'],
duration: 0.456) duration: 0.456)
expect(subject.duration).to be_within(0.00001).of(1.234)
expect(subject.calls).to eq(2)
results = subject.results results = subject.results
expect(results[:calls]).to eq(2) expect(results[:calls]).to eq(2)
expect(results[:duration]).to eq('1234.00ms') expect(results[:duration]).to eq('1234.00ms')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment