1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-29 18:52:22 +01:00

health endpoint & automated version check

This commit is contained in:
Sylvain 2020-01-14 14:33:00 +01:00
parent 64764e5498
commit bb48a13f83
15 changed files with 86 additions and 25 deletions

View File

@ -55,7 +55,7 @@ class API::ExportsController < API::ApiController
when 'reservations'
export = export.where('created_at > ?', Reservation.maximum('updated_at'))
when 'members'
export = export.where('created_at > ?', User.with_role(:member).maximum('updated_at'))
export = export.where('created_at > ?', User.members.maximum('updated_at'))
else
raise ArgumentError, "Unknown export users/#{type}"
end

View File

@ -116,10 +116,10 @@ class API::MembersController < API::ApiController
authorize :export
last_update = [
User.with_role(:member).maximum('updated_at'),
Profile.where(user_id: User.with_role(:member)).maximum('updated_at'),
InvoicingProfile.where(user_id: User.with_role(:member)).maximum('updated_at'),
StatisticProfile.where(user_id: User.with_role(:member)).maximum('updated_at'),
User.members.maximum('updated_at'),
Profile.where(user_id: User.members).maximum('updated_at'),
InvoicingProfile.where(user_id: User.members).maximum('updated_at'),
StatisticProfile.where(user_id: User.members).maximum('updated_at'),
Subscription.maximum('updated_at')
].max

View File

@ -5,6 +5,8 @@ class HealthController < ActionController::Base
respond_to :json
def status
require 'version'
render json: {
name: 'Fab-Manager',
status: 'running',
@ -13,7 +15,12 @@ class HealthController < ActionController::Base
redis: HealthService.redis?,
elasticsearch: HealthService.elasticsearch?
},
stats: HealthService.stats
up_to_date: {
migrations: HealthService.migrations?,
version: Version.up_to_date?
},
stats: HealthService.stats,
tagline: 'Manage the DIY'
}
end
end
end

View File

@ -93,6 +93,10 @@ class User < ActiveRecord::Base
User.with_role(:admin)
end
def self.members
User.with_role(:member)
end
def self.superadmin
return unless Rails.application.secrets.superadmin_email.present?

View File

@ -27,9 +27,43 @@ class HealthService
false
end
def self.migrations?
!ActiveRecord::Migrator.needs_migration?
end
def self.stats
# TODO
'651ad6a5z1daz65d1az65d156d1fz16'
require 'version'
require 'openssl'
require 'base64'
stats = {
version: Version.current,
members: User.members.count,
admins: User.admins.count,
availabilities: last_week_availabilities,
reservations: last_week_new_reservations,
plans: !Rails.application.secrets.fablab_without_plans,
spaces: !Rails.application.secrets.fablab_without_spaces,
online_payment: !Rails.application.secrets.fablab_without_online_payments,
invoices: !Rails.application.secrets.fablab_without_invoices,
openlab: Rails.application.secrets.openlab_app_secret.present?
}.to_json.to_s
key = Setting.find_by(name: 'hub_public_key')&.value
return 'disabled' unless key
public_key = OpenSSL::PKey::RSA.new(key)
Base64.encode64(public_key.public_encrypt(stats))
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

View File

@ -49,7 +49,7 @@ class UsersExportService
# export members
def export_members(export)
@members = User.with_role(:member)
@members = User.members
.includes(:group, :tags, :projects, :profile,
invoicing_profile: [:invoices, :address, organization: [:address]],
statistic_profile: [:trainings, subscriptions: [:plan]])

View File

@ -9,7 +9,7 @@ class NotifyPrivacyUpdateWorker
# notify all users
NotificationCenter.call type: :notify_privacy_policy_changed,
receiver: User.with_role(:member).all,
receiver: User.members.all,
attached_object: setting
end

View File

@ -9,7 +9,7 @@ class VersionCheckWorker
res = FabHub.fab_manager_version_check
setting_ver = Setting.find_or_initialize_by(name: 'hub_last_version')
setting_ver.value = { version: res['semver'], security: res['security'], status: res['up_to_date'] }.to_json.to_s
setting_ver.value = { version: res['last_version']['semver'], security: res['last_version']['security'], status: res['up_to_date'] }.to_json.to_s
setting_ver.save!
setting_key = Setting.find_or_initialize_by(name: 'hub_public_key')

View File

@ -74,6 +74,9 @@ module Fablab
config.after_initialize do
plugins = FabManager.plugins
plugins&.each(&:notify_after_initialize)
require 'version'
Version.check_and_schedule
end
end
end

View File

@ -39,8 +39,4 @@ free_disk_space:
class: "FreeDiskSpaceWorker"
queue: default
version_check:
cron: "15 1 * * 0" # every sunday at 1:15am
class: "VersionCheckWorker"
<%= PluginRegistry.insert_code('yml.schedule') %>

View File

@ -22,15 +22,15 @@ class FabHub
uri = URI.join(hub_base_url, rel_url)
uri.query = URI.encode_www_form(payload)
res = Net::HTTP.get_response(uri, read_timeout: 20)
res = Net::HTTP.get_response(uri)
JSON.parse(res.body) if res.is_a?(Net::HTTPSuccess)
end
def self.hub_base_url
if Rails.env.production?
ENV['HUB_BASE_URL'] || 'https://hub.fab-manager.com/api'
ENV['HUB_BASE_URL'] || 'https://hub.fab-manager.com'
else
ENV['HUB_BASE_URL'] || 'http://localhost:3000/api'
ENV['HUB_BASE_URL'] || 'http://localhost:3000'
end
end
end
end

View File

@ -48,7 +48,7 @@ namespace :fablab do
desc 'sync users to the stripe database'
task sync_members: :environment do
User.with_role(:member).each do |member|
User.members.each do |member|
begin
stp_customer = Stripe::Customer.retrieve member.stp_customer_id
StripeWorker.perform_async(:create_stripe_customer, member.id) if stp_customer.nil? || stp_customer[:deleted]

View File

@ -6,4 +6,21 @@ class Version
package = File.read('package.json')
JSON.parse(package)['version']
end
def self.up_to_date?
hub_version = Setting.find_by(name: 'hub_last_version')&.value
return unless hub_version
json = JSON.parse(hub_version)
json['status']
end
def self.check_and_schedule
VersionCheckWorker.perform_async
# every sunday at 1:15am
m = DateTime.current.minute
h = DateTime.current.hour
d = DateTime.current.cwday
Sidekiq::Cron::Job.create(name: 'Automatic version check', cron: "#{m} #{h} * * #{d}", class: 'VersionCheckWorker')
end
end

View File

@ -1,8 +1,8 @@
module Reservations
class CreateAsAdminTest < ActionDispatch::IntegrationTest
setup do
@user_without_subscription = User.with_role(:member).without_subscription.first
@user_with_subscription = User.with_role(:member).with_subscription.second
@user_without_subscription = User.members.without_subscription.first
@user_with_subscription = User.members.with_subscription.second
@admin = User.with_role(:admin).first
login_as(@admin, scope: :user)
end

View File

@ -1,8 +1,8 @@
module Reservations
class CreateTest < ActionDispatch::IntegrationTest
setup do
@user_without_subscription = User.with_role(:member).without_subscription.first
@user_with_subscription = User.with_role(:member).with_subscription.second
@user_without_subscription = User.members.without_subscription.first
@user_with_subscription = User.members.with_subscription.second
end
test 'user without subscription reserves a machine with success' do