log_magic 0.0.1 → 0.0.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
  SHA1:
3
- metadata.gz: 142e9bf7993790706efd5abd39ee6d982caf9d68
4
- data.tar.gz: 47141e7128a5fb074b484d299536f304a043513e
3
+ metadata.gz: 9954218d1b500263e589e5db673d289a871ef149
4
+ data.tar.gz: 1befbd958b7416c9dcfce6c7406900eebd62cd40
5
5
  SHA512:
6
- metadata.gz: d8cc18c555aae5aa378b51f36a69f70bf250af9b832f707158dd6d3befb4ac75751823e5cd26ce1ec97974e8bcdd5ca64ae01250669b0fc646d2a6153936937f
7
- data.tar.gz: 75344a1374e3f47403f12ff70ee4d4f2d18254428bb8690b95fcbcf0beb0d0da30ffdf5d5803d9fd615283da1aa7b3cd545a3ac16085c7e83f9161f2276946f7
6
+ metadata.gz: 61daa1cfd8b646a67ce3aa9d42e841b52b664689dfa1f9bc466367109e9e69b7e1e059dd1b781682c58b89e6f70f31f36e76d3eab41250422b0308698853b10e
7
+ data.tar.gz: a9ca50d9b56b0cd28280938174de0195e4abf45bed0fda07578785745a9fa7bd8e977f1c43207ca59382b5925ff0c5e85173ac4f0de5c33b8f360c8ab9a20ddc
data/bin/log_magic CHANGED
@@ -2,7 +2,17 @@
2
2
 
3
3
  require 'log_magic'
4
4
 
5
- puts ARGV[0]
6
- log_magic = LogMagic.new(ARGV[0])
7
- log_magic.initialize_listener
8
- log_magic.start
5
+ begin
6
+ log_magic = LogMagic.new(ARGV[0])
7
+
8
+ server = fork do
9
+ explainer_server = LogMagic::SearchkickExplainer.new
10
+ explainer_server.start
11
+ end
12
+ Process.detach(server)
13
+
14
+ log_magic.initialize_listener
15
+ log_magic.start
16
+ ensure
17
+ `kill -9 #{server}` unless server.nil?
18
+ end
data/bin/rebuild CHANGED
@@ -1,2 +1,2 @@
1
1
  gem build log_magic.gemspec
