1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/services/health_service.rb

79 lines
2.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# Various methods to check the application status
class HealthService
def self.database?
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection
ActiveRecord::Base.connected?
rescue ActiveRecord::ActiveRecordError
false
end
def self.redis?
!!Sidekiq.redis(&:info) # rubocop:disable Style/DoubleNegation
rescue Redis::CannotConnectError
false
end
def self.elasticsearch?
require 'elasticsearch/transport'
client = Elasticsearch::Client.new host: "http://#{Rails.application.secrets.elaticsearch_host}:9200"
response = client.perform_request 'GET', '_cluster/health'
!!response.body # rubocop:disable Style/DoubleNegation
rescue Elasticsearch::Transport::Transport::Error
false
rescue Faraday::ConnectionFailed
false
end
def self.migrations?
2020-04-07 15:19:41 +02:00
!ActiveRecord::Base.connection.migration_context.needs_migration?
end
def self.row_stats
require 'version'
{
version: Version.current,
members: User.members.count,
admins: User.admins.count,
availabilities: last_week_availabilities,
reservations: last_week_new_reservations,
2020-05-27 12:29:45 +02:00
plans: Setting.get('plans_module'),
spaces: Setting.get('spaces_module'),
2020-06-08 17:42:59 +02:00
online_payment: Setting.get('online_payment_module'),
invoices: Setting.get('invoicing_module'),
openlab: Setting.get('openlab_app_secret').present?
}
end
def self.stats
2020-05-13 15:02:03 +02:00
enable = Setting.get('fab_analytics')
return false if enable == 'false'
require 'openssl'
require 'base64'
row_stats.to_json.to_s
2020-05-13 15:02:03 +02:00
key = Setting.get('hub_public_key')
2020-01-15 10:46:04 +01:00
return false unless key
public_key = OpenSSL::PKey::RSA.new(key)
2020-02-26 12:00:51 +01:00
Base64.encode64(public_key.public_encrypt(row_stats.to_json.to_s))
end
# availabilities for the last week
def self.last_week_availabilities
Availability.where('start_at >= ? AND end_at <= ?', DateTime.current - 7.days, DateTime.current).count
end
# reservations made during the last week
def self.last_week_new_reservations
Reservation.where('created_at >= ? AND created_at < ?', DateTime.current - 7.days, DateTime.current).count
end
end