objects_total_count.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::Unit::Import::Zendesk::ObjectsTotalCount < Sequencer::Unit::Common::Provider::Attribute
  3. include ::Sequencer::Unit::Import::Common::Model::Statistics::Mixin::EmptyDiff
  4. uses :client
  5. private
  6. def statistics_diff
  7. %i[Groups Users Organizations Tickets].index_with do |object|
  8. empty_diff.merge(
  9. total: request(object).count!
  10. )
  11. end
  12. end
  13. # the special "incremental_export" logic is needed because Zendesk
  14. # archives records and doesn't return them via e.g. client.tickets
  15. # endpoint as described here:
  16. # https://github.com/zammad/zammad/issues/558#issuecomment-267951351
  17. # Counting via the incremental_export endpoint has the limitations
  18. # that it returns max. 1000. That's why we need to update the total
  19. # number while importing in the resource loop
  20. def request(object)
  21. require 'zendesk_api' # Only load this gem when it is really used.
  22. resource_class = "::ZendeskAPI::#{object.to_s.singularize}".safe_constantize
  23. if resource_class.respond_to?(:incremental_export)
  24. # read as: ::ZendeskAPI::Ticket.incremental_export(client, 1)
  25. resource_class.incremental_export(client, 1)
  26. else
  27. # read as: client.groups
  28. client.send(object.to_s.underscore.to_sym)
  29. end
  30. end
  31. end