status.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module MonitoringHelper
  3. class Status
  4. INCLUDE_CLASSES = [User, Group, Overview, Ticket, Ticket::Article, TextModule, Taskbar, ObjectManager::Attribute, KnowledgeBase::Category, KnowledgeBase::Answer].freeze
  5. def fetch_status
  6. {
  7. counts: counts,
  8. last_created_at: last_created_at,
  9. last_login: last_login,
  10. agents: agents_count,
  11. storage: storage
  12. }
  13. end
  14. private
  15. def last_login
  16. User
  17. .where.not(last_login: nil)
  18. .order(last_login: :desc)
  19. .first
  20. &.last_login
  21. end
  22. def agents_count
  23. User.with_permissions('ticket.agent').count
  24. end
  25. def counts
  26. INCLUDE_CLASSES.each_with_object({}) do |elem, memo|
  27. memo[elem.table_name] = elem.count
  28. end
  29. end
  30. def last_created_at
  31. INCLUDE_CLASSES.each_with_object({}) do |elem, memo|
  32. memo[elem.table_name] = elem.last&.created_at
  33. end
  34. end
  35. def storage
  36. return if ActiveRecord::Base.connection_db_config.configuration_hash[:adapter] != 'postgresql'
  37. sql = 'SELECT SUM(CAST(coalesce(size, \'0\') AS INTEGER)) FROM stores'
  38. stored = ActiveRecord::Base.connection.exec_query(sql).first&.dig('sum')
  39. return if !stored
  40. {
  41. kB: stored / 1024,
  42. MB: stored / 1024 / 1024,
  43. GB: stored / 1024 / 1024 / 1024,
  44. }
  45. end
  46. end
  47. end