Skip to content

Commit

Permalink
Merge pull request dejan#163 from dejan/rubocop
Browse files Browse the repository at this point in the history
Add Rubocop
  • Loading branch information
dejan authored Aug 5, 2019
2 parents 5e12ea7 + e47f8f2 commit 53f3d5d
Show file tree
Hide file tree
Showing 33 changed files with 229 additions and 115 deletions.
23 changes: 17 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
version: 2
jobs:
rubocop:
docker:
- image: circleci/buildpack-deps
working_directory: ~/project/meta_request
steps:
- checkout:
path: ~/project
- setup_remote_docker
- run: docker-compose run --rm rubocop

unit_tests:
docker:
- image: circleci/buildpack-deps
Expand All @@ -8,7 +18,7 @@ jobs:
- checkout:
path: ~/project
- setup_remote_docker
- run: docker-compose run test-unit
- run: docker-compose run --rm test-unit

test_rails_4_1:
docker:
Expand All @@ -18,7 +28,7 @@ jobs:
- checkout:
path: ~/project
- setup_remote_docker
- run: docker-compose run test-rails-4.1
- run: docker-compose run --rm test-rails-4.1

test_rails_5_0:
docker:
Expand All @@ -28,7 +38,7 @@ jobs:
- checkout:
path: ~/project
- setup_remote_docker
- run: docker-compose run test-rails-5.0
- run: docker-compose run --rm test-rails-5.0

test_rails_5_1:
docker:
Expand All @@ -38,7 +48,7 @@ jobs:
- checkout:
path: ~/project
- setup_remote_docker
- run: docker-compose run test-rails-5.1
- run: docker-compose run --rm test-rails-5.1

test_rails_5_2:
docker:
Expand All @@ -48,7 +58,7 @@ jobs:
- checkout:
path: ~/project
- setup_remote_docker
- run: docker-compose run test-rails-5.2
- run: docker-compose run --rm test-rails-5.2

test_rails_6_0:
docker:
Expand All @@ -58,12 +68,13 @@ jobs:
- checkout:
path: ~/project
- setup_remote_docker
- run: docker-compose run test-rails-6.0
- run: docker-compose run --rm test-rails-6.0

workflows:
version: 2
test_all:
jobs:
- rubocop
- unit_tests
- test_rails_4_1
- test_rails_5_0
Expand Down
1 change: 1 addition & 0 deletions meta_request/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inherit_from: .rubocop_todo.yml
54 changes: 54 additions & 0 deletions meta_request/.rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-08-05 19:13:21 +0000 using RuboCop version 0.74.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 3
Metrics/AbcSize:
Max: 23

# Offense count: 3
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 33

# Offense count: 5
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 18

# Offense count: 1
Style/CaseEquality:
Exclude:
- 'lib/meta_request/event.rb'

# Offense count: 14
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'lib/meta_request.rb'
- 'lib/meta_request/app_notifications.rb'
- 'lib/meta_request/app_request.rb'
- 'lib/meta_request/config.rb'
- 'lib/meta_request/log_interceptor.rb'
- 'lib/meta_request/middlewares.rb'
- 'lib/meta_request/middlewares/app_request_handler.rb'
- 'lib/meta_request/middlewares/headers.rb'
- 'lib/meta_request/middlewares/meta_request_handler.rb'
- 'lib/meta_request/railtie.rb'
- 'lib/meta_request/storage.rb'
- 'lib/meta_request/utils.rb'
- 'res/dummy_controller.rb'
- 'res/meta_request_test.rb'

# Offense count: 26
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 105
5 changes: 4 additions & 1 deletion meta_request/Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# frozen_string_literal: true

source 'https://rubygems.org'

group :development do
gem 'bundler'
gem 'rake'
gem 'minitest'
gem 'minitest-ci'
gem 'rake'
gem 'rubocop', '~> 0.74.0', require: false
end

gemspec
4 changes: 3 additions & 1 deletion meta_request/Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rake/testtask'

Expand All @@ -7,4 +9,4 @@ Rake::TestTask.new do |t|
t.test_files = FileList['test/unit/**/*_test.rb']
end

