rack-webprofiler 0.1.0.pre.beta1 → 0.1.0.pre.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +8 -8
  2. data/README.md +7 -17
  3. data/lib/rack/templates/assets/css/profiler.css +1 -1
  4. data/lib/rack/templates/assets/css/rwpt.css +1 -1
  5. data/lib/rack/templates/assets/sass/_highlight.scss +63 -0
  6. data/lib/rack/templates/assets/sass/_variables.scss +1 -0
  7. data/lib/rack/templates/assets/sass/profiler.scss +1 -1
  8. data/lib/rack/templates/assets/sass/rwpt.scss +27 -9
  9. data/lib/rack/templates/async.erb +8 -8
  10. data/lib/rack/templates/panel/_sidebar.erb +3 -23
  11. data/lib/rack/templates/panel/index.erb +18 -6
  12. data/lib/rack/templates/panel/layout.erb +9 -5
  13. data/lib/rack/templates/panel/show.erb +25 -7
  14. data/lib/rack/templates/profiler.erb +15 -12
  15. data/lib/rack/web_profiler/collector/rack/request_collector.rb +38 -35
  16. data/lib/rack/web_profiler/collector/ruby_collector.rb +9 -7
  17. data/lib/rack/web_profiler/collector/time_collector.rb +2 -2
  18. data/lib/rack/web_profiler/collector/view.rb +44 -0
  19. data/lib/rack/web_profiler/collector.rb +29 -16
  20. data/lib/rack/web_profiler/config.rb +0 -10
  21. data/lib/rack/web_profiler/controller.rb +69 -64
  22. data/lib/rack/web_profiler/engine.rb +15 -6
  23. data/lib/rack/web_profiler/model/collection_record.rb +1 -0
  24. data/lib/rack/web_profiler/request.rb +5 -1
  25. data/lib/rack/web_profiler/router.rb +1 -1
  26. data/lib/rack/web_profiler/version.rb +1 -1
  27. data/lib/rack/web_profiler/view.rb +236 -0
  28. data/lib/rack/web_profiler.rb +34 -17
  29. data/rack-webprofiler.gemspec +1 -0
  30. metadata +19 -10
  31. data/lib/rack/web_profiler/auto_configure/rails.rb +0 -12
  32. data/lib/rack/web_profiler/collector/rails/active_record_collector.rb +0 -25
  33. data/lib/rack/web_profiler/collector/rails/logger_collector.rb +0 -22
  34. data/lib/rack/web_profiler/collector/rails/rails_collector.rb +0 -25
  35. data/lib/rack/web_profiler/collector/rails/request_collector.rb +0 -50
  36. data/lib/rack/web_profiler/collector/sinatra/request_collector.rb +0 -216
  37. data/lib/rack/web_profiler/collector/sinatra/sinatra_collector.rb +0 -25
  38. data/lib/rack/web_profiler/erb.rb +0 -9
@@ -56,7 +56,7 @@ module Rack
56
56
  def serve_asset(path)
57
57
  rf = Rack::File.new(::File.expand_path("../../templates/assets/", __FILE__))
58
58
  request = @request.dup
59
- request.env[PATH_INFO] = path
59
+ request.env[PATH_INFO] = "/#{path}"
60
60
 
61
61
  path_info = Utils.unescape(request.env[PATH_INFO])
62
62
  clean_path_info = Utils.clean_path_info(path_info)
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class WebProfiler
3
- VERSION = "0.1.0-beta1".freeze
3
+ VERSION = "0.1.0-beta2".freeze
4
4
  end
5
5
  end
