Skip to content

Commit

Permalink
fix timestamp format
Browse files Browse the repository at this point in the history
  • Loading branch information
mensfeld committed May 26, 2024
1 parent bc2d7f0 commit 5f0a975
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/karafka/web/pro/ui/lib/search/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def iterator
when 'offset'
offset
when 'timestamp'
Time.at(timestamp)
# Kafka timestamp of message is in ms, we need a second precision for
# `Time#at`
Time.at(timestamp / 1_000.to_f)
else
# This should never happen. Contact us if you see this.
raise ::Karafka::Errors::UnsupportedCaseError, offset_type
Expand Down
2 changes: 1 addition & 1 deletion lib/karafka/web/pro/ui/views/search/_search_modal.erb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@

<div class="form-check">
<input class="form-check-input" type="radio" name="search[offset_type]" id="offset-timestamp" value="timestamp" <%= 'checked' if offset_type == 'timestamp' %>>
<label class="form-check-label" for="offset-timestamp">Timestamp</label>
<label class="form-check-label" for="offset-timestamp">Timestamp (in ms)</label>
<input type="number" value="<%= timestamp %>" class="form-control mt-2" id="offset-timestamp-input" name="search[timestamp]" min="0" max="<%= ((Time.now.to_f + 60 * 60 * 24 * 31) * 1_000).to_i %>" disabled>
</div>
</div>
Expand Down
119 changes: 118 additions & 1 deletion spec/lib/karafka/web/pro/ui/controllers/search_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
let(:valid_search) do
<<~SEARCH.tr("\n", '&')
search[matcher]=Raw+payload+includes
search[phrase]=1
search[phrase]=find-me
search[partitions][]=all
search[offset_type]=latest
search[limit]=1000
Expand Down Expand Up @@ -124,4 +124,121 @@
expect(body).not_to include(support_message)
end
end

context 'when searching a topic that has matches only in the second partition' do
let(:partitions) { 2 }

before do
produce_many(topic, %w[message message2 message 3], partition: 0)
produce_many(topic, %w[find-me also-find-me find-me-and], partition: 1)

get "explorer/#{topic}/search?#{valid_search}"
end

it do
expect(response).to be_ok
expect(body).to include(breadcrumbs)
expect(body).to include(search_modal)
expect(body).to include('table')
expect(body).to include('Raw payload includes')
expect(body).to include('Search criteria:')
expect(body).to include('Total Messages Checked')
expect(body).to include('Partition 0')
expect(body).to include('Partition 1')
expect(body).to include(metadata_button)
expect(body).to include(search_metadata)
expect(body).to include('<td>3</td>')
expect(body).not_to include(nothing_found)
expect(body).not_to include(no_search_criteria)
expect(body).not_to include(search_modal_errors)
expect(body).not_to include('matcher: is invalid')
expect(body).not_to include(pagination)
expect(body).not_to include(support_message)
end
end

context 'when searching a topic that has results but before the start timestamp' do
let(:partitions) { 2 }
let(:valid_search) do
<<~SEARCH.tr("\n", '&')
search[matcher]=Raw+payload+includes
search[phrase]=find-me
search[partitions][]=all
search[offset_type]=timestamp
search[timestamp]=#{(Time.now.to_f * 1_000).to_i}
search[limit]=1000
SEARCH
end

before do
produce_many(topic, %w[find-me also-find-me find-me-and], partition: 0)
produce_many(topic, %w[find-me also-find-me find-me-and], partition: 1)

sleep(1)

get "explorer/#{topic}/search?#{valid_search}"
end

it do
expect(response).to be_ok
expect(body).to include(breadcrumbs)
expect(body).to include(search_modal)
expect(body).to include('table')
expect(body).to include('Raw payload includes')
expect(body).to include('Search criteria:')
expect(body).to include('Total Messages Checked')
expect(body).to include('Partition 0')
expect(body).to include('Partition 1')
expect(body).to include(metadata_button)
expect(body).to include(search_metadata)
expect(body).to include(nothing_found)
expect(body).not_to include(no_search_criteria)
expect(body).not_to include(search_modal_errors)
expect(body).not_to include('matcher: is invalid')
expect(body).not_to include(pagination)
expect(body).not_to include(support_message)
end
end

context 'when searching a topic that has results after the start timestamp' do
let(:partitions) { 2 }
let(:valid_search) do
<<~SEARCH.tr("\n", '&')
search[matcher]=Raw+payload+includes
search[phrase]=find-me
search[partitions][]=all
search[offset_type]=timestamp
search[timestamp]=#{((Time.now.to_f - 100) * 1_000).to_i}
search[limit]=1000
SEARCH
end

before do
produce_many(topic, %w[find-me also-find-me find-me-and], partition: 0)
produce_many(topic, %w[find-me also-find-me find-me-and], partition: 1)

get "explorer/#{topic}/search?#{valid_search}"
end

it do
expect(response).to be_ok
expect(body).to include(breadcrumbs)
expect(body).to include(search_modal)
expect(body).to include('table')
expect(body).to include('Raw payload includes')
expect(body).to include('Search criteria:')
expect(body).to include('Total Messages Checked')
expect(body).to include('Partition 0')
expect(body).to include('Partition 1')
expect(body).to include(metadata_button)
expect(body).to include(search_metadata)
expect(body).to include('<td>6</td>')
expect(body).not_to include(nothing_found)
expect(body).not_to include(no_search_criteria)
expect(body).not_to include(search_modal_errors)
expect(body).not_to include('matcher: is invalid')
expect(body).not_to include(pagination)
expect(body).not_to include(support_message)
end
end
end
10 changes: 5 additions & 5 deletions spec/lib/karafka/web/pro/ui/lib/search/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
offset_type: 'latest',
partitions: %w[0 1],
phrase: 'test phrase',
timestamp: Time.now.to_i
timestamp: (Time.now.to_f * 1_000).to_i
}
end

Expand All @@ -26,7 +26,7 @@
Karafka::Messages::Message,
partition: 0,
offset: 0,
timestamp: Time.now.to_i,
timestamp: (Time.now.to_f * 1_000).to_i,
clean!: nil,
raw_payload: ''
)
Expand All @@ -37,7 +37,7 @@
Karafka::Messages::Message,
partition: 1,
offset: 1,
timestamp: Time.now.to_i,
timestamp: (Time.now.to_f * 1_000).to_i,
clean!: nil,
raw_payload: ''
)
Expand Down Expand Up @@ -242,7 +242,7 @@

search_criteria[:limit] = 100
search_criteria[:offset_type] = 'timestamp'
search_criteria[:timestamp] = Time.now.to_i
search_criteria[:timestamp] = (Time.now.to_f * 1_000).to_i
search_criteria[:partitions] = %w[all]
end

Expand All @@ -262,7 +262,7 @@

search_criteria[:limit] = 100
search_criteria[:offset_type] = 'timestamp'
search_criteria[:timestamp] = Time.now.to_i - 100
search_criteria[:timestamp] = ((Time.now.to_f - 100) * 1_000).to_i
search_criteria[:partitions] = %w[all]
end

Expand Down

0 comments on commit 5f0a975

Please sign in to comment.