task :default => :test
task default: :test
6 changes: 6 additions & 0 deletions meta_request/docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '3.7'
services:
console:
build: .
volumes:
- .:/app
3 changes: 3 additions & 0 deletions meta_request/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
version: '3.7'
services:
rubocop:
build: .
command: bundle exec rubocop
test-unit:
build: .
test-rails-4.1:
Expand Down
22 changes: 12 additions & 10 deletions meta_request/lib/meta_request.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# frozen_string_literal: true

module MetaRequest
autoload :VERSION, "meta_request/version"
autoload :Config, "meta_request/config"
autoload :Event, "meta_request/event"
autoload :AppRequest, "meta_request/app_request"
autoload :Storage, "meta_request/storage"
autoload :Middlewares, "meta_request/middlewares"
autoload :LogInterceptor, "meta_request/log_interceptor"
autoload :AppNotifications, "meta_request/app_notifications"
autoload :Utils, "meta_request/utils"
autoload :VERSION, 'meta_request/version'
autoload :Config, 'meta_request/config'
autoload :Event, 'meta_request/event'
autoload :AppRequest, 'meta_request/app_request'
autoload :Storage, 'meta_request/storage'
autoload :Middlewares, 'meta_request/middlewares'
autoload :LogInterceptor, 'meta_request/log_interceptor'
autoload :AppNotifications, 'meta_request/app_notifications'
autoload :Utils, 'meta_request/utils'

def self.config
@config ||= Config.new
Expand All @@ -24,4 +26,4 @@ def self.rails_root
end
end

require "meta_request/railtie"
require 'meta_request/railtie'
51 changes: 25 additions & 26 deletions meta_request/lib/meta_request/app_notifications.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# frozen_string_literal: true

module MetaRequest
class AppNotifications

# these are the specific keys in the cache payload that we display in the
# panel view
CACHE_KEY_COLUMNS = [:key, :hit, :options, :type]
CACHE_KEY_COLUMNS = %i[key hit options type].freeze

