Skip to content

Commit

Permalink
Merge branch 'dev' for release 5.6.10
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainbx committed Feb 2, 2023
2 parents 7f99007 + ed5670d commit 77272ad
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 78 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog Fab-manager

## v5.6.10 2023 February 02

- Optimized memory consumption in statistics fetcher service
- Fix a bug: private method `create_statistic_subtype'
- [TODO DEPLOY] `rails db:seed`

## v5.6.9 2023 February 02

- Updated shakapaker to 6.5.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Statistics::Builders::MembersBuilderService
class << self
def build(options = default_options)
# account list
Statistics::FetcherService.members_list(options).each do |m|
Statistics::FetcherService.each_member(options) do |m|
Stats::Account.create({ date: format_date(m[:date]),
type: 'member',
subType: 'created',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Statistics::Builders::ProjectsBuilderService
class << self
def build(options = default_options)
# project list
Statistics::FetcherService.projects_list(options).each do |p|
Statistics::FetcherService.each_project(options) do |p|
Stats::Project.create({ date: format_date(p[:date]),
type: 'project',
subType: 'published',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class << self
def build(options = default_options)
# machine/space/training list
%w[machine space training event].each do |category|
Statistics::FetcherService.send("reservations_#{category}_list", options).each do |r|
Statistics::FetcherService.send("each_#{category}_reservation", options) do |r|
%w[booking hour].each do |type|
stat = "Stats::#{category.capitalize}"
.constantize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def build(options = default_options)
}
# orders list
states.each do |sub_type, order_states|
Statistics::FetcherService.store_orders_list(order_states, options).each do |o|
Statistics::FetcherService.each_store_order(order_states, options) do |o|
Stats::Order.create({ date: format_date(o[:date]),
type: 'store',
subType: sub_type,
Expand Down
157 changes: 86 additions & 71 deletions app/services/statistics/fetcher_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def subscriptions_list(options = default_options)
result = []
InvoiceItem.where("object_type = '#{Subscription.name}' AND invoice_items.created_at >= :start_date " \
'AND invoice_items.created_at <= :end_date', options)
.eager_load(invoice: [:coupon]).each do |i|
.eager_load(invoice: [:coupon]).find_each do |i|
next if i.invoice.is_a?(Avoir)

sub = i.object
Expand All @@ -39,97 +39,105 @@ def subscriptions_list(options = default_options)
result
end

def reservations_machine_list(options = default_options)
result = []
# @param options [Hash]
# @yield [reservation]
# @yieldparam [Hash]
def each_machine_reservation(options = default_options)
Reservation
.where("reservable_type = 'Machine' AND slots_reservations.canceled_at IS NULL AND " \
'reservations.created_at >= :start_date AND reservations.created_at <= :end_date', options)
.eager_load(:slots, :slots_reservations, :invoice_items, statistic_profile: [:group])
.each do |r|
.find_each do |r|
next unless r.reservable

profile = r.statistic_profile
result.push({ date: r.created_at.to_date,
reservation_id: r.id,
machine_id: r.reservable.id,
machine_type: r.reservable.friendly_id,
machine_name: r.reservable.name,
slot_dates: r.slots.map(&:start_at).map(&:to_date),
nb_hours: (r.slots.map(&:duration).map(&:to_i).reduce(:+) / 3600.0).to_i,
ca: calcul_ca(r.original_invoice) }.merge(user_info(profile)))
result = { date: r.created_at.to_date,
reservation_id: r.id,
machine_id: r.reservable.id,
machine_type: r.reservable.friendly_id,
machine_name: r.reservable.name,
slot_dates: r.slots.map(&:start_at).map(&:to_date),
nb_hours: (r.slots.map(&:duration).map(&:to_i).reduce(:+) / 3600.0).to_i,
ca: calcul_ca(r.original_invoice) }.merge(user_info(profile))
yield result
end
result
end

def reservations_space_list(options = default_options)
result = []
# @param options [Hash]
# @yield [reservation]
# @yieldparam [Hash]
def each_space_reservation(options = default_options)
Reservation
.where("reservable_type = 'Space' AND slots_reservations.canceled_at IS NULL AND " \
'reservations.created_at >= :start_date AND reservations.created_at <= :end_date', options)
.eager_load(:slots, :slots_reservations, :invoice_items, statistic_profile: [:group])
.each do |r|
.find_each do |r|
next unless r.reservable

profile = r.statistic_profile
result.push({ date: r.created_at.to_date,
reservation_id: r.id,
space_id: r.reservable.id,
space_name: r.reservable.name,
space_type: r.reservable.slug,
slot_dates: r.slots.map(&:start_at).map(&:to_date),
nb_hours: (r.slots.map(&:duration).map(&:to_i).reduce(:+) / 3600.0).to_i,
ca: calcul_ca(r.original_invoice) }.merge(user_info(profile)))
result = { date: r.created_at.to_date,
reservation_id: r.id,
space_id: r.reservable.id,
space_name: r.reservable.name,
space_type: r.reservable.slug,
slot_dates: r.slots.map(&:start_at).map(&:to_date),
nb_hours: (r.slots.map(&:duration).map(&:to_i).reduce(:+) / 3600.0).to_i,
ca: calcul_ca(r.original_invoice) }.merge(user_info(profile))
yield result
end
result
end

def reservations_training_list(options = default_options)
result = []
# @param options [Hash]
# @yield [reservation]
# @yieldparam [Hash]
def each_training_reservation(options = default_options)
Reservation
.where("reservable_type = 'Training' AND slots_reservations.canceled_at IS NULL AND " \
'reservations.created_at >= :start_date AND reservations.created_at <= :end_date', options)
.eager_load(:slots, :slots_reservations, :invoice_items, statistic_profile: [:group])
.each do |r|
.find_each do |r|
next unless r.reservable

profile = r.statistic_profile
slot = r.slots.first
result.push({ date: r.created_at.to_date,
reservation_id: r.id,
training_id: r.reservable.id,
training_type: r.reservable.friendly_id,
training_name: r.reservable.name,
training_date: slot.start_at.to_date,
nb_hours: difference_in_hours(slot.start_at, slot.end_at),
ca: calcul_ca(r.original_invoice) }.merge(user_info(profile)))
result = { date: r.created_at.to_date,
reservation_id: r.id,
training_id: r.reservable.id,
training_type: r.reservable.friendly_id,
training_name: r.reservable.name,
training_date: slot.start_at.to_date,
nb_hours: difference_in_hours(slot.start_at, slot.end_at),
ca: calcul_ca(r.original_invoice) }.merge(user_info(profile))
yield result
end
result
end

def reservations_event_list(options = default_options)
result = []
# @param options [Hash]
# @yield [reservation]
# @yieldparam [Hash]
def each_event_reservation(options = default_options)
Reservation
.where("reservable_type = 'Event' AND slots_reservations.canceled_at IS NULL AND " \
'reservations.created_at >= :start_date AND reservations.created_at <= :end_date', options)
.eager_load(:slots, :slots_reservations, :invoice_items, statistic_profile: [:group])
.each do |r|
.find_each do |r|
next unless r.reservable

profile = r.statistic_profile
slot = r.slots.first
result.push({ date: r.created_at.to_date,
reservation_id: r.id,
event_id: r.reservable.id,
event_type: r.reservable.category.slug,
event_name: r.reservable.name,
event_date: slot.start_at.to_date,
event_theme: (r.reservable.event_themes.first ? r.reservable.event_themes.first.name : ''),
age_range: (r.reservable.age_range_id ? r.reservable.age_range.name : ''),
nb_places: r.total_booked_seats,
nb_hours: difference_in_hours(slot.start_at, slot.end_at),
ca: calcul_ca(r.original_invoice) }.merge(user_info(profile)))
result = { date: r.created_at.to_date,
reservation_id: r.id,
event_id: r.reservable.id,
event_type: r.reservable.category.slug,
event_name: r.reservable.name,
event_date: slot.start_at.to_date,
event_theme: (r.reservable.event_themes.first ? r.reservable.event_themes.first.name : ''),
age_range: (r.reservable.age_range_id ? r.reservable.age_range.name : ''),
nb_places: r.total_booked_seats,
nb_hours: difference_in_hours(slot.start_at, slot.end_at),
ca: calcul_ca(r.original_invoice) }.merge(user_info(profile))
yield result
end
result
end

def members_ca_list(options = default_options)
Expand All @@ -139,7 +147,7 @@ def members_ca_list(options = default_options)
users_list = []
Reservation.where('reservations.created_at >= :start_date AND reservations.created_at <= :end_date', options)
.eager_load(:slots, :invoice_items, statistic_profile: [:group])
.each do |r|
.find_each do |r|
next unless r.reservable

reservations_ca_list.push(
Expand All @@ -148,7 +156,7 @@ def members_ca_list(options = default_options)
end
Avoir.where('invoices.created_at >= :start_date AND invoices.created_at <= :end_date', options)
.eager_load(:invoice_items, statistic_profile: [:group])
.each do |i|
.find_each do |i|
# the following line is a workaround for issue #196
profile = i.statistic_profile || i.main_item.object&.wallet&.user&.statistic_profile
avoirs_ca_list.push({ date: i.created_at.to_date, ca: calcul_avoir_ca(i) || 0 }.merge(user_info(profile)))
Expand All @@ -162,43 +170,50 @@ def members_ca_list(options = default_options)
users_list
end

def members_list(options = default_options)
result = []
# @param options [Hash]
# @yield [reservation]
# @yieldparam [Hash]
def each_member(options = default_options)
member = Role.find_by(name: 'member')
StatisticProfile.where('role_id = :member AND created_at >= :start_date AND created_at <= :end_date',
options.merge(member: member.id))
.each do |sp|
.find_each do |sp|
next if sp.user&.need_completion?

result.push({ date: sp.created_at.to_date }.merge(user_info(sp)))
result = { date: sp.created_at.to_date }.merge(user_info(sp))
yield result
end
result
end

def projects_list(options = default_options)
result = []
# @param options [Hash]
# @yield [reservation]
# @yieldparam [Hash]
def each_project(options = default_options)
Project.where('projects.published_at >= :start_date AND projects.published_at <= :end_date', options)
.eager_load(:licence, :themes, :components, :machines, :project_users, author: [:group])
.each do |p|
result.push({ date: p.created_at.to_date }.merge(user_info(p.author)).merge(project_info(p)))
.find_each do |p|
result = { date: p.created_at.to_date }.merge(user_info(p.author)).merge(project_info(p))
yield result
end
result
end

def store_orders_list(states, options = default_options)
result = []
# @param states [Array<String>]
# @param options [Hash]
# @yield [reservation]
# @yieldparam [Hash]
def each_store_order(states, options = default_options)
Order.includes(order_items: [:orderable])
.joins(:order_items, :order_activities)
.where(order_items: { orderable_type: 'Product' })
.where(orders: { state: states })
.where('order_activities.created_at >= :start_date AND order_activities.created_at <= :end_date', options)
.group('orders.id')
.each do |o|
result.push({ date: o.created_at.to_date, ca: calcul_ca(o.invoice) }
.merge(user_info(o.statistic_profile))
.merge(store_order_info(o)))
.find_each do |o|
result = { date: o.created_at.to_date, ca: calcul_ca(o.invoice) }
.merge(user_info(o.statistic_profile))
.merge(store_order_info(o))
yield result
end
result
end

private
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/sentry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production.
config.traces_sample_rate = 0.1
config.traces_sample_rate = 0.01
config.environment = Rails.env
config.release = Version.current
end
Expand Down
2 changes: 1 addition & 1 deletion db/seeds/statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
Plan.find_each do |plan|
type = plan.find_statistic_type
subtype = if StatisticSubType.find_by(key: plan.slug).nil?
plan.create_statistic_subtype
StatisticSubType.create!(key: plan.slug, label: plan.name)
else
StatisticSubType.find_by(key: plan.slug)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/tasks/fablab/es.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace :fablab do
namespace :es do
desc '(re)Build ElasticSearch fablab base for stats'
task build_stats: :environment do
exit unless Setting.get('statistics_module')

delete_stats_index
create_stats_index
create_stats_mappings
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fab-manager",
"version": "5.6.9",
"version": "5.6.10",
"description": "Fab-manager is the FabLab management solution. It provides a comprehensive, web-based, open-source tool to simplify your administrative tasks and your marker's projects.",
"keywords": [
"fablab",
Expand Down

0 comments on commit 77272ad

Please sign in to comment.