2
- gem install log_magic-0.0.0.gem
2
+ gem install log_magic-0.0.1.gem
@@ -0,0 +1,4 @@
1
+ #h2 Query
2
+
3
+ %p
4
+ = pretty_print_json(query_json)
@@ -0,0 +1,40 @@
1
+ require 'rack'
2
+ require 'byebug'
3
+ require 'haml'
4
+
5
+ class LogMagic::SearchkickExplainer
6
+ include ::LogMagic::JSONUtils
7
+
8
+ attr_accessor :database_name
9
+
10
+ def initialize()
11
+ @persistance_layer = ::LogMagic::PersistanceLayer.new
12
+ end
13
+
14
+ def query_json
15
+ @persistance_layer.retrieve_value(@uuid)
16
+ end
17
+
18
+ def start
19
+ app = Proc.new do |env|
20
+ @uuid = env['PATH_INFO'][1..-1]
21
+ body = rendered_template
22
+
23
+ ['200', {'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s}, [body]]
24
+ end
25
+
26
+ Rack::Handler::WEBrick.run app, Port: 7467,
27
+ Logger: WEBrick::Log::new('/dev/null')
28
+ end
29
+
30
+ def rendered_template
31
+ engine = Haml::Engine.new(File.read(template_path))
32
+ engine.render(self)
33
+ end
34
+
35
+ def template_path
36
+ File.expand_path(
37
+ File.join(__FILE__, '..', '..', 'explainer_templates', 'searchkick.haml')
38
+ )
39
+ end
40
+ end
@@ -0,0 +1,21 @@
1
+ require 'redis'
2
+
3
+ class LogMagic::PersistanceLayer
4
+ attr_accessor :db
5
+
6
+ def initialize
7
+ @redis = Redis.new
8
+ end
9
+
10
+ def add_value(value)
11
+ uuid = SecureRandom.uuid
12
+
13
+ @redis.set(uuid, value)
14
+
15
+ return uuid
16
+ end
17
+
18
+ def retrieve_value(uuid)
19
+ @redis.get(uuid)
20
+ end
21
+ end
@@ -1,22 +1,16 @@
1
1
  require 'json'
2
2
 
3
3
  class LogMagic::SearchkickPrinter
4
- include ::LogMagic::JSONUtils
5
-
6
- def initialize(line)
7
- @line = line
8
- end
9
-
10
- def extract_query_json
11
- @line.match(/'.*'/)[0][1..-2]
4
+ def initialize(query_json, uuid)
5
+ @query_json = query_json
6
+ @uuid = uuid
12
7
  end
13
8
 
14
9
  def print
15
10
  """
16
- ElasticSearch query generated by Searchkick
17
- <------------------------------------------
18
- #{pretty_print_json(extract_query_json)}
19
- ------------------------------------------>
11
+ ElasticSearch query generated by Searchkick explained
12
+ Copy and view in browser: http://localhost:7467/#{@uuid}
13
+ <------------------------------------------>
20
14
  """
21
15
  end
22
16
  end
@@ -9,8 +9,22 @@ class LogMagic::SearchkickRefiner
9
9
  lines.last =~ /Search[^c]*curl/
10
10
  end
11
11
 
12
- def output
13
- searchkick_printer = ::LogMagic::SearchkickPrinter.new(lines.last)
12
+ def compute(persistance_layer)
13
+ query_json = extract_query_json
14
+ uuid = persist(persistance_layer, query_json)
15
+ output(query_json, uuid)
16
+ end
17
+
18
+ def output(query_json, uuid)
19
+ searchkick_printer = ::LogMagic::SearchkickPrinter.new(query_json, uuid)
14
20
  searchkick_printer.print
15
21
  end
22
+
23
+ def extract_query_json
24
+ @lines.last.match(/'.*'/)[0][1..-2]
25
+ end
26
+
27
+ def persist(persistance_layer, query_json)
28
+ persistance_layer.add_value(query_json)
29
+ end
16
30
  end
@@ -1,6 +1,8 @@
1
1
  module LogMagic::JSONUtils
2
2
  def pretty_print_json(json)
3
3
  JSON.pretty_generate(parse_json(json))
4
+ .gsub("\n", '<br>')
5
+ .gsub(" ", '&nbsp;')
4
6
  end
5
7
 
6
8
  def parse_json(json)
data/lib/log_magic.rb CHANGED
@@ -1,12 +1,19 @@
1
1
  require 'file-tail'
2
2
 
3
3
  class LogMagic
4
+ attr_reader :persistance_layer
5
+
4
6
  def initialize(log_file_name)
5
7
  @log_file_name = log_file_name
6
8
  @refiners = []
9
+ @persistance_layer = PersistanceLayer.new
7
10
  add_searchkick_refiner
8
11
  end
9
12
 
13
+ def database_name
14
+ @persistance_layer.database_name
15
+ end
16
+
10
17
  def initialize_listener
11
18
  @log_listener = LogListener.new(@log_file_name)
12
19
  end
@@ -14,8 +21,8 @@ class LogMagic
14
21
  def start
15
22
  @log_listener.listen do |line|
16
23
  add_line_to_refiners(line)
17
- check_for_refiner_output do |output|
18
- puts output
24
+ check_for_refiner_match do |refiner|
25
+ puts refiner.compute(persistance_layer)
19
26
  end
20
27
  end
21
28
  end
@@ -26,10 +33,10 @@ class LogMagic
26
33
  end
27
34
  end
28
35
 
29
- def check_for_refiner_output
36
+ def check_for_refiner_match
30
37
  @refiners.any? do |refiner|
31
38
  if refiner.matches?
32
- yield refiner.output
39
+ yield refiner
33
40
  end
34
41
  end
35
42
  end
@@ -44,3 +51,6 @@ require 'log_magic/utils/json_utils'
44
51
  require 'log_magic/log_listener'
45
52
  require 'log_magic/refiners/searchkick_refiner'
46
53
  require 'log_magic/printers/searchkick_printer'
54
+
55
+ require 'log_magic/explainers/searchkick_explainer'
56
+ require 'log_magic/persistance_layer'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log_magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Korfmann
@@ -24,6 +24,48 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: haml
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
27
69
  - !ruby/object:Gem::Dependency
28
70
  name: byebug
29
71
  requirement: !ruby/object:Gem::Requirement
@@ -49,7 +91,10 @@ files:
49
91
  - bin/log_magic
50
92
  - bin/rebuild
51
93
  - lib/log_magic.rb
94
+ - lib/log_magic/explainer_templates/searchkick.haml
95
+ - lib/log_magic/explainers/searchkick_explainer.rb
52
96
  - lib/log_magic/log_listener.rb
97
+ - lib/log_magic/persistance_layer.rb
53
98
  - lib/log_magic/printers/searchkick_printer.rb
54
99
  - lib/log_magic/refiners/searchkick_refiner.rb
55
100
  - lib/log_magic/utils/json_utils.rb