# define this here so we can pass it in to all of our cache subscribe calls
CACHE_BLOCK = Proc.new {|*args|
CACHE_BLOCK = proc { |*args|
name, start, ending, transaction_id, payload = args

# from http://edgeguides.rubyonrails.org/active_support_instrumentation.html#cache-fetch-hit-active-support
#
# :super_operation :fetch is added when a read is used with #fetch
# :super_operation :fetch is added when a read is used with #fetch
#
# so if :super_operation is present, we'll use it for the type. otherwise
# strip (say) 'cache_delete.active_support' down to 'delete'
Expand All @@ -36,17 +37,17 @@ class AppNotifications
# display sequel events without modification. otherwise the ui would need to
# be modified to support a sequel tab (or to change the display name on the
# active_record tab when necessary - which maybe makes more sense?)
SQL_EVENT_NAME = "sql.active_record"
SQL_EVENT_NAME = 'sql.active_record'

SQL_BLOCK = Proc.new {|*args|
name, start, ending, transaction_id, payload = args
SQL_BLOCK = proc { |*args|
_name, start, ending, transaction_id, payload = args
callsite = Utils.dev_callsite(caller)
payload.merge!(callsite) if callsite

Event.new(SQL_EVENT_NAME, start, ending, transaction_id, payload)
}

VIEW_BLOCK = Proc.new {|*args|
VIEW_BLOCK = proc { |*args|
name, start, ending, transaction_id, payload = args
payload[:identifier] = MetaRequest::Utils.sub_source_path(payload[:identifier])

Expand All @@ -56,34 +57,32 @@ class AppNotifications
# Subscribe to all events relevant to RailsPanel
#
def self.subscribe
new.
subscribe("meta_request.log").
subscribe("sql.active_record", &SQL_BLOCK).
subscribe("sql.sequel", &SQL_BLOCK).
subscribe("render_partial.action_view", &VIEW_BLOCK).
subscribe("render_template.action_view", &VIEW_BLOCK).
subscribe("process_action.action_controller.exception").
subscribe("process_action.action_controller") do |*args|
new
.subscribe('meta_request.log')
.subscribe('sql.active_record', &SQL_BLOCK)
.subscribe('sql.sequel', &SQL_BLOCK)
.subscribe('render_partial.action_view', &VIEW_BLOCK)
.subscribe('render_template.action_view', &VIEW_BLOCK)
.subscribe('process_action.action_controller.exception')
.subscribe('process_action.action_controller') do |*args|
name, start, ending, transaction_id, payload = args
payload[:status] = '500' if payload[:exception]
Event.new(name, start, ending, transaction_id, payload)
end.
subscribe("cache_read.active_support", &CACHE_BLOCK).
subscribe("cache_generate.active_support", &CACHE_BLOCK).
subscribe("cache_fetch_hit.active_support", &CACHE_BLOCK).
subscribe("cache_write.active_support", &CACHE_BLOCK).
subscribe("cache_delete.active_support", &CACHE_BLOCK).
subscribe("cache_exist?.active_support", &CACHE_BLOCK)
end
.subscribe('cache_read.active_support', &CACHE_BLOCK)
.subscribe('cache_generate.active_support', &CACHE_BLOCK)
.subscribe('cache_fetch_hit.active_support', &CACHE_BLOCK)
.subscribe('cache_write.active_support', &CACHE_BLOCK)
.subscribe('cache_delete.active_support', &CACHE_BLOCK)
.subscribe('cache_exist?.active_support', &CACHE_BLOCK)
end

def subscribe(event_name)
ActiveSupport::Notifications.subscribe(event_name) do |*args|
event = block_given?? yield(*args) : Event.new(*args)
event = block_given? ? yield(*args) : Event.new(*args)
AppRequest.current.events << event if AppRequest.current
end
self
end

end

end
3 changes: 2 additions & 1 deletion meta_request/lib/meta_request/app_request.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module MetaRequest
class AppRequest
attr_reader :id, :events
Expand All @@ -14,6 +16,5 @@ def self.current
def current!
Thread.current[:meta_request_id] = self
end

end
end
2 changes: 2 additions & 0 deletions meta_request/lib/meta_request/config.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module MetaRequest
class Config
attr_writer :logger, :storage_pool_size, :source_path
Expand Down
32 changes: 16 additions & 16 deletions meta_request/lib/meta_request/event.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# frozen_string_literal: true

require 'active_support'
require 'active_support/json'
require 'active_support/core_ext'

module MetaRequest

# Subclass of ActiveSupport Event that is JSON encodable
#
class Event < ActiveSupport::Notifications::Event
NOT_JSON_ENCODABLE = 'Not JSON Encodable'.freeze
NOT_JSON_ENCODABLE = 'Not JSON Encodable'

attr_reader :duration

Expand All @@ -27,46 +28,45 @@ def self.events_for_exception(exception_wrapper)
end
trace.unshift "#{exception.class} (#{exception.message})"
trace.map do |call|
Event.new('process_action.action_controller.exception', 0, 0, nil, {:call => call})
Event.new('process_action.action_controller.exception', 0, 0, nil, call: call)
end
end

private

def json_encodable(payload)
return {} unless payload.is_a?(Hash)
transform_hash(payload, :deep => true) { |hash, key, value|

transform_hash(payload, deep: true) do |hash, key, value|
if value.class.to_s == 'ActionDispatch::Http::Headers'
value = value.to_h.select { |k, _| k.upcase == k }
elsif defined?(ActiveRecord) && value.is_a?(ActiveRecord::ConnectionAdapters::AbstractAdapter)
value = NOT_JSON_ENCODABLE
end

begin
value.to_json(:methods => [:duration])
value.to_json(methods: [:duration])
new_value = value
rescue
rescue StandardError
new_value = NOT_JSON_ENCODABLE
end
hash[key] = new_value
}.with_indifferent_access
end.with_indifferent_access
end

# https://gist.github.com/dbenhur/1070399
def transform_hash(original, options={}, &block)
def transform_hash(original, options = {}, &block)
options[:safe_descent] ||= {}
new_hash = {}
options[:safe_descent][original.object_id] = new_hash
original.inject(new_hash) { |result, (key,value)|
if (options[:deep] && Hash === value)
value = options[:safe_descent].fetch( value.object_id ) {
original.each_with_object(new_hash) do |(key, value), result|
if options[:deep] && Hash === value
value = options[:safe_descent].fetch(value.object_id) do
transform_hash(value, options, &block)
}
end
end
block.call(result,key,value)
result
}
block.call(result, key, value)
end
end

end
end
Loading

0 comments on commit 53f3d5d

Please sign in to comment.