papertrail 0.9.6 → 0.9.7

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.
data/lib/papertrail.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Papertrail
2
- VERSION = "0.9.6"
2
+ VERSION = "0.9.7"
3
3
  end
4
4
 
5
5
  require 'papertrail/search_client'
@@ -9,18 +9,24 @@ module Papertrail
9
9
  class Cli
10
10
  include Papertrail::CliHelpers
11
11
 
12
- def run
13
- # Let it slide if we have invalid JSON
14
- if JSON.respond_to?(:default_options)
15
- JSON.default_options[:check_utf8] = false
16
- end
12
+ attr_reader :options, :query_options, :connection
17
13
 
18
- options = {
14
+ def initialize
15
+ @options = {
19
16
  :configfile => nil,
20
17
  :delay => 2,
21
18
  :follow => false
22
19
  }
23
20
 
21
+ @query_options = {}
22
+ end
23
+
24
+ def run
25
+ # Let it slide if we have invalid JSON
26
+ if JSON.respond_to?(:default_options)
27
+ JSON.default_options[:check_utf8] = false
28
+ end
29
+
24
30
  if configfile = find_configfile
25
31
  configfile_options = load_configfile(configfile)
26
32
  options.merge!(configfile_options)
@@ -66,9 +72,7 @@ module Papertrail
66
72
  options.merge!(configfile_options)
67
73
  end
68
74
 
69
- connection = Papertrail::Connection.new(options)
70
-
71
- query_options = {}
75
+ @connection = Papertrail::Connection.new(options)
72
76
 
73
77
  if options[:system]
74
78
  query_options[:system_id] = connection.find_id_for_source(options[:system])
@@ -84,33 +88,72 @@ module Papertrail
84
88
  end
85
89
  end
86
90
 
87
- set_min_max_time!(options, query_options)
88
-
89
- search_query = connection.query(ARGV[0], query_options)
90
-
91
91
  if options[:follow]
92
+ search_query = connection.query(ARGV[0], query_options)
93
+
92
94
  loop do
93
- if options[:json]
94
- $stdout.puts search_query.search.data.to_json
95
- else
96
- search_query.search.events.each do |event|
97
- $stdout.puts event
98
- end
99
- end
100
- $stdout.flush
95
+ display_results(search_query.search)
101
96
  sleep options[:delay]
102
97
  end
98
+ elsif options[:min_time]
99
+ query_time_range
103
100
  else
104
- if options[:json]
105
- $stdout.puts search_query.search.data.to_json
106
- else
107
- search_query.search.events.each do |event|
101
+ set_min_max_time!(options, query_options)
102
+ search_query = connection.query(ARGV[0], query_options)
103
+ display_results(search_query.search)
104
+ end
105
+ end
106
+
107
+ def query_time_range
108
+ min_time = parse_time(options[:min_time])
109
+
110
+ if options[:max_time]
111
+ max_time = parse_time(options[:max_time])
112
+ end
113
+
114
+ search_results = connection.query(ARGV[0], query_options.merge(:min_time => min_time.to_i, :tail => false)).search
115
+
116
+ loop do
117
+ search_results.events.each do |event|
118
+ # If we've found an event beyond what we were looking for, we're done
119
+ if max_time && event.received_at > max_time
120
+ break
121
+ end
122
+
123
+ if options[:json]
124
+ $stdout.puts event.data.to_json
125
+ else
108
126
  $stdout.puts event
109
127
  end
110
128
  end
129
+
130
+ # If we've found the end of what we're looking for, we're done
131
+ if max_time && search_results.max_time_at > max_time
132
+ break
133
+ end
134
+
135
+ if search_results.reached_end?
136
+ break
137
+ end
138
+
139
+ # Perform the next search
140
+ search_results = connection.query(ARGV[0], query_options.merge(:min_id => search_results.max_id, :tail => false)).search
141
+ end
142
+ end
143
+
144
+ def display_results(results)
145
+ if options[:json]
146
+ $stdout.puts results.data.to_json
147
+ else
148
+ results.events.each do |event|
149
+ $stdout.puts event
150
+ end
111
151
  end
152
+
153
+ $stdout.flush
112
154
  end
113
155
 
156
+
114
157
  def usage
115
158
  <<-EOF
116
159
 
@@ -8,8 +8,12 @@ module Papertrail
8
8
  @data = data
9
9
  end
10
10
 
11
+ def received_at
12
+ @received_at ||= Time.parse(data['received_at'])
13
+ end
14
+
11
15
  def to_s
12
- "#{Time.parse(data['received_at']).strftime('%b %e %X')} #{data['hostname']} #{data['program']}: #{data['message']}"
16
+ "#{received_at.strftime('%b %e %X')} #{data['hostname']} #{data['program']}: #{data['message']}"
13
17
  end
14
18
  end
15
19
  end
@@ -12,6 +12,14 @@ module Papertrail
12
12
  end
13
13
  end
14
14
 
15
+ def reached_end?
16
+ @data['reached_end']
17
+ end
18
+
19
+ def max_time_at
20
+ @max_time_at ||= Time.parse(@data['max_time_at'])
21
+ end
22
+
15
23
  def max_id
16
24
  @data['max_id']
17
25
  end
data/papertrail.gemspec CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'papertrail'
16
- s.version = '0.9.6'
17
- s.date = '2012-10-05'
16
+ s.version = '0.9.7'
17
+ s.date = '2013-02-18'
18
18
  s.rubyforge_project = 'papertrail'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 9
8
- - 6
9
- version: 0.9.6
8
+ - 7
9
+ version: 0.9.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - Papertrail
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-10-05 00:00:00 -07:00
17
+ date: 2013-02-18 00:00:00 -08:00
18
18
  default_executable: papertrail
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency