spring_onion 0.2.0 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 797e6837cbc8fb508afac284d54509ce175ac77346320bcc0697c0cd54b49a06
4
- data.tar.gz: 487f8a88353cce686f0b259cd83f2401cb3d599a4d05e403250018b2e3ae4fdc
3
+ metadata.gz: b40885dd3b876b902fa6670cde6f312362239a5d65da300297f9240cee05afbc
4
+ data.tar.gz: 3193d9ab4417169c380bb9fe1bf788752b4f347d9c0bdb34b5237a9f5e2c674f
5
5
  SHA512:
6
- metadata.gz: bc53955a16cd100e68ef306b798966f62937963ce8a3a67b819779379892fa50125b61c30e127cd1d1c3c0bd23cf58b9961a13a196430b15f55705b553ec9f0e
7
- data.tar.gz: d0c482e0864d25b05d3dc002de0b36d6b6b2d5414d8af7402bf86390f7a1182dc2d0cd745afb6cd8dcfe5d4c889141c351f4d3f6414acd957c4d2bd43322af41
6
+ metadata.gz: e1f37308e5b4dbc00112b4cc7e12a1060fe4c39a8dbb379bb5d10bf3c689151d590095a39e4f732c236993f19efccd8b624bd5baf7f162c3422646f49a377728
7
+ data.tar.gz: 73aafec72dad64c8e54daa129dd65bbd419f7dc013b94da830228825211a2e3693627185c5e9f50f02b4217f24a244feec7a324a14267c32f0e063c7149febb2
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- spring_onion (0.2.0)
4
+ spring_onion (0.2.5)
5
5
  activerecord
6
6
  coderay
7
7
  mysql2
data/README.md CHANGED
@@ -39,7 +39,7 @@ SpringOnion.source_filter_re = //
39
39
 
40
40
  class Employee < ActiveRecord::Base; end
41
41
 
42
- Employee.all.to_a.count
42
+ p Employee.all.to_a.count
43
43
  #=> SpringOnion INFO 2020-07-18 01:53:27 +0900 {"sql":"SELECT `employees`.* FROM `employees`","explain":[{"line":1,"select_type":"SIMPLE","table":"employees","partitions":null,"type":"ALL","possible_keys":null,"key":null,"key_len":null,"ref":null,"rows":298936,"filtered":100.0,"Extra":null}],"warnings":{"line 1":["slow_type"]},"backtrace":["/foo/bar/zoo/baz.rb:18:in `\u003ctop (required)\u003e'"]}
44
44
  #=> 300024
