software_versions_dimension.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # frozen_string_literal: true
  2. class Admin::Metrics::Dimension::SoftwareVersionsDimension < Admin::Metrics::Dimension::BaseDimension
  3. include Redisable
  4. def key
  5. 'software_versions'
  6. end
  7. protected
  8. def perform_query
  9. [mastodon_version, ruby_version, postgresql_version, redis_version, elasticsearch_version].compact
  10. end
  11. def mastodon_version
  12. value = Mastodon::Version.to_s
  13. {
  14. key: 'mastodon',
  15. human_key: 'Mastodon',
  16. value: value,
  17. human_value: value,
  18. }
  19. end
  20. def ruby_version
  21. yjit = defined?(RubyVM::YJIT) && RubyVM::YJIT.enabled?
  22. value = "#{RUBY_VERSION}p#{RUBY_PATCHLEVEL}#{yjit ? ' +YJIT' : ''}"
  23. {
  24. key: 'ruby',
  25. human_key: 'Ruby',
  26. value: value,
  27. human_value: value,
  28. }
  29. end
  30. def postgresql_version
  31. value = ActiveRecord::Base.connection.execute('SELECT VERSION()').first['version'].match(/\A(?:PostgreSQL |)([^\s]+).*\z/)[1]
  32. {
  33. key: 'postgresql',
  34. human_key: 'PostgreSQL',
  35. value: value,
  36. human_value: value,
  37. }
  38. end
  39. def redis_version
  40. value = redis_info['redis_version']
  41. {
  42. key: 'redis',
  43. human_key: 'Redis',
  44. value: value,
  45. human_value: value,
  46. }
  47. end
  48. def elasticsearch_version
  49. return unless Chewy.enabled?
  50. client_info = Chewy.client.info
  51. version = client_info.dig('version', 'number')
  52. {
  53. key: 'elasticsearch',
  54. human_key: client_info.dig('version', 'distribution') == 'opensearch' ? 'OpenSearch' : 'Elasticsearch',
  55. value: version,
  56. human_value: version,
  57. }
  58. rescue Faraday::ConnectionFailed, Elasticsearch::Transport::Transport::Error
  59. nil
  60. end
  61. def redis_info
  62. @redis_info ||= if redis.is_a?(Redis::Namespace)
  63. redis.redis.info
  64. else
  65. redis.info
  66. end
  67. end
  68. end