model_observer 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b200ec400039b569da0a1d685d37f0d72fb563e1
4
- data.tar.gz: 3040c514b35539fe11a212b57da89116ca6f1597
3
+ metadata.gz: 4cefc88cdbb1e00c4b1550cb9f45edcc6b0fd078
4
+ data.tar.gz: a0798211619936ad40912908f92ba157423c0695
5
5
  SHA512:
6
- metadata.gz: e255c6498d986abd55298e9750396b5918f02de3518fa602a001c5167786d332145f495d771e2cb348195ea0af49eab8ffbb256971a522a1015819cfb1541cbb
7
- data.tar.gz: 919a712014c8a795bb7f45336e01e8fd59f5f70909d8be09f39cc2b5e90d8cad3f679e87a5841a3168e749d48e2159fa3897785950d742f1d5b9c4d93e082c14
6
+ metadata.gz: 96c0cf861d126fb6be493c7ee17cc4dc9090775187cb7d49c01bbd34ff77a310b8a81b6038b3af8453b78075ad66685c4a6c4994b6df25d53f6fa99d3e69a6f0
7
+ data.tar.gz: 3182a0ed8c4cb77aadddeaafb5a971eb2e22f6a39123c6439ea083446de315dee9f5ae44e967e06af73880d63e53a01b8f4888ad63e8add7c3dc54d06312f3da
data/README.md CHANGED
@@ -16,18 +16,29 @@ and bundle
16
16
 
17
17
  bundle
18
18
 
19
- ## Log
19
+ ## Query Log
20
20
 
21
- The ModelObserver log will look like:
21
+ The time include instantiate models will be logged after each sql log like this:
22
+
23
+ Author Load (0.5ms) SELECT `authors`.* FROM `authors` WHERE `authors`.`id` = 1 LIMIT 1
24
+ Author Instantiate (1.5ms) SELECT `authors`.* FROM `authors` WHERE `authors`.`id` = 1 LIMIT 1
25
+
26
+ 1.5ms == 0.5ms(DB query) + 1.0ms(model instantiation)
27
+
28
+ ## Summary Log
29
+
30
+ The summary of each request will be added to the end like this:
22
31
 
23
32
  ===== Model Observer Start =====
24
- Author: 1 sum(10ms) avg(10ms)
25
- Book: 36 sum(13ms) avg(0ms)
26
- id(20): 2
27
- id(50): 9
28
- id(61): 4
29
- id(68): 2
30
- id(111): 2
31
- id(123): 3
32
- id(124): 3
33
- ===== Model Observer End =====
33
+ Author: 1 sum(10.6ms) avg(10.6ms)
34
+ Book: 27 sum(25.7ms) avg(1.0ms)
35
+ id(319): 3
36
+ id(377): 3
37
+ id(487): 3
38
+ id(489): 3
39
+ id(493): 3
40
+ id(496): 3
41
+ id(499): 3
42
+ id(507): 3
43
+ id(536): 3
44
+ ===== Model Observer End =======
@@ -2,6 +2,7 @@ module ModelObserver
2
2
  module ActiveRecord
3
3
  def self.enable
4
4
  require 'active_record'
5
+
5
6
  ::ActiveRecord::Inheritance::ClassMethods.class_eval do
6
7
  alias_method :origin_instantiate, :instantiate
7
8
 
@@ -12,6 +13,17 @@ module ModelObserver
12
13
  instance
13
14
  end
14
15
  end
16
+
17
+ ::ActiveRecord::Querying.class_eval do
18
+ alias_method :origin_find_by_sql, :find_by_sql
19
+
20
+ def find_by_sql(sql, binds = [])
21
+ ::ActiveSupport::Notifications.instrumenter.instrument(
22
+ "instantiate.model_observer",
23
+ :sql => connection.to_sql(sanitize_sql(sql), binds),
24
+ :name => "#{name} Instantiate") { origin_find_by_sql(sql, binds) }
25
+ end
26
+ end
15
27
  end
16
28
  end
17
29
  end
@@ -2,6 +2,7 @@ module ModelObserver
2
2
  module ActiveRecord
3
3
  def self.enable
4
4
  require 'active_record'
5
+
5
6
  ::ActiveRecord::Persistence::ClassMethods.class_eval do
6
7
  alias_method :origin_instantiate, :instantiate
7
8
 
@@ -12,6 +13,17 @@ module ModelObserver
12
13
  instance
13
14
  end
14
15
  end
16
+
17
+ ::ActiveRecord::Querying.class_eval do
18
+ alias_method :origin_find_by_sql, :find_by_sql
19
+
20
+ def find_by_sql(sql, binds = [])
21
+ ::ActiveSupport::Notifications.instrumenter.instrument(
22
+ "instantiate.model_observer",
23
+ :sql => connection.to_sql(sanitize_sql(sql), binds),
24
+ :name => "#{name} Instantiate") { origin_find_by_sql(sql, binds) }
25
+ end
26
+ end
15
27
  end
16
28
  end
17
29
  end
@@ -0,0 +1,19 @@
1
+ require 'active_record/log_subscriber'
2
+
3
+ module ModelObserver
4
+ class LogSubscriber < ::ActiveRecord::LogSubscriber
5
+ def instantiate(event)
6
+ return unless logger.debug?
7
+
8
+ payload = event.payload
9
+ name = '%s (%.1fms)' % [payload[:name], event.duration]
10
+ sql = payload[:sql].squeeze(' ')
11
+
12
+ name = color(name, YELLOW, true)
13
+ sql = color(sql, nil, true)
14
+ debug " #{name} #{sql}"
15
+ end
16
+ end
17
+ end
18
+
19
+ ModelObserver::LogSubscriber.attach_to :model_observer
@@ -0,0 +1,14 @@
1
+ module ModelObserver
2
+ module Querying
3
+ ::ActiveRecord::Querying.class_eval do
4
+ alias_method :origin_find_by_sql, :find_by_sql
5
+
6
+ def find_by_sql(sql, binds = [])
7
+ ::ActiveSupport::Notifications.instrumenter.instrument(
8
+ "instantiate.active_record",
9
+ :sql => sql,
10
+ :name => "#{name} Load") { origin_find_by_sql }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module ModelObserver
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -19,6 +19,7 @@ module ModelObserver
19
19
  else
20
20
  raise "Current version of active_record is not supported."
21
21
  end
22
+ require 'model_observer/log_subscriber'
22
23
  end
23
24
 
24
25
  class << self
@@ -32,7 +33,7 @@ module ModelObserver
32
33
  def write_to_rails_log
33
34
  Rails.logger.warn "===== Model Observer Start ====="
34
35
  Rails.logger.warn Analyser.result
35
- Rails.logger.warn "===== Model Observer End ====="
36
+ Rails.logger.warn "===== Model Observer End ======="
36
37
  end
37
38
  end
38
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_observer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weihu Chen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-31 00:00:00.000000000 Z
11
+ date: 2013-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3
@@ -51,7 +51,9 @@ files:
51
51
  - lib/model_observer/analyser.rb
52
52
  - lib/model_observer/collector.rb
53
53
  - lib/model_observer/dependency.rb
54
+ - lib/model_observer/log_subscriber.rb
54
55
  - lib/model_observer/metric.rb
56
+ - lib/model_observer/querying.rb
55
57
  - lib/model_observer/rack.rb
56
58
  - lib/model_observer/railtie.rb
57
59
  - lib/model_observer/version.rb