Class: CloudwatchQuery::Query
- Inherits:
-
Object
- Object
- CloudwatchQuery::Query
- Includes:
- Enumerable
- Defined in:
- lib/cloudwatch_query/query.rb
Constant Summary collapse
- DEFAULT_FIELDS =
%w[@timestamp @message @logStream @log].freeze
Instance Method Summary collapse
- #before(time) ⇒ Object
- #between(start_time, end_time) ⇒ Object
- #contains(text) ⇒ Object
- #each(&block) ⇒ Object
-
#execute ⇒ Object
(also: #to_a)
Execution.
-
#fields(*field_list) ⇒ Object
Field selection.
-
#initialize ⇒ Query
constructor
A new instance of Query.
- #limit(n) ⇒ Object
-
#logs(*groups) ⇒ Object
(also: #log_group)
Log group selection.
- #matches(pattern) ⇒ Object
- #past(amount, unit) ⇒ Object
-
#since(time) ⇒ Object
Time range.
- #sort(field, order = :desc) ⇒ Object
- #to_insights_query ⇒ Object
-
#where(**conditions) ⇒ Object
Filtering.
Constructor Details
#initialize ⇒ Query
Returns a new instance of Query.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/cloudwatch_query/query.rb', line 9 def initialize @log_groups = [] @conditions = [] @fields = DEFAULT_FIELDS.dup @start_time = nil @end_time = nil @limit = nil @sort_field = "@timestamp" @sort_order = "desc" end |
Instance Method Details
#before(time) ⇒ Object
52 53 54 55 |
# File 'lib/cloudwatch_query/query.rb', line 52 def before(time) @end_time = TimeHelpers.to_epoch(time) self end |
#between(start_time, end_time) ⇒ Object
57 58 59 60 61 |
# File 'lib/cloudwatch_query/query.rb', line 57 def between(start_time, end_time) @start_time = TimeHelpers.to_epoch(start_time) @end_time = TimeHelpers.to_epoch(end_time) self end |
#contains(text) ⇒ Object
36 37 38 39 |
# File 'lib/cloudwatch_query/query.rb', line 36 def contains(text) @conditions << "@message like /#{escape_regex(text)}/" self end |
#each(&block) ⇒ Object
100 101 102 |
# File 'lib/cloudwatch_query/query.rb', line 100 def each(&block) execute.each(&block) end |
#execute ⇒ Object Also known as: to_a
Execution
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/cloudwatch_query/query.rb', line 88 def execute validate! client.execute( query_string: to_insights_query, log_group_names: @log_groups, start_time: resolved_start_time, end_time: resolved_end_time, limit: resolved_limit ) end |
#fields(*field_list) ⇒ Object
Field selection
71 72 73 74 |
# File 'lib/cloudwatch_query/query.rb', line 71 def fields(*field_list) @fields = field_list.flatten.map { |f| f.to_s.start_with?("@") ? f.to_s : "@#{f}" } self end |
#limit(n) ⇒ Object
76 77 78 79 |
# File 'lib/cloudwatch_query/query.rb', line 76 def limit(n) @limit = n self end |
#logs(*groups) ⇒ Object Also known as: log_group
Log group selection
21 22 23 24 |
# File 'lib/cloudwatch_query/query.rb', line 21 def logs(*groups) @log_groups.concat(groups.flatten) self end |
#matches(pattern) ⇒ Object
41 42 43 44 |
# File 'lib/cloudwatch_query/query.rb', line 41 def matches(pattern) @conditions << "@message like /#{pattern}/" self end |
#past(amount, unit) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/cloudwatch_query/query.rb', line 63 def past(amount, unit) seconds = TimeHelpers.duration_in_seconds(amount, unit) @start_time = (Time.now - seconds).to_i @end_time = Time.now.to_i self end |
#since(time) ⇒ Object
Time range
47 48 49 50 |
# File 'lib/cloudwatch_query/query.rb', line 47 def since(time) @start_time = TimeHelpers.to_epoch(time) self end |
#sort(field, order = :desc) ⇒ Object
81 82 83 84 85 |
# File 'lib/cloudwatch_query/query.rb', line 81 def sort(field, order = :desc) @sort_field = field.to_s.start_with?("@") ? field.to_s : "@#{field}" @sort_order = order.to_s self end |
#to_insights_query ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/cloudwatch_query/query.rb', line 104 def to_insights_query parts = [] parts << "fields #{@fields.join(', ')}" @conditions.each { |c| parts << "filter #{c}" } parts << "sort #{@sort_field} #{@sort_order}" parts << "limit #{resolved_limit}" if resolved_limit parts.join(" | ") end |
#where(**conditions) ⇒ Object
Filtering
28 29 30 31 32 33 34 |
# File 'lib/cloudwatch_query/query.rb', line 28 def where(**conditions) conditions.each do |field, value| field_name = field.to_s.start_with?("@") ? field.to_s : field.to_s @conditions << "#{field_name} = '#{escape_value(value)}'" end self end |