peephole 0.0.8 → 0.1.0

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: 66c47da85914a7ef81b2261760da44946c076b1c
4
- data.tar.gz: 9b810c39f4a54659227ba7f22d49c83a0d509542
3
+ metadata.gz: 6c1e1d86e93630e38593455701a77a55da123ae9
4
+ data.tar.gz: 128d0ed52547503ad721e48e84c975f4cd9ff0c3
5
5
  SHA512:
6
- metadata.gz: 534da23fd70f3fc234dec13863e3c925b0f217c51cab40bbcdc6d202a1555826a12c7764209e6689eb8bc8e9869f3ee3f21371f823aba903a465398109f6f3f3
7
- data.tar.gz: 9695bbc96f79cb4101f29ad2b888555dcaf162510da234d9f1bedacbb006f54478ccc44928abc4100307af43c9cb1189cbd0b7138c3dbe0ea5043a907f73d5f7
6
+ metadata.gz: 9debb5b6a4edc80500caa2f70337427cc926d961783527e3bd20a8a85c43eb5fcc16c78f532f1e7f2adf72b6066cd097ceff986e7576b879f26a7c27ba4d12de
7
+ data.tar.gz: b436052310debd50fd478c75e9e3c9934380145b654ae889f974627b64d1cea9c057578441041e03a4c78e754cad05030bd0fe96197b307987b29c792df9ad90
data/README.md CHANGED
@@ -27,7 +27,8 @@ $ rails g peephole:install
27
27
  ```
28
28
  # config/initializers/peephole.rb
29
29
  Peephole.configure do |config|
30
- config.paginates_per = 200
30
+ config.lines_per = 200
31
+ config.bytes_per = 5000
31
32
  config.peeper? do
32
33
  # current_user.role.admin?
33
34
 
@@ -3,19 +3,27 @@ require_dependency "peephole/application_controller"
3
3
  module Peephole
4
4
  class LogfilesController < ApplicationController
5
5
  before_action :set_logfile
6
+ before_action :set_page, only: %i(show raw)
6
7
 
7
8
  def show
8
- @page = params[:page].to_i.nonzero? || 1
9
- @loglines, @eof = @logfile.loglines(@page)
9
+ @loglines, @eof = @logfile.lines(@page)
10
10
  end
11
11
 
12
12
  def download
13
13
  send_file @logfile.path
14
14
  end
15
15
 
16
+ def raw
17
+ @raw, @eof = @logfile.bytes(@page)
18
+ end
19
+
16
20
  private
17
21
  def set_logfile
18
22
  @logfile = Logfile.find(params[:id])
19
23
  end
24
+
25
+ def set_page
26
+ @page = params[:page].to_i.nonzero? || 1
27
+ end
20
28
  end
21
29
  end
@@ -5,10 +5,6 @@ module Peephole
5
5
  LOG_PATH = Rails.root.join('log')
6
6
 
7
7
  class << self
8
- def glob
9
- Dir.glob(LOG_PATH.join("#{Rails.env}.log*"))
10
- end
11
-
12
8
  def all
13
9
  glob.map do |path|
14
10
  Logfile.new(path)
@@ -21,6 +17,11 @@ module Peephole
21
17
  end
22
18
  Logfile.new(path)
23
19
  end
20
+
21
+ private
22
+ def glob
23
+ Dir.glob(LOG_PATH.join("#{Rails.env}.log*"))
24
+ end
24
25
  end
25
26
 
26
27
  def initialize(path)
@@ -32,8 +33,12 @@ module Peephole
32
33
  filename
33
34
  end
34
35
 
35
- def loglines(page)
36
- Logline.where(path, page)
36
+ def lines(page)
37
+ Logline.lines(path, page)
38
+ end
39
+
40
+ def bytes(page)
41
+ Logline.bytes(path, page)
37
42
  end
38
43
 
39
44
  def size
@@ -43,5 +48,9 @@ module Peephole
43
48
  def updated_at
44
49
  File.mtime(path)
45
50
  end
51
+
52
+ def gz?
53
+ File.extname(path) == '.gz'
54
+ end
46
55
  end
47
56
  end
@@ -12,52 +12,80 @@ module Peephole
12
12
  end
13
13
 
14
14
  class << self
15
- def first(page)
16
- (page - 1) * Peephole.config.paginates_per
15
+ def first_line(page)
16
+ first(page, :line)
17
17
  end
18
18
 
19
- def last(page)
20
- page * Peephole.config.paginates_per
19
+ def last_line(page)
20
+ last(page, :line)
21
21
  end
22
22
 
23
- def where(path, page)
24
- loglines = []
25
- logmap = {}
23
+ def lines(path, page)
24
+ lines = []
25
+ map = {}
26
26
  eof = true
27
27
  each(path, page) do |line, i|
28
- next if i < first(page)
29
- if i >= last(page)
28
+ next if i < first_line(page)
29
+ if i >= last_line(page)
30
30
  eof = false
31
31
  break
32
32
  end
33
- parse(line, i + 1, loglines, logmap)
33
+ parse(line, i + 1, lines, map)
34
34
  end
35
- [loglines, eof]
35
+ [lines, eof]
36
36
  end
37
37
 
38
- def parse(line, num, loglines, logmap)
39
- logline = new(line, num)
40
- case logline.type
41
- when TYPE::STARTED
42
- logmap[logline.uuid] = logline if logline.uuid.present?
43
- when TYPE::PARAMS
44
- line = logmap[logline.uuid].presence || logline
45
- line.params = logline.params
46
- loglines << line
47
- when TYPE::COMPLETED
48
- logmap[logline.uuid].try(:status=, logline.status)
49
- end
38
+ def first_byte(page)
39
+ first(page, :byte)
40
+ end
41
+
42
+ def last_byte(page)
43
+ last(page, :byte)
50
44
  end
51
45
 
52
- def each(path, page, &block)
53
- iterator = case path.to_s
54
- when /\.gz\z/
55
- Zlib::GzipReader.open(path).each
56
- else
57
- IO.foreach(path)
46
+ def bytes(path, page)
47
+ eof = false
48
+ raw = ''
49
+ open(path) do |f|
50
+ f.seek(first_byte(page))
51
+ raw = f.read(Peephole.config.bytes_per)
52
+ eof = f.eof?
58
53
  end
59
- iterator.with_index(&block)
54
+ [raw, eof]
60
55
  end
56
+
57
+ private
58
+ def first(page, type)
59
+ (page - 1) * Peephole.config.send("#{type}s_per")
60
+ end
61
+
62
+ def last(page, type)
63
+ page * Peephole.config.send("#{type}s_per")
64
+ end
65
+
66
+ def parse(line, num, lines, map)
67
+ logline = new(line, num)
68
+ case logline.type
69
+ when TYPE::STARTED
70
+ map[logline.uuid] = logline if logline.uuid.present?
71
+ when TYPE::PARAMS
72
+ line = map[logline.uuid].presence || logline
73
+ line.params = logline.params
74
+ lines << line
75
+ when TYPE::COMPLETED
76
+ map[logline.uuid].try(:status=, logline.status)
77
+ end
78
+ end
79
+
80
+ def each(path, page, &block)
81
+ iterator = case path.to_s
82
+ when /\.gz\z/
83
+ Zlib::GzipReader.open(path).each
84
+ else
85
+ IO.foreach(path)
86
+ end
87
+ iterator.with_index(&block)
88
+ end
61
89
  end
62
90
 
63
91
  def initialize(line, num)
@@ -0,0 +1,11 @@
1
+ <ol class="breadcrumb">
2
+ <li><%= link_to 'Home', :root %></li>
3
+ <li><%= link_to @logfile.filename, type == :line ? logfile_path(@logfile) : raw_logfile_path(@logfile) %></li>
4
+ <li class="active">
5
+ Displaying <%= type %>s
6
+ <%= Peephole::Logline.send("first_#{type}", @page) %>
7
+ -
8
+ <%= Peephole::Logline.send("last_#{type}", @page) %>
9
+ </li>
10
+ </ol>
11
+
@@ -0,0 +1,6 @@
1
+ <%= render 'breadcrumb', type: :byte %>
2
+ <%= render 'pager' %>
3
+
4
+ <pre><%= @raw %></pre>
5
+
6
+ <%= render 'pager' %>
@@ -1,11 +1,4 @@
1
- <ol class="breadcrumb">
2
- <li><%= link_to 'Home', :root %></li>
3
- <li><%= link_to @logfile.filename, logfile_path(@logfile) %></li>
4
- <li class="active">
5
- Displaying lines <%= Peephole::Logline.first(@page) %> - <%= Peephole::Logline.last(@page) %>
6
- </li>
7
- </ol>
8
-
1
+ <%= render 'breadcrumb', type: :line %>
9
2
  <%= render 'pager' %>
10
3
 
11
4
  <table class="table table-bordered">
@@ -10,7 +10,12 @@
10
10
  <td><%= logfile.size %> KB</td>
11
11
  <td><%= logfile.updated_at %></td>
12
12
  <td class="text-right">
13
- <%= link_to 'Download', download_logfile_path(logfile), class: 'btn btn-default btn-sm' %>
13
+ <div class="btn-group btn-group-sm">
14
+ <% unless logfile.gz? %>
15
+ <%= link_to 'Raw', raw_logfile_path(logfile), class: 'btn btn-default' %>
16
+ <% end %>
17
+ <%= link_to 'Download', download_logfile_path(logfile), class: 'btn btn-default' %>
18
+ </div>
14
19
  </td>
15
20
  </tr>
16
21
  <% end %>
data/config/routes.rb CHANGED
@@ -5,6 +5,7 @@ Peephole::Engine.routes.draw do
5
5
  resources :logfiles, only: [:show], id: /.+/ do
6
6
  member do
7
7
  get :download
8
+ get :raw
8
9
  end
9
10
  end
10
11
  end
@@ -1,5 +1,6 @@
1
1
  Peephole.configure do |config|
2
- config.paginates_per = 200
2
+ config.lines_per = 200
3
+ config.bytes_per = 5000
3
4
  config.peeper? do
4
5
  !Rails.env.production?
5
6
  end
@@ -2,9 +2,12 @@ module Peephole
2
2
  class Config
3
3
  include ActiveSupport::Configurable
4
4
 
5
- config_accessor :paginates_per do
5
+ config_accessor :lines_per do
6
6
  200
7
7
  end
8
+ config_accessor :bytes_per do
9
+ 5000
10
+ end
8
11
 
9
12
  DEFAULT_PEEPER = proc { !Rails.env.production? }
10
13
  def peeper?(&block)
@@ -1,3 +1,3 @@
1
1
  module Peephole
2
- VERSION = "0.0.8"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: peephole
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tnantoka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-17 00:00:00.000000000 Z
11
+ date: 2015-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -86,7 +86,9 @@ files:
86
86
  - app/models/peephole/logfile.rb
87
87
  - app/models/peephole/logline.rb
88
88
  - app/views/layouts/peephole/application.html.erb
89
+ - app/views/peephole/logfiles/_breadcrumb.html.erb
89
90
  - app/views/peephole/logfiles/_pager.html.erb
91
+ - app/views/peephole/logfiles/raw.html.erb
90
92
  - app/views/peephole/logfiles/show.html.erb
91
93
  - app/views/peephole/welcome/index.html.erb
92
94
  - config/routes.rb