spring_onion 0.2.1 → 0.2.2

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: f81f2b07f2a586b8e02444106908d6d8eb8631d772db5bff80dd0fa6ee72b550
4
- data.tar.gz: 0e23da7945c119c5c5dcf90f2ead4985647536d0c7f3cb091b47e8f2c94ae168
3
+ metadata.gz: 54ea2a17b7ab30f6e646ed61b125dcc22e82cf304c946d26c70222a6e5a78341
4
+ data.tar.gz: af308c44d05e7b58048e5c29bd8f708a14d7067111210338ec52296ae9e00f16
5
5
  SHA512:
6
- metadata.gz: 115c03712806b8b97d9ff1e65f512db06e8ebc9f4e555b743d97c57b9159c58dc4f43410b7fb73db178a584425e22b403f6005ed3be6cb9a01c57b3295c686e3
7
- data.tar.gz: 4aa1951677c97b2fa2b45e2eeb3d49b51543e1632537c316a9d73d775b73b95472ced9074e06adc32d4c523bef5f069b9c4826c1f4bdf0a1e638e61d458dc2e1
6
+ metadata.gz: 1d5fcd164189c1a47126b009f0d058baffc306958cb2dcbd3bbf8e0f0bd4d54f49b9f862204b0d46bb4f68e44cd24257a2f62dced99f93b900d620f7871f2cc4
7
+ data.tar.gz: 0cae3be0dc5e774d092b3e2624bc943604dc8a0aac30dadf645a86f736f13f80978d0ec5a4a5bf595c7eff150f047f79a95769ab9d344bc15917d2ca4dd9b0c1
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- spring_onion (0.2.1)
4
+ spring_onion (0.2.2)
5
5
  activerecord
6
6
  coderay
7
7
  mysql2
@@ -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
@@ -60,9 +60,9 @@ module SpringOnion
60
60
  end
61
61
 
62
62
  @logger = Logger.new($stdout).tap do |logger|
63
- logger.formatter = lambda { |severity, datetime, _progname, msg|
63
+ logger.formatter = lambda do |severity, datetime, _progname, msg|
64
64
  "\n#{self}\t#{severity}\t#{datetime}\t#{msg}\n"
65
- }
65
+ end
66
66
  end
67
67
 
68
68
  @trace_len = 3
@@ -19,7 +19,7 @@ module SpringOnion
19
19
 
20
20
  exp = conn.query("EXPLAIN #{sql}", as: :hash).to_a
21
21
  exp.each { |r| r.delete('id') }
22
- _validate(exp, sql, trace)
22
+ _validate_explain(exp, sql, trace)
23
23
  end
24
24
  end
25
25
  rescue StandardError => e
@@ -29,35 +29,26 @@ module SpringOnion
29
29
  yield
30
30
  end
31
31
 
32
- def _validate(exp, sql, trace)
32
+ def _validate_explain(exp, sql, trace)
33
33
  warnings = SpringOnion.warnings
34
- warning_names_by_line = {}
34
+ warning_names_by_index = {}
35
35
 
36
36
  exp.each_with_index do |row, i|
37
37
  warning_names = warnings.select do |_name, validator|
38
38
  validator.call(row)
39
39
  end.keys
40
40
 
41
- warning_names_by_line["line #{i + 1}"] = warning_names unless warning_names.empty?
41
+ warning_names_by_index[i] = warning_names unless warning_names.empty?
42
42
  end
43
43
 
44
- return if warning_names_by_line.empty?
44
+ return if warning_names_by_index.empty?
45
45
 
46
- h = {
46
+ SpringOnion::JsonLogger.log(
47
47
  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)
48
+ explain: exp,
49
+ warnings: warning_names_by_index,
50
+ trace: trace
51
+ )
61
52
  end
62
53
  end
63
54
  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.1'
4
+ VERSION = '0.2.2'
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.1
4
+ version: 0.2.2
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