1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-20 14:54:15 +01:00

(quality) Optimized statistics fetcher service

This commit is contained in:
Sylvain 2023-02-02 15:43:05 +01:00
parent ce23597657
commit 4d942b4161
6 changed files with 92 additions and 75 deletions

View File

@ -1,5 +1,7 @@
# Changelog Fab-manager
- Optimized memory consumption in statistics fetcher service
## v5.6.9 2023 February 02
- Updated shakapaker to 6.5.5

View File

@ -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',

View File

@ -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',

View File

@ -8,7 +8,7 @@ class Statistics::Builders::ReservationsBuilderService
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

View File

@ -13,7 +13,7 @@ class Statistics::Builders::StoreOrdersBuilderService
}
# 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,

View File

@ -13,7 +13,7 @@ class Statistics::FetcherService
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
@ -39,97 +39,105 @@ class Statistics::FetcherService
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)
@ -139,7 +147,7 @@ class Statistics::FetcherService
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(
@ -148,7 +156,7 @@ class Statistics::FetcherService
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)))
@ -162,43 +170,50 @@ class Statistics::FetcherService
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