Class: Putpaws::CloudWatch::LogCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/putpaws/cloud_watch/log_command.rb

Constant Summary collapse

SECONDS =
{
  's' => 1,
  'm' => 60,
  'h' => 60 * 60,
  'd' => 60 * 60 * 24,
  'w' => 60 * 60 * 24 * 7,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region:, log_group_prefix: nil) ⇒ LogCommand

Returns a new instance of LogCommand.



47
48
49
50
51
# File 'lib/putpaws/cloud_watch/log_command.rb', line 47

def initialize(region:, log_group_prefix: nil)
  @client = Aws::CloudWatchLogs::Client.new({region: region})
  @log_group_prefix = log_group_prefix
  @log_group = nil
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



44
45
46
# File 'lib/putpaws/cloud_watch/log_command.rb', line 44

def client
  @client
end

#log_groupObject

Returns the value of attribute log_group.



46
47
48
# File 'lib/putpaws/cloud_watch/log_command.rb', line 46

def log_group
  @log_group
end

#log_group_prefixObject (readonly)

Returns the value of attribute log_group_prefix.



45
46
47
# File 'lib/putpaws/cloud_watch/log_command.rb', line 45

def log_group_prefix
  @log_group_prefix
end

#regionObject (readonly)

Returns the value of attribute region.



45
46
47
# File 'lib/putpaws/cloud_watch/log_command.rb', line 45

def region
  @region
end

Class Method Details

.config(config, type: "default") ⇒ Object



13
14
15
16
17
18
19
# File 'lib/putpaws/cloud_watch/log_command.rb', line 13

def self.config(config, type: "default")
  if type == "build"
    new(**config.build_log_command_params)
  else
    new(**config.log_command_params)
  end
end

.filter_args(since: nil, since_for: nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/putpaws/cloud_watch/log_command.rb', line 28

def self.filter_args(since: nil, since_for: nil)
  since_sec = parse_unit_time(since)
  since_for_sec = parse_unit_time(since_for)
  start_time = Time.now - (since_sec || (60*1))
  end_time = if since_sec
    since_for_sec && (start_time + since_for_sec)
  else
    nil
  end
  args = {
    start_time: start_time.to_f * 1000,
    end_time: end_time && (end_time.to_f * 1000),
  }
  args.select{|k,v| v}
end

.parse_unit_time(ut) ⇒ Object



21
22
23
24
25
26
# File 'lib/putpaws/cloud_watch/log_command.rb', line 21

def self.parse_unit_time(ut)
  return nil unless ut
  matched, number, unit = ut.match(/\A(\d+)([smhdw])\z/).to_a
  return nil unless matched
  number.to_i * SECONDS[unit]
end

Instance Method Details

#filter_same_moment_events(events, timestamp) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/putpaws/cloud_watch/log_command.rb', line 91

def filter_same_moment_events(events, timestamp)
  @event_ids_already_shown ||= []
  filtered_events = events.reject{|e| @event_ids_already_shown.include?(e.event_id)}
  event_ids = events.map(&:event_id).uniq
  unless event_ids.empty?
    @event_ids_already_shown = event_ids
  end
  # pp @event_ids_already_shown
  filtered_events
end

#list_log_groupsObject



53
54
55
56
# File 'lib/putpaws/cloud_watch/log_command.rb', line 53

def list_log_groups
  res = client.describe_log_groups(log_group_name_prefix: log_group_prefix)
  res.log_groups
end

#log_events(**args) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/putpaws/cloud_watch/log_command.rb', line 58

def log_events(**args)
  raise "Log group Not Set" unless log_group
  res = client.filter_log_events({
    log_group_name: log_group,
    **args
  })
end

#newest_timestamp(newest) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/putpaws/cloud_watch/log_command.rb', line 81

def newest_timestamp(newest)
  newest = newest.to_i
  if @newest_timestamp
    @newest_timestamp = [@newest_timestamp, newest].max
  else
    @newest_timestamp = newest
  end
  @newest_timestamp
end

#tail_log_events(**args) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/putpaws/cloud_watch/log_command.rb', line 66

def tail_log_events(**args)
  res = log_events(**args)
  nt = newest_timestamp([args[:start_time], *res.events.map(&:timestamp)].compact.max)
  events = filter_same_moment_events(res.events, args[:start_time])
  next_args = if res.next_token
    args.merge(next_token: res.next_token)
  else
    args.merge(
      next_token: nil, 
      start_time: nt,
    )
  end
  [events, next_args]
end