Class: Indy::Search
- Inherits:
-
Object
- Object
- Indy::Search
- Defined in:
- lib/indy/search.rb
Instance Attribute Summary collapse
-
#end_time ⇒ Object
Returns the value of attribute end_time.
-
#inclusive ⇒ Object
Returns the value of attribute inclusive.
-
#source ⇒ Object
Returns the value of attribute source.
-
#start_time ⇒ Object
Returns the value of attribute start_time.
Instance Method Summary collapse
-
#initialize(params_hash = {}) ⇒ Search
constructor
A new instance of Search.
-
#inside_time_window?(time_string, start_time, end_time, inclusive) ⇒ Boolean
Evaluate if a log entry satisfies the configured time conditions.
-
#is_match?(type, result, search_criteria) ⇒ Boolean
Evaluates if field => value criteria is an exact match on entry.
-
#iterate_and_compare(type, search_criteria, &block) ⇒ Object
Helper function called by Indy#for, Indy#like and Indy#all.
-
#multiline_search(&block) ⇒ Object
Performs #search for multi-line based entries.
-
#reset_scope ⇒ Object
Clear time scope settings.
-
#search(&block) ⇒ Object
Search the @source and yield to the block the entry that was found with the LogDefinition.
-
#standard_search(&block) ⇒ Object
Performs #search for line based entries.
-
#time_scope(params_hash) ⇒ Object
Parse hash to set @start_time, @end_time and @inclusive.
-
#time_scope_from_direction(direction, span, time) ⇒ Object
Parse direction, span, and time to set @start_time and @end_time.
-
#use_time_criteria? ⇒ Boolean
Return true if start or end time has been set, and a :time field exists.
Constructor Details
#initialize(params_hash = {}) ⇒ Search
Returns a new instance of Search.
9 10 11 12 13 |
# File 'lib/indy/search.rb', line 9 def initialize(params_hash={}) while (param = params_hash.shift) do send("#{param.first}=",param.last) end end |
Instance Attribute Details
#end_time ⇒ Object
Returns the value of attribute end_time.
7 8 9 |
# File 'lib/indy/search.rb', line 7 def end_time @end_time end |
#inclusive ⇒ Object
Returns the value of attribute inclusive.
7 8 9 |
# File 'lib/indy/search.rb', line 7 def inclusive @inclusive end |
#source ⇒ Object
Returns the value of attribute source.
5 6 7 |
# File 'lib/indy/search.rb', line 5 def source @source end |
#start_time ⇒ Object
Returns the value of attribute start_time.
7 8 9 |
# File 'lib/indy/search.rb', line 7 def start_time @start_time end |
Instance Method Details
#inside_time_window?(time_string, start_time, end_time, inclusive) ⇒ Boolean
Evaluate if a log entry satisfies the configured time conditions
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/indy/search.rb', line 147 def inside_time_window?(time_string,start_time,end_time,inclusive) time = Indy::Time.parse_date(time_string, @source.log_definition.time_format) return false unless time if inclusive return true unless (time > end_time || time < start_time) else return true unless (time >= end_time || time <= start_time) end return false end |
#is_match?(type, result, search_criteria) ⇒ Boolean
Evaluates if field => value criteria is an exact match on entry
101 102 103 104 105 106 107 |
# File 'lib/indy/search.rb', line 101 def is_match?(type, result, search_criteria) if type == :for search_criteria.reject {|criteria,value| result[criteria] == value }.empty? elsif type == :like search_criteria.reject {|criteria,value| result[criteria] =~ /#{value}/i }.empty? end end |
#iterate_and_compare(type, search_criteria, &block) ⇒ Object
Helper function called by Indy#for, Indy#like and Indy#all
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/indy/search.rb', line 23 def iterate_and_compare(type,search_criteria,&block) results = [] results += search do |entry| if type == :all || is_match?(type,entry,search_criteria) result_struct = @source.log_definition.create_struct(entry) if block_given? block.call(result_struct) else result_struct end end end results.compact end |
#multiline_search(&block) ⇒ Object
Performs #search for multi-line based entries
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/indy/search.rb', line 71 def multiline_search(&block) is_time_search = use_time_criteria? source_io = StringIO.new( (is_time_search ? @source.open([@start_time,@end_time]) : @source.open ).join("\n") ) results = source_io.read.scan(@source.log_definition.entry_regexp).collect do |entry| hash = @source.log_definition.parse_entry_captures(entry) next unless hash next unless inside_time_window?(hash[:time],@start_time,@end_time,@inclusive) if is_time_search block.call(hash) if block_given? end results.compact end |
#reset_scope ⇒ Object
Clear time scope settings
140 141 142 |
# File 'lib/indy/search.rb', line 140 def reset_scope @inclusive = @start_time = @end_time = nil end |
#search(&block) ⇒ Object
Search the @source and yield to the block the entry that was found with the LogDefinition
This method is supposed to be used internally.
44 45 46 47 48 49 50 |
# File 'lib/indy/search.rb', line 44 def search(&block) if @source.log_definition.multiline multiline_search(&block) else standard_search(&block) end end |
#standard_search(&block) ⇒ Object
Performs #search for line based entries
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/indy/search.rb', line 55 def standard_search(&block) is_time_search = use_time_criteria? results = [] source_lines = (is_time_search ? @source.open([@start_time,@end_time]) : @source.open) source_lines.each do |single_line| hash = @source.log_definition.parse_entry(single_line) next unless hash next unless inside_time_window?(hash[:time],@start_time,@end_time,@inclusive) if is_time_search results << (block.call(hash) if block_given?) end results.compact end |
#time_scope(params_hash) ⇒ Object
Parse hash to set @start_time, @end_time and @inclusive
112 113 114 115 116 117 118 119 120 |
# File 'lib/indy/search.rb', line 112 def time_scope(params_hash) if params_hash[:time] time_scope_from_direction(params_hash[:direction], params_hash[:span], params_hash[:time]) else @start_time = Indy::Time.parse_date(params_hash[:start_time], @source.log_definition.time_format) if params_hash[:start_time] @end_time = Indy::Time.parse_date(params_hash[:end_time], @source.log_definition.time_format) if params_hash[:end_time] end @inclusive = params_hash[:inclusive] end |
#time_scope_from_direction(direction, span, time) ⇒ Object
Parse direction, span, and time to set @start_time and @end_time
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/indy/search.rb', line 125 def time_scope_from_direction(direction, span, time) time = Indy::Time.parse_date(time, @source.log_definition.time_format) span = (span.to_i * 60).seconds if span if direction == :before @end_time = time @start_time = time - span if span elsif direction == :after @start_time = time @end_time = time + span if span end end |
#use_time_criteria? ⇒ Boolean
Return true if start or end time has been set, and a :time field exists
86 87 88 89 90 91 92 93 |
# File 'lib/indy/search.rb', line 86 def use_time_criteria? if @start_time || @end_time # ensure both boundaries are set @start_time ||= Indy::Time.forever_ago(@source.log_definition.time_format) @end_time ||= Indy::Time.forever(@source.log_definition.time_format) end @start_time && @end_time end |