Skip to content

Commit

Permalink
Better vars and missing timestamp sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
KnVerey committed Dec 14, 2018
1 parent 4b0c715 commit f9a0193
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
21 changes: 10 additions & 11 deletions lib/kubernetes-deploy/container_logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def initialize(parent_id:, container_name:, namespace:, context:, logger:)
def sync
new_logs = fetch_latest
return unless new_logs.present?
@lines += deduplicate(new_logs)
@lines += sort_and_deduplicate(new_logs)
end

def empty?
Expand Down Expand Up @@ -60,21 +60,20 @@ def rfc3339_timestamp(time)
time.strftime("%FT%T.%N%:z")
end

def deduplicate(logs)
deduped = []

def sort_and_deduplicate(logs)
parsed_lines = logs.map { |line| split_timestamped_line(line) }
sorted_lines = parsed_lines.sort do |a, b|
if a[0].nil? || b[0].nil?
0
sorted_lines = parsed_lines.sort do |(timestamp1, _msg1), (timestamp2, _msg2)|
if timestamp1.nil?
-1
elsif timestamp2.nil?
1
else
a[0] <=> b[0]
timestamp1 <=> timestamp2
end
end

sorted_lines.each do |line|
timestamp = line[0]
msg = line[1]
deduped = []
sorted_lines.each do |timestamp, msg|
next if likely_duplicate?(timestamp)
@last_timestamp = timestamp if timestamp
deduped << msg
Expand Down
13 changes: 10 additions & 3 deletions test/unit/container_logs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,23 @@ def test_print_latest_supports_prefixing
end

def test_logs_without_timestamps_are_not_deduped
logs_response_1_with_anomaly = logs_response_1 + "Line 3.5"
logs_response_2_with_anomaly = "Line 3.5\n" + logs_response_2
logs_response_1_with_anomaly = logs_response_1 + "No timestamp"
logs_response_2_with_anomaly = "No timestamp 2\n" + logs_response_2
KubernetesDeploy::Kubectl.any_instance.stubs(:run)
.returns([logs_response_1_with_anomaly, "", ""])
.then.returns([logs_response_2_with_anomaly, "", ""])

@logs.sync
@logs.sync
@logs.print_all
assert_logs_match("Line 3.5", 2)
assert_logs_match_all([
"No timestamp", # moved to start of batch 1
"Line 1",
"Line 2",
"Line 3",
"No timestamp 2", # moved to start of batch 2
"Line 4"
], in_order: true)
end

def test_deduplication_works_when_exact_same_batch_is_returned_more_than_once
Expand Down

0 comments on commit f9a0193

Please sign in to comment.