@@ -0,0 +1,236 @@
1
+ require "erb"
2
+ require "rouge"
3
+
4
+ module Rack
5
+ class WebProfiler
6
+ # View
7
+ class View
8
+ def initialize(template, layout: nil, context: nil)
9
+ @template = template
10
+ @layout = layout
11
+ @context = context
12
+ end
13
+
14
+ def result(variables = {})
15
+ unless @template.nil?
16
+ templates = [read_template(@template)]
17
+ templates << read_template(@layout) unless @layout.nil?
18
+
19
+ content = templates.inject(nil) do |prev, temp|
20
+ render(temp, variables) { prev }
21
+ end
22
+ end
23
+ end
24
+
25
+ def context
26
+ @context ||= Context.new
27
+ end
28
+
29
+ private
30
+
31
+ def read_template(template)
32
+ unless template.empty?
33
+ path = ::File.expand_path("../../templates/#{template}", __FILE__)
34
+ return ::File.read(path) if ::File.exist?(path)
35
+ end
36
+ template
37
+ end
38
+
39
+ def options
40
+ @options ||= {
41
+ :safe_level => nil,
42
+ :trim_mode => '%-',
43
+ :eoutvar => '@_erbout',
44
+ }
45
+ end
46
+
47
+ def render(str, variables = {})
48
+ opts = options
49
+
50
+ format_variables(variables).each do |name, value|
51
+ context.instance_variable_set("@#{name}", value)
52
+ end
53
+
54
+ context.instance_eval do
55
+ erb = ::ERB.new(str, *opts.values_at(:safe_level, :trim_mode, :eoutvar))
56
+ erb.result(binding).sub(/\A\n/, '')
57
+ end
58
+ # @todo better error when there is an ERB error.
59
+ end
60
+
61
+ def format_variables(v)
62
+ case v
63
+ when Binding
64
+ h = {}
65
+ v.eval("instance_variables").each do |k|
66
+ h[k.to_s.sub(/^@/, '')] = v.eval("instance_variable_get(:#{k})")
67
+ end
68
+ h
69
+ when Hash
70
+ v
71
+ else
72
+ {}
73
+ end
74
+ end
75
+
76
+ # CommonHelpers.
77
+ module CommonHelpers
78
+ def content_for(key, content = nil, &block)
79
+ block ||= proc { |*| content }
80
+ content_blocks[key.to_sym] << capture_later(&block)
81
+ end
82
+
83
+ def content_for?(key)
84
+ content_blocks[key.to_sym].any?
85
+ end
86
+
87
+ def yield_content(key, default = nil)
88
+ return default if content_blocks[key.to_sym].empty?
89
+ content_blocks[key.to_sym].map { |b| capture(&b) }.join
90
+ end
91
+
92
+ #
93
+ def partial(path, variables: nil)
94
+ return "" if path.nil?
95
+
96
+ variables ||= binding if variables.nil?
97
+
98
+ capture do
99
+ WebProfiler::View.new(path, context: self).result(variables)
100
+ end
101
+ end
102
+
103
+ #
104
+ def h(obj)
105
+ case obj
106
+ when String
107
+ ::ERB::Util.html_escape(obj)
108
+ else
109
+ ::ERB::Util.html_escape(obj.inspect)
110
+ end
111
+ end
112
+
113
+ #
114
+ def highlight(code: nil, language: nil)
115
+ language = language.to_sym if language.is_a? String
116
+
117
+ case language
118
+ when :ruby
119
+ lexer = Rouge::Lexers::Ruby.new
120
+ when :json
121
+ lexer = Rouge::Lexers::Jsonnet.new
122
+ when :xml
123
+ lexer = Rouge::Lexers::XML.new
124
+ else
125
+ lexer = Rouge::Lexers::PlainText.new
126
+ end
127
+
128
+ code = capture(&Proc.new) if block_given?
129
+
130
+ formatter = Rouge::Formatters::HTML.new
131
+ formatter = Rouge::Formatters::HTMLPygments.new(formatter, css_class='highlight')
132
+
133
+ "<div class=\"highlight\">#{formatter.format(lexer.lex(code))}</div>"
134
+ end
135
+
136
+ def capture(&block)
137
+ @capture = nil
138
+ @_erbout, _buf_was = '', @_erbout
139
+ result = yield
140
+ @_erbout = _buf_was
141
+ result.strip.empty? && @capture ? @capture : result
142
+ end
143
+
144
+ private
145
+
146
+ def capture_later(&block)
147
+ proc { |*| @capture = capture(&block) }
148
+ end
149
+
150
+ def content_blocks
151
+ @content_blocks ||= Hash.new {|h,k| h[k] = [] }
152
+ end
153
+ end
154
+
155
+ # CollectorHelpers.
156
+ module CollectorHelpers
157
+
158
+ #
159
+ def collector_status(collector, collection)
160
+ collector_data_storage(collector, collection, :status)
161
+ end
162
+
163
+ #
164
+ def collector_datas(collector, collection)
165
+ collector_data_storage(collector, collection, :datas)
166
+ end
167
+
168
+ def collector_tab(collector, collection)
169
+ return nil unless is_collection_contains_datas_for_collector?(collection, collector)
170
+
171
+ c = collector_view_context(collector, collection)
172
+ c.tab_content
173
+ end
174
+
175
+ def collector_panel(collector, collection)
176
+ return nil unless is_collection_contains_datas_for_collector?(collection, collector)
177
+
178
+ c = collector_view_context(collector, collection)
179
+ c.panel_content
180
+ end
181
+
182
+ def collector_has_tab?(collector, collection)
183
+ collector_data_storage(collector, collection, :show_tab)
184
+ end
185
+
186
+ def collector_has_panel?(collector, collection)
187
+ collector_data_storage(collector, collection, :show_panel)
188
+ end
189
+
190
+ private
191
+
192
+ def collector_view_context(collector, collection)
193
+ collectors_view_context[collector.name] ||= begin
194
+ v = WebProfiler::Collector::View.new(collector.template)
195
+ v.result(collector: collector, collection: collection)
196
+ v.context
197
+ end
198
+ end
199
+
200
+ def collector_data_storage(collector, collection, key = nil)
201
+ return nil unless is_collection_contains_datas_for_collector?(collection, collector)
202
+
203
+ storage = collection.datas[collector.name.to_sym]
204
+ storage[key] if !key.nil? && storage.has_key?(key)
205
+ end
206
+
207
+ def is_valid_collector?(collector)
208
+ !collector.nil? \
209
+ && collector.kind_of?(WebProfiler::Collector::Definition)
210
+ end
211
+
212
+ def is_valid_collection?(collection)
213
+ !collection.nil? \
214
+ && collection.kind_of?(WebProfiler::Model::CollectionRecord)
215
+ end
216
+
217
+ def is_collection_contains_datas_for_collector?(collection, collector)
218
+ is_valid_collector?(collector) \
219
+ && is_valid_collection?(collection) \
220
+ && collection.datas.has_key?(collector.name.to_sym)
221
+ end
222
+
223
+ private
224
+
225
+ def collectors_view_context
226
+ @collectors_view_context ||= {}
227
+ end
228
+ end
229
+
230
+ class Context
231
+ include CommonHelpers
232
+ include CollectorHelpers
233
+ end
234
+ end
235
+ end
236
+ end
@@ -12,10 +12,7 @@ module Rack
12
12
  autoload :Model, "rack/web_profiler/model"
