Class: Karafka::Web::Ui::Lib::Paginations::OffsetBased

Inherits:
Base
  • Object
show all
Defined in:
lib/karafka/web/ui/lib/paginations/offset_based.rb

Overview

Kafka offset based pagination backend

Allows us to support paginating over offsets

Instance Attribute Summary

Attributes inherited from Base

#current_offset, #previous_offset

Instance Method Summary collapse

Constructor Details

#initialize(previous_offset, current_offset, next_offset, visible_offsets) ⇒ OffsetBased

Returns a new instance of OffsetBased.

Parameters:

  • previous_offset (Integer, false)

    previous offset or false if should not be presented

  • current_offset (Integer)

    current offset

  • next_offset (Integer, Boolean)

    should we show next offset page button. If false it will not be presented.

  • visible_offsets (Array<Integer>)

    offsets that are visible in the paginated view. It is needed for the current page label



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/karafka/web/ui/lib/paginations/offset_based.rb', line 19

def initialize(
  previous_offset,
  current_offset,
  next_offset,
  visible_offsets
)
  @previous_offset = previous_offset
  @current_offset = current_offset
  @next_offset = next_offset
  @visible_offsets = visible_offsets
  super()
end

Instance Method Details

#current_labelString

Returns label of the current page. It is combined out of the first and the last offsets to show the range where we are. It will be empty if no offsets but this is not a problem as then we should not display pagination at all.

Returns:

  • (String)

    label of the current page. It is combined out of the first and the last offsets to show the range where we are. It will be empty if no offsets but this is not a problem as then we should not display pagination at all



81
82
83
84
85
# File 'lib/karafka/web/ui/lib/paginations/offset_based.rb', line 81

def current_label
  first = @visible_offsets.first
  last = @visible_offsets.last
  [first, last].compact.uniq.join(' - ').to_s
end

#current_offset?Boolean

Returns We show current label with offsets that are present on the given page.

Returns:

  • (Boolean)

    We show current label with offsets that are present on the given page



61
62
63
# File 'lib/karafka/web/ui/lib/paginations/offset_based.rb', line 61

def current_offset?
  true
end

#first_offsetBoolean

Returns first page offset is always nothing because we use the default -1 for the offset.

Returns:

  • (Boolean)

    first page offset is always nothing because we use the default -1 for the offset.



50
51
52
# File 'lib/karafka/web/ui/lib/paginations/offset_based.rb', line 50

def first_offset
  false
end

#first_offset?Boolean

Returns active only when we are not on the first page. First page is always indicated by the current offset being -1. If there is someone that sets up the current offset to a value equal to the last message in the topic partition, we do not consider it as a first page and we allow to “reset” to -1 via the first page button.

Returns:

  • (Boolean)

    active only when we are not on the first page. First page is always indicated by the current offset being -1. If there is someone that sets up the current offset to a value equal to the last message in the topic partition, we do not consider it as a first page and we allow to “reset” to -1 via the first page button



44
45
46
# File 'lib/karafka/web/ui/lib/paginations/offset_based.rb', line 44

def first_offset?
  @current_offset != -1
end

#next_offsetInteger

If there is no next offset, we point to 0 as there should be no smaller offset than that in Kafka ever

Returns:

  • (Integer)


74
75
76
# File 'lib/karafka/web/ui/lib/paginations/offset_based.rb', line 74

def next_offset
  next_offset? ? @next_offset : 0
end

#next_offset?Boolean

Returns move to the next page if not false. False indicates, that there is no next page to move to.

Returns:

  • (Boolean)

    move to the next page if not false. False indicates, that there is no next page to move to



67
68
69
# File 'lib/karafka/web/ui/lib/paginations/offset_based.rb', line 67

def next_offset?
  !!@next_offset
end

#offset_keyString

Returns for offset based pagination we use the offset param name.

Returns:

  • (String)

    for offset based pagination we use the offset param name



88
89
90
# File 'lib/karafka/web/ui/lib/paginations/offset_based.rb', line 88

def offset_key
  'offset'
end

#paginate?Boolean

Show pagination only when there is more than one page of results to be presented

Returns:

  • (Boolean)


35
36
37
# File 'lib/karafka/web/ui/lib/paginations/offset_based.rb', line 35

def paginate?
  @current_offset && (!!@previous_offset || !!@next_offset)
end

#previous_offset?Boolean

Returns Active previous page link when it is not the first page.

Returns:

  • (Boolean)

    Active previous page link when it is not the first page



55
56
57
# File 'lib/karafka/web/ui/lib/paginations/offset_based.rb', line 55

def previous_offset?
  !!@previous_offset
end