2020-01-13 17:05:53 +01:00
|
|
|
# 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?
|
2022-12-01 16:34:14 +01:00
|
|
|
!!Sidekiq.redis(&:info)
|
2020-01-13 17:05:53 +01:00
|
|
|
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'
|
2022-12-01 16:34:14 +01:00
|
|
|
!!response.body
|
|
|
|
rescue Elasticsearch::Transport::Transport::Error, Faraday::ConnectionFailed
|
2020-07-01 15:13:44 +02:00
|
|
|
false
|
2020-01-13 17:05:53 +01:00
|
|
|
end
|
|
|
|
|
2020-01-14 14:33:00 +01:00
|
|
|
def self.migrations?
|
2020-04-07 15:19:41 +02:00
|
|
|
!ActiveRecord::Base.connection.migration_context.needs_migration?
|
2020-01-14 14:33:00 +01:00
|
|
|
end
|
|
|
|
|
2020-01-15 12:48:55 +01:00
|
|
|
def self.row_stats
|
2020-01-14 14:33:00 +01:00
|
|
|
require 'version'
|
2020-01-15 12:48:55 +01:00
|
|
|
{
|
2020-01-14 14:33:00 +01:00
|
|
|
version: Version.current,
|
|
|
|
members: User.members.count,
|
|
|
|
admins: User.admins.count,
|
2022-12-01 16:34:14 +01:00
|
|
|
managers: User.managers.count,
|
2020-01-14 14:33:00 +01:00
|
|
|
availabilities: last_week_availabilities,
|
|
|
|
reservations: last_week_new_reservations,
|
2022-12-01 16:34:14 +01:00
|
|
|
orders: last_week_orders,
|
2020-05-27 12:29:45 +02:00
|
|
|
plans: Setting.get('plans_module'),
|
2020-05-26 13:59:40 +02:00
|
|
|
spaces: Setting.get('spaces_module'),
|
2020-06-08 17:42:59 +02:00
|
|
|
online_payment: Setting.get('online_payment_module'),
|
2022-12-01 16:34:14 +01:00
|
|
|
gateway: Setting.get('payment_gateway'),
|
|
|
|
wallet: Setting.get('wallet_module'),
|
|
|
|
statistics: Setting.get('statistics_module'),
|
|
|
|
trainings: Setting.get('trainings_module'),
|
|
|
|
public_agenda: Setting.get('public_agenda_module'),
|
|
|
|
machines: Setting.get('machines_module'),
|
|
|
|
store: Setting.get('store_module'),
|
2020-05-26 18:07:07 +02:00
|
|
|
invoices: Setting.get('invoicing_module'),
|
2020-06-08 15:08:07 +02:00
|
|
|
openlab: Setting.get('openlab_app_secret').present?
|
2020-01-15 12:48:55 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.stats
|
2020-05-13 15:02:03 +02:00
|
|
|
enable = Setting.get('fab_analytics')
|
2020-01-15 12:48:55 +01:00
|
|
|
return false if enable == 'false'
|
|
|
|
|
|
|
|
require 'openssl'
|
|
|
|
require 'base64'
|
|
|
|
|
|
|
|
row_stats.to_json.to_s
|
2020-01-14 14:33:00 +01:00
|
|
|
|
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
|
2020-01-14 14:33:00 +01:00
|
|
|
|
|
|
|
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))
|
2020-01-14 14:33:00 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# availabilities for the last week
|
|
|
|
def self.last_week_availabilities
|
2023-02-14 13:10:58 +01:00
|
|
|
Availability.where('start_at >= ? AND end_at <= ?', 7.days.ago, Time.current).count
|
2020-01-14 14:33:00 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# reservations made during the last week
|
|
|
|
def self.last_week_new_reservations
|
2023-02-14 13:10:58 +01:00
|
|
|
Reservation.where('created_at >= ? AND created_at < ?', 7.days.ago, Time.current).count
|
2020-01-13 17:05:53 +01:00
|
|
|
end
|
|
|
|
|
2022-12-01 16:34:14 +01:00
|
|
|
def self.last_week_orders
|
2023-02-14 13:10:58 +01:00
|
|
|
Order.where('created_at >= ? AND created_at < ?', 7.days.ago, Time.current).where.not(state: 'cart').count
|
2022-12-01 16:34:14 +01:00
|
|
|
end
|
|
|
|
end
|