indy 0.1.4 → 0.1.5
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/History.txt +8 -0
- data/indy.gemspec +8 -5
- data/lib/indy/indy.rb +56 -17
- data/performance/large.log +10000 -10000
- data/performance/profile_spec.rb +22 -2
- data/spec/indy_spec.rb +14 -2
- data/spec/time_spec.rb +13 -15
- metadata +7 -7
data/History.txt
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
=== 0.1.5 / 2011-01-21
|
2
|
+
|
3
|
+
* Searching with time scopes (#after, #within, #before) are much faster
|
4
|
+
* Dates used in time scopes are limited to 1901 - 2038
|
5
|
+
* The Windows platform limits dates further to 1969 - 2038
|
6
|
+
* Explicit format string for time forces the old (slower) time scoping code
|
7
|
+
* However, this removes the harsh limits on date values
|
8
|
+
|
1
9
|
=== 0.1.4 / 2011-01-19
|
2
10
|
|
3
11
|
* Add support for Ruby 1.8.5
|
data/indy.gemspec
CHANGED
@@ -62,9 +62,12 @@ Gem::Specification.new do |s|
|
|
62
62
|
|
63
63
|
}
|
64
64
|
|
65
|
-
|
66
|
-
s.files
|
67
|
-
|
68
|
-
s.
|
69
|
-
s.
|
65
|
+
# exclusions = [File.join("performance", "large.log")]
|
66
|
+
# s.files = `git ls-files`.split("\n") - exclusions
|
67
|
+
|
68
|
+
s.rubygems_version = "1.3.7"
|
69
|
+
s.files = `git ls-files`.split("\n")
|
70
|
+
s.extra_rdoc_files = ["README.md", "History.txt"]
|
71
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
72
|
+
s.require_path = "lib"
|
70
73
|
end
|
data/lib/indy/indy.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
-
require 'ostruct'
|
2
1
|
require 'active_support/core_ext'
|
3
2
|
|
4
3
|
class Indy
|
5
4
|
|
6
|
-
|
5
|
+
class InvalidSource < Exception; end
|
6
|
+
|
7
|
+
VERSION = "0.1.5"
|
7
8
|
|
8
9
|
#
|
9
10
|
# hash with one key (:string, :file, or :cmd) set to the string that defines the log
|
@@ -21,10 +22,6 @@ class Indy
|
|
21
22
|
#
|
22
23
|
attr_accessor :time_format
|
23
24
|
|
24
|
-
FOREVER_AGO = DateTime.now - 200_000
|
25
|
-
FOREVER = DateTime.now + 200_000
|
26
|
-
|
27
|
-
|
28
25
|
#
|
29
26
|
# Initialize Indy.
|
30
27
|
#
|
@@ -72,7 +69,10 @@ class Indy
|
|
72
69
|
# @example
|
73
70
|
# Indy.search(:source => {:cmd => "cat apache.log"}, :pattern => LOG_PATTERN, :time_format => MY_TIME_FORMAT).for(:all)
|
74
71
|
#
|
75
|
-
def search(params)
|
72
|
+
def search(params=nil)
|
73
|
+
|
74
|
+
raise Indy::InvalidSource if params.nil?
|
75
|
+
|
76
76
|
if params.respond_to?(:keys) && params[:source]
|
77
77
|
Indy.new(params)
|
78
78
|
else
|
@@ -231,7 +231,8 @@ class Indy
|
|
231
231
|
#
|
232
232
|
def within(scope_criteria)
|
233
233
|
if scope_criteria[:time]
|
234
|
-
@start_time, @end_time = scope_criteria[:time]
|
234
|
+
@start_time, @end_time = scope_criteria[:time].collect {|str| parse_date(str) }
|
235
|
+
|
235
236
|
@inclusive = scope_criteria[:inclusive] || false
|
236
237
|
end
|
237
238
|
|
@@ -254,6 +255,8 @@ class Indy
|
|
254
255
|
#
|
255
256
|
def source=(param)
|
256
257
|
|
258
|
+
raise Indy::InvalidSource if param.nil?
|
259
|
+
|
257
260
|
cmd = param[:cmd] rescue nil
|
258
261
|
@source[:cmd] = param[:cmd] if cmd
|
259
262
|
|
@@ -261,6 +264,8 @@ class Indy
|
|
261
264
|
File.exist?(param) ? @source[:file] = param : @source[:string] = param
|
262
265
|
end
|
263
266
|
|
267
|
+
raise Indy::InvalidSource unless @source.values.reject {|value| value.kind_of? String }.empty?
|
268
|
+
|
264
269
|
end
|
265
270
|
|
266
271
|
#
|
@@ -293,6 +298,7 @@ class Indy
|
|
293
298
|
#
|
294
299
|
def _search(&block)
|
295
300
|
|
301
|
+
line_matched = nil
|
296
302
|
time_search = use_time_criteria?
|
297
303
|
|
298
304
|
source_io = open_source
|
@@ -300,6 +306,8 @@ class Indy
|
|
300
306
|
|
301
307
|
hash = parse_line(line)
|
302
308
|
|
309
|
+
hash ? (line_matched = true) : next
|
310
|
+
|
303
311
|
if time_search
|
304
312
|
set_time(hash)
|
305
313
|
next unless inside_time_window?(hash)
|
@@ -307,11 +315,12 @@ class Indy
|
|
307
315
|
hash[:_time] = nil if hash
|
308
316
|
end
|
309
317
|
|
310
|
-
next unless hash
|
311
318
|
block_given? ? block.call(hash) : nil
|
312
319
|
|
313
320
|
end
|
314
321
|
|
322
|
+
warn "No matching lines found in source: #{source_io.class}" unless line_matched
|
323
|
+
|
315
324
|
source_io.close if @source[:file] || @source[:cmd]
|
316
325
|
|
317
326
|
results.compact
|
@@ -375,8 +384,8 @@ class Indy
|
|
375
384
|
def use_time_criteria?
|
376
385
|
if @start_time || @end_time
|
377
386
|
# ensure both boundaries are set
|
378
|
-
@start_time = @start_time ||
|
379
|
-
@end_time = @end_time ||
|
387
|
+
@start_time = @start_time || forever_ago
|
388
|
+
@end_time = @end_time || forever
|
380
389
|
end
|
381
390
|
|
382
391
|
return (@time_field && @start_time && @end_time)
|
@@ -416,16 +425,26 @@ class Indy
|
|
416
425
|
#
|
417
426
|
def parse_date(param)
|
418
427
|
return nil unless @time_field
|
428
|
+
return param if param.kind_of? Time or param.kind_of? DateTime
|
419
429
|
|
420
430
|
time_string = param[@time_field] ? param[@time_field] : param
|
421
431
|
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
432
|
+
if @time_format
|
433
|
+
begin
|
434
|
+
# Attempt the appropriate parse method
|
435
|
+
DateTime.strptime(time_string, @time_format)
|
436
|
+
rescue
|
437
|
+
# If appropriate, fall back to simple parse method
|
438
|
+
DateTime.parse(time_string) rescue nil
|
439
|
+
end
|
440
|
+
else
|
441
|
+
begin
|
442
|
+
Time.parse(time_string)
|
443
|
+
rescue Exception => e
|
444
|
+
raise "Failed to create time object. The error was: #{e.message}"
|
445
|
+
end
|
428
446
|
end
|
447
|
+
|
429
448
|
end
|
430
449
|
|
431
450
|
#
|
@@ -469,4 +488,24 @@ class Indy
|
|
469
488
|
Struct::Line.new( *params )
|
470
489
|
end
|
471
490
|
|
491
|
+
#
|
492
|
+
# Return a time or datetime object way in the future
|
493
|
+
#
|
494
|
+
def forever
|
495
|
+
@time_format ? DateTime.new(4712) : Time.at(0x7FFFFFFF)
|
496
|
+
end
|
497
|
+
|
498
|
+
#
|
499
|
+
# Return a time or datetime object way in the past
|
500
|
+
#
|
501
|
+
def forever_ago
|
502
|
+
begin
|
503
|
+
@time_format ? DateTime.new(-4712) : Time.at(-0x7FFFFFFF)
|
504
|
+
rescue
|
505
|
+
# Windows Ruby Time can't handle dates prior to 1969
|
506
|
+
@time_format ? DateTime.new(-4712) : Time.at(0)
|
507
|
+
end
|
508
|
+
end
|
509
|
+
|
510
|
+
|
472
511
|
end
|