Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions bin/summarize-user-events
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ class SummarizeUserEvents
end

def run
results = cloudwatch_client.fetch(
query: query,
from: from_date,
to: to_date,
)
pp results
find_cloudwatch_events do |cloudwatch_event|
pp cloudwatch_event
end
end


Expand All @@ -46,6 +43,7 @@ class SummarizeUserEvents
, @timestamp
| filter properties.user_id = '#{uuid}'
| sort @timestamp asc
| limit 10000
QUERY
end

Expand All @@ -57,6 +55,32 @@ class SummarizeUserEvents
log_group_name: 'prod_/srv/idp/shared/log/events.log',
)
end

def find_cloudwatch_events(&block)
if $stdin.tty?
cloudwatch_source(&block)
else
warn "Reading Cloudwatch events as newline-delimited JSON (ndjson) from stdin"
stdin_source(&block)
end
end

def stdin_source(&block)
$stdin.each_line do |line|
next if line.blank?
event = JSON.parse(line)
block.call(event)
Comment on lines +68 to +72
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

micro-optimization! don't assign a variable to the block argument, it saves an allocation on the heap, use the yield keyword which uses the stack version

Also added some docs to indicate that it takes a block

Suggested change
def stdin_source(&block)
$stdin.each_line do |line|
next if line.blank?
event = JSON.parse(line)
block.call(event)
# @yield calls block for each parsed event
# @yieldparam [Hash] event
def stdin_source
$stdin.each_line do |line|
next if line.blank?
event = JSON.parse(line)
yield event

end
end

def cloudwatch_source(&block)
cloudwatch_client.fetch(
query: query,
from: from_date,
to: to_date,
&block
)
end
end


Expand Down