13
13
  autoload :Request, "rack/web_profiler/request"
14
14
  autoload :Router, "rack/web_profiler/router"
15
-
16
- module AutoConfigure
17
- autoload :Rails, "rack/web_profiler/auto_configure/rails"
18
- end
15
+ autoload :View, "rack/web_profiler/view"
19
16
 
20
17
  class << self
21
18
  def config
@@ -38,8 +35,9 @@ module Rack
38
35
  # @param app [Proc]
39
36
  def initialize(app, tmp_dir: nil)
40
37
  @app = app
41
- # WebProfiler.config(&Proc.new) if block_given?
38
+
42
39
  WebProfiler.config.tmp_dir = tmp_dir unless tmp_dir.nil?
40
+ WebProfiler.config(&Proc.new) if block_given?
43
41
  end
44
42
 
45
43
  # Call
@@ -48,23 +46,42 @@ module Rack
48
46
  #
49
47
  # @return [Array]
50
48
  def call(env)
51
- request = WebProfiler::Request.new(env)
52
- request.start_runtime!
49
+ begin
50
+ request = WebProfiler::Request.new(env)
51
+ request.start_runtime!
53
52
 
54
- response = WebProfiler::Router.response_for(request)
55
- return response.finish if response.is_a? Rack::Response
53
+ response = WebProfiler::Router.response_for(request)
54
+ return response.finish if response.is_a? Rack::Response
56
55
 
57
- # begin
58
- status, headers, body = @app.call(env)
59
- # rescue Exception => e
60
- # raise e
61
- # end
56
+ status, headers, body = @app.call(env)
57
+ rescue => e
58
+ process(request, body, status, headers, e)
59
+ raise
60
+ end
61
+
62
+ process(request, body, status, headers)
63
+ end
62
64
 
65
+ private
66
+
67
+ # Process the request.
68
+ #
69
+ # @param request [Rack::WebProfiler::Request]
70
+ # @param body
71
+ # @param status [Integer]
72
+ # @param headers [Hash]
73
+ # @param exception [Exception, nil]
74
+ #
75
+ # @return [Rack::Response]
76
+ def process(request, body, status, headers, exception = nil)
63
77
  request.save_runtime!
64
78
 
65
- WebProfiler::Engine.process(request, body, status, headers).finish
79
+ unless exception.nil?
80
+ request.save_exception(exception)
81
+ WebProfiler::Engine.process_exception(request).finish
82
+ else
83
+ WebProfiler::Engine.process(request, body, status, headers).finish
84
+ end
66
85
  end
67
86
  end
68
87
  end
69
-
70
- require "rack/web_profiler/auto_configure/rails" if defined? Rails
@@ -36,6 +36,7 @@ Gem::Specification.new do |spec|
36
36
  spec.add_dependency "docile", "~> 1.1"
37
37
  spec.add_dependency "sequel", "~> 4"
38
38
  spec.add_dependency "sqlite3", "~> 1.3"
39
+ spec.add_dependency "rouge", "~> 2.0"
39
40
 
40
41
  spec.add_development_dependency "bundler", "~> 1.11"
41
42
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-webprofiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre.beta1
4
+ version: 0.1.0.pre.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Brousse
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-19 00:00:00.000000000 Z
11
+ date: 2016-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rouge
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: bundler
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -125,6 +139,7 @@ files:
125
139
  - lib/rack/templates/assets/js/rwpt.js
126
140
  - lib/rack/templates/assets/js/rwpt.min.js
127
141
  - lib/rack/templates/assets/sass/_fonts.scss
142
+ - lib/rack/templates/assets/sass/_highlight.scss
128
143
  - lib/rack/templates/assets/sass/_normalize.scss
129
144
  - lib/rack/templates/assets/sass/_variables.scss
130
145
  - lib/rack/templates/assets/sass/profiler.scss
@@ -136,31 +151,25 @@ files:
136
151
  - lib/rack/templates/panel/show.erb
137
152
  - lib/rack/templates/profiler.erb
138
153
  - lib/rack/web_profiler.rb
139
- - lib/rack/web_profiler/auto_configure/rails.rb
140
154
  - lib/rack/web_profiler/collector.rb
141
155
  - lib/rack/web_profiler/collector/debug_collector.rb
142
156
  - lib/rack/web_profiler/collector/erb_collector.rb
143
157
  - lib/rack/web_profiler/collector/performance_collector.rb
144
158
  - lib/rack/web_profiler/collector/rack/rack_collector.rb
145
159
  - lib/rack/web_profiler/collector/rack/request_collector.rb
146
- - lib/rack/web_profiler/collector/rails/active_record_collector.rb
147
- - lib/rack/web_profiler/collector/rails/logger_collector.rb
148
- - lib/rack/web_profiler/collector/rails/rails_collector.rb
149
- - lib/rack/web_profiler/collector/rails/request_collector.rb
150
160
  - lib/rack/web_profiler/collector/ruby_collector.rb
151
- - lib/rack/web_profiler/collector/sinatra/request_collector.rb
152
- - lib/rack/web_profiler/collector/sinatra/sinatra_collector.rb
153
161
  - lib/rack/web_profiler/collector/time_collector.rb
162
+ - lib/rack/web_profiler/collector/view.rb
154
163
  - lib/rack/web_profiler/collectors.rb
155
164
  - lib/rack/web_profiler/config.rb
156
165
  - lib/rack/web_profiler/controller.rb
157
166
  - lib/rack/web_profiler/engine.rb
158
- - lib/rack/web_profiler/erb.rb
159
167
  - lib/rack/web_profiler/model.rb
160
168
  - lib/rack/web_profiler/model/collection_record.rb
161
169
  - lib/rack/web_profiler/request.rb
162
170
  - lib/rack/web_profiler/router.rb
163
171
  - lib/rack/web_profiler/version.rb
172
+ - lib/rack/web_profiler/view.rb
164
173
  - lib/rack/webprofiler.rb
165
174
  - rack-webprofiler.gemspec
166
175
  homepage: http://github.com/nicolas-brousse/rack-webprofiler
@@ -1,12 +0,0 @@
1
- module Rack
2
- # AutoConfigure::Rails
3
- class WebProfiler::AutoConfigure::Rails
4
- class Engine < ::Rails::Engine # :nodoc:
5
- initializer "rack-web_profiler.configure_middleware" do |app|
6
- app.middleware.use Rack::WebProfiler do |c|
7
- c.tmp_dir = ::File.expand_path(::File.join(Rails.root, "tmp"), __FILE__)
8
- end
9
- end
10
- end
11
- end
12
- end
@@ -1,25 +0,0 @@
1
- module Rack
2
- class WebProfiler::Collector::Rails::ActiveRecordCollector
3
- include Rack::WebProfiler::Collector::DSL
4
-
5
- icon nil
6
-
7
- collector_name "rails_activerecord"
8
- position 1
9
-
10
- collect do |_request, _response|
11
- store :sql_requests, []
12
- end
13
-
14
- template __FILE__, type: :DATA
15
-
16
- is_enabled? -> { defined? ActiveRecord }
17
- end
18
- end
19
-
20
- # See: https://github.com/noahd1/oink/blob/master/lib/oink/middleware.rb#L46
21
-
22
- __END__
23
- <%# content_for :tab do %>
24
-
25
- <%# end %>
@@ -1,22 +0,0 @@
1
- module Rack
2
- class WebProfiler::Collector::Rails::LoggerCollector
3
- include Rack::WebProfiler::Collector::DSL
4
-
5
- icon nil
6
-
7
- collector_name "rails_logger"
8
- position 1
9
-
10
- collect do |_request, _response|
11
- end
12
-
13
- template __FILE__, type: :DATA
14
-
15
- is_enabled? -> { defined? Rails }
16
- end
17
- end
18
-
19
- __END__
20
- <%# content_for :tab do %>
21
-
22
- <%# end %>
@@ -1,25 +0,0 @@
1
- module Rack
2
- class WebProfiler::Collector::Rails::RailsCollector
3
- include Rack::WebProfiler::Collector::DSL
4
-
5
- icon nil
6
-
7
- collector_name "rails"
8
- position 1
9
-
10
- collect do |_request, _response|
11
- store :rails_version, Rails.version
12
- store :rails_env, Rails.env
13
- store :rails_doc_url, "http://api.rubyonrails.org/v#{Rails.version}/"
14
- end
15
-
16
- template __FILE__, type: :DATA
17
-
18
- is_enabled? -> { defined? Rails }
19
- end
20
- end
21
-
22
- __END__
23
- <%# content_for :tab do %>
24
- <%= data[:rails_version] %> | <%= data[:rails_env] %>
25
- <%# end %>
@@ -1,50 +0,0 @@
1
- module Rack
2
- class WebProfiler::Collector::Rails::RequestCollector
3
- include Rack::WebProfiler::Collector::DSL
4
-
5
- icon nil
6
-
7
- collector_name "rails_request"
8
- position 1
9
-
10
- collect do |request, response|
11
- route, _matches, request_params = find_route(request)
12
-
13
- store :request_path, request.path
14
- store :request_method, request.request_method
15
- store :request_params, request_params || {}
16
- store :request_cookies, request.cookies
17
- store :request_get, request.GET
18
- store :request_post, request.POST
19
- # store :rack_env, request.env.each { |k, v| v.to_s }
20
- # puts request.env.map{ |k, v| k => v.to_s }
21
- store :response_status, response.status
22
- store :route_name, route.nil? ? nil : route.name
23
-
24
- if response.successful?
25
- status :success
26
- elsif response.redirection?
27
- status :warning
28
- else
29
- status :error
30
- end
31
- end
32
-
33
- template __FILE__, type: :DATA
34
-
35
- is_enabled? -> { defined? Rails }
36
-
37
- class << self
38
- def find_route(request)
39
- Rails.application.routes.router.recognize(request) do |route, matches, params|
40
- return [route, matches, params]
41
- end
42
- end
43
- end
44
- end
45
- end
46
-
47
- __END__
48
- <%# content_for :tab do %>
49
- <%= data[:response_status] %> | <%= data[:request_method] %> <%= data[:request_path] %>
50
- <%# end %>