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



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

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



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

def current_offset?
  true
end

#first_offsetInteger

Returns -1 because it will then select the highest offsets.

Returns:

  • (Integer)

    -1 because it will then select the highest offsets



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

def first_offset
  -1
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 && @previous_offset != false
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)


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

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



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

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



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

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



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

def previous_offset?
  !!@previous_offset
end