45
45
  ```
@@ -7,6 +7,7 @@ require 'active_support'
7
7
  require 'spring_onion/config'
8
8
  require 'spring_onion/error'
9
9
  require 'spring_onion/explainer'
10
+ require 'spring_onion/json_logger'
10
11
  require 'spring_onion/version'
11
12
 
12
13
  ActiveSupport.on_load :active_record do
@@ -42,12 +42,9 @@ module SpringOnion
42
42
  @ignore_sql_filter_re !~ sql && @sql_filter_re =~ sql
43
43
  end
44
44
 
45
- @source_filter_re = Regexp.union(
46
- [%r{/app/}].tap do |ary|
47
- re = ENV['SPRING_ONION_SOURCE_FILTER_RE']
48
- ary << Regexp.new(re) if re
49
- end
50
- )
45
+ @source_filter_re = ENV['SPRING_ONION_SOURCE_FILTER_RE'].yield_self do |re|
46
+ re ? Regexp.new(re) : %r{/app/}
47
+ end
51
48
 
52
49
  @ignore_source_filter_re = Regexp.union(
53
50
  [RbConfig::TOPDIR, *Gem.path, '/.rbenv/versions/'].tap do |ary|
@@ -63,9 +60,9 @@ module SpringOnion
63
60
  end
64
61
 
65
62
  @logger = Logger.new($stdout).tap do |logger|
66
- logger.formatter = lambda { |severity, datetime, _progname, msg|
63
+ logger.formatter = lambda do |severity, datetime, _progname, msg|
67
64
  "\n#{self}\t#{severity}\t#{datetime}\t#{msg}\n"
68
- }
65
+ end
69
66
  end
70
67
 
71
68
  @trace_len = 3
@@ -3,14 +3,16 @@
3
3
  module SpringOnion
4
4
  module Explainer
5
5
  def execute(*args)
6
- _with_explain(args.first) do
6
+ _with_explain(sql: args.first) do
7
7
  super
8
8
  end
9
9
  end
10
10
 
11
- def _with_explain(sql)
11
+ private
12
+
13
+ def _with_explain(sql:)
12
14
  begin
13
- if SpringOnion.enabled && sql =~ /^SELECT\b/ && SpringOnion.sql_filter.call(sql)
15
+ if SpringOnion.enabled && sql =~ /\A\s*SELECT\b/i && SpringOnion.sql_filter.call(sql)
14
16
  trace = SpringOnion.source_filter.call(caller)
15
17
 
16
18
  unless trace.length.zero?
@@ -19,7 +21,7 @@ module SpringOnion
19
21
 
20
22
  exp = conn.query("EXPLAIN #{sql}", as: :hash).to_a
21
23
  exp.each { |r| r.delete('id') }
22
- _validate(exp, sql, trace)
24
+ _validate_explain(sql: sql, exp: exp, trace: trace)
23
25
  end
24
26
  end
25
27
  rescue StandardError => e
@@ -29,35 +31,26 @@ module SpringOnion
29
31
  yield
30
32
  end
31
33
 
32
- def _validate(exp, sql, trace)
34
+ def _validate_explain(sql:, exp:, trace:)
33
35
  warnings = SpringOnion.warnings
34
- warning_names_by_line = {}
36
+ warning_names_by_index = {}
35
37
 
36
38
  exp.each_with_index do |row, i|
37
39
  warning_names = warnings.select do |_name, validator|
38
40
  validator.call(row)
39
41
  end.keys
40
42
 
41
- warning_names_by_line["line #{i + 1}"] = warning_names unless warning_names.empty?
43
+ warning_names_by_index[i] = warning_names unless warning_names.empty?
42
44
  end
43
45
 
44
- return if warning_names_by_line.empty?
46
+ return if warning_names_by_index.empty?
45
47
 
46
- h = {
48
+ SpringOnion::JsonLogger.log(
47
49
  sql: sql,
48
- explain: exp.each_with_index.map { |r, i| { line: i + 1 }.merge(r) },
49
- warnings: warning_names_by_line,
50
- backtrace: trace.slice(0, SpringOnion.trace_len),
51
- }
52
-
53
- line = if SpringOnion.json_pretty
54
- JSON.pretty_generate(h)
55
- else
56
- JSON.dump(h)
57
- end
58
-
59
- line = CodeRay.scan(line, :json).terminal if SpringOnion.color
60
- SpringOnion.logger.info(line)
50
+ explain: exp,
51
+ warnings: warning_names_by_index,
52
+ trace: trace
53
+ )
61
54
  end
62
55
  end
63
56
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SpringOnion
4
+ module JsonLogger
5
+ module_function
6
+
7
+ def log(sql:, explain:, warnings:, trace:)
8
+ h = {
9
+ sql: sql,
10
+ explain: explain.each_with_index.map { |r, i| { line: i + 1 }.merge(r) },
11
+ warnings: warnings.transform_keys { |i| "line #{i + 1}" },
12
+ backtrace: trace.slice(0, SpringOnion.trace_len),
13
+ }
14
+
15
+ line = if SpringOnion.json_pretty
16
+ JSON.pretty_generate(h)
17
+ else
18
+ JSON.dump(h)
19
+ end
20
+
21
+ line = CodeRay.scan(line, :json).terminal if SpringOnion.color
22
+ SpringOnion.logger.info(line)
23
+ end
24
+ end
25
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SpringOnion
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.5'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spring_onion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - winebarrel
@@ -142,6 +142,7 @@ files:
142
142
  - lib/spring_onion/config.rb
143
143
  - lib/spring_onion/error.rb
144
144
  - lib/spring_onion/explainer.rb
145
+ - lib/spring_onion/json_logger.rb
145
146
  - lib/spring_onion/version.rb
146
147
  - spring_onion.gemspec
147
148
  homepage: https://github.com/winebarrel